crazy problem with strtok puttings ptrs in **argv

A

alef

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=0xbffe70e4 "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_index *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
 
E

Eric Sosman

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=0xbffe70e4 "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_index *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.
 
C

CBFalconer

.... 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_index *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.
 
S

Steven Kobes

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top