Hi,
I have the following code which is driving me crazy. I compile it on
MacOSX and it keeps crashing upon entering a command in the program
(ran trough gdb)
[command]pwd
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION _FAILURE at address: 0x00000000
0x000105bc in parse_commands (command=0xbffe 70e4 "pwd") at
/Users/alefveld/Projects/project1/client.c:198
198 while((argv[i++]=strtok(command ,""))) {
(gdb) quit
this is the conflicting piece of code. i suspect it's in the ptr to ptr
because when working with a normal ptr no problem occurs. i just don't
want to make a dozen strtok calls. i would like all the arguments of a
single command to be split up in tokens and put nicely in my **argv. do
i need to initialize anything? is the char *ptr strtok giving me not
correct? do i need to cast it ?
ps *command is a '\0' ended string.
int parse_commands( char *command)
{
int i=0, argc=0;
char **argv={0}; // initialize everything to NULL
connection_inde x *tmp=head; // not relevant here
/* Split up command in tokens */
while((argv[i++]=strtok(command ,""))) {
command=NULL;
argc++;}
/* A command always exists of 2 or more arguments. */
if(argc<=1) return -1;
thanks a lot,
Rgds,
Alef 3 2638 al**@xs4all.nl wrote: Hi,
I have the following code which is driving me crazy. I compile it on MacOSX and it keeps crashing upon entering a command in the program (ran trough gdb)
[command]pwd
Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION _FAILURE at address: 0x00000000 0x000105bc in parse_commands (command=0xbffe 70e4 "pwd") at /Users/alefveld/Projects/project1/client.c:198 198 while((argv[i++]=strtok(command ,""))) { (gdb) quit
this is the conflicting piece of code. i suspect it's in the ptr to ptr because when working with a normal ptr no problem occurs. i just don't want to make a dozen strtok calls. i would like all the arguments of a single command to be split up in tokens and put nicely in my **argv. do i need to initialize anything? is the char *ptr strtok giving me not correct? do i need to cast it ?
ps *command is a '\0' ended string.
int parse_commands( char *command) { int i=0, argc=0; char **argv={0}; // initialize everything to NULL connection_inde x *tmp=head; // not relevant here
/* Split up command in tokens */ while((argv[i++]=strtok(command ,""))) {
There are at least two problems here.
First, argv is NULL. Since argv[0] is equivalent to
*(argv + 0) which is the same as *argv which is the same
as *NULL, you can't expect anything good to happen. You
are trying to store the value returned by strtok(), but
you have not provided any memory to store it in.
Second, using "" as the second argument to strtok()
is legal, but silly: it will "tokenize" the string using
an empty set of delimiter characters -- and since the
string therefore contains no delimiters, strtok() will
just return the entire thing in one lump.
-- Er*********@sun .com al**@xs4all.nl wrote:
.... snip ... this is the conflicting piece of code. i suspect it's in the ptr to ptr because when working with a normal ptr no problem occurs. i just don't want to make a dozen strtok calls. i would like all the arguments of a single command to be split up in tokens and put nicely in my **argv. do i need to initialize anything? is the char *ptr strtok giving me not correct? do i need to cast it ?
You are allowed to capitalize the first letters of sentences and
the personal pronoun 'I'. This adds to the legibility of your
article. ps *command is a '\0' ended string.
int parse_commands( char *command) { int i=0, argc=0; char **argv={0}; // initialize everything to NULL
so you declared a single pointer and set it to NULL. It is name
argv for some reason, and points (after suitable initialization
only) to storage that holds a pointer to char.
connection_inde x *tmp=head; // not relevant here
/* Split up command in tokens */ while((argv[i++]=strtok(command ,""))) {
argv is still a NULL, pointing nowhere. This is undefined
behavior. By the way, you are also allowed to embed real blanks in
your code. There are no penalties for legibility.
command=NULL; argc++;}
/* A command always exists of 2 or more arguments. */ if(argc<=1) return -1;
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! al**@xs4all.nl wrote: char **argv={0}; // initialize everything to NULL
The braces, and the comment, suggest a confusion between pointers and
arrays... you've made argv a pointer, not an array, and this line is
equivalent to
char **argv = NULL;
Set argv to point to valid memory before dereferencing it.
/* Split up command in tokens */ while ((argv[i++] = strtok(command, ""))) { command = NULL; argc++; }
You don't want an empty delimiter string. That makes the entire
command one token. In other words, strtok(s, ""), when s is not NULL,
simply returns s. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bulba! |
last post by:
OK. I have reworked this program (below) to use just the data
manipulation capabilities of Python (training was largely the
motivation). I've tried to manipulate the data just in Python
and not in typical loops. One thing that may not be entirely
crazy in this, IMHO, is the attempt to use built-in capabilities
of the language as much as possible instead of doing it "manually".
Anyway, Python is the only language I've seen (apart from...
|
by: Leonardo Javier Bel?n |
last post by:
I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.
Thanks in advance. Leonardo.
/**************************************************************************/
#include <stdio.h>
#include <string.h>
|
by: Marco Chiarandini |
last post by:
Dear all,
I am experiencing a problem in the deallocation of STL data
structures.
In paritcular I create a vector of sets and insert integers in each
set. I do not define any deallocator since they should be already part
of the STL. Nevertheless, when I exit the code I have memory leackage.
I checked carefully and the leackage appear related with how many
insertions I do in the set data structures.
|
by: Chumbo |
last post by:
If I have this(please bear with me - this is a case of a former java
guy going back to C ;-)
int main () {
char *tokconvert(char*);
char str = "dot.delimited.str";
char *result;
result = tokconvert(str);
return 0;
|
by: magix |
last post by:
Dear Guru,
I have been thinking hard on how to token based on demiliter after certain
position.
Example, I have list of possible string below, and the the delimiter is "1"
with the rules below
- if the 5th character is "1", then it is delimiter
elseif the 7th character is "1", then it is delimiter
else
this string is not a valid string
| |
by: Vlad Dogaru |
last post by:
Hello,
I am trying to learn C, especially pointers. The following code
attempts to count the appearences of each word in a text file, but
fails invariably with Segmentation Fault. Please help me out, I've
already tried all my ideas. Also, please do comment on my coding style
or other aspects. Thank you.
#include <stdio.h>
#include <string.h>
|
by: Stu Cazzo |
last post by:
Hi all,
I have a question on why strtok is doing what it's doing for my
splitString( string2 ); call.
Below is the output for the entire program:
token was: word1
token was: word2
token was: word3
token was: word1
|
by: stevenruiz |
last post by:
Hello All
My question mainly is how to use/reference Double Pointers? I am
currently trying to understand what the meaning of a 'vector of
pointers' means also? What I am trying to do is take a char array and
break it up into words omitting the spaces. What needs to be noted is
that I am trying to accomplish this only using char ** and char *.
Therefore, I am creating it from scratch. Below is code that I have
written so far:
|
by: jsimps44 |
last post by:
Hi,
I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any help at all would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |