crazy problem with strtok puttings ptrs in **argv

Discussion in 'C Programming' started by alef@xs4all.nl, Jul 26, 2005.

  1. Guest

    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
    , Jul 26, 2005
    #1
    1. Advertising

  2. Eric Sosman Guest

    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=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.

    --
    Eric Sosman, Jul 26, 2005
    #2
    1. Advertising

  3. CBFalconer Guest

    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_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.

    > command=NULL;
    > argc++;}
    >
    > /* A command always exists of 2 or more arguments. */
    > if(argc<=1) return -1;



    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jul 26, 2005
    #3
  4. Steven Kobes Guest

    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.
    Steven Kobes, Jul 27, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,574
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,942
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    898
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    662
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page