Program repeats itself, pointer trouble I suspect.

Discussion in 'C Programming' started by Neil, Feb 15, 2007.

  1. Neil

    Neil Guest

    Hello all!


    I wrote program with a array of pointers, and I suspect they are
    pointing at each other in the Do ...While loop.
    Something is messed up with the increment variable word. A program
    clip of what I'm talking about.

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char string[50] = {"Have a nice day folks"};
    char *line_ptr;
    char *list[20] = { '\0' }; //Initialize the array of pointers to
    NULL.
    int word = 0;

    line_ptr = strtok(string, " ");

    do
    {
    list[word] = line_ptr;
    word++;
    line_ptr = strtok(NULL," ");
    } while (line_ptr != NULL);

    return 0;
    }
    It's kinda weird, cause the program repeats itself when I run it. I
    left out the output section, cause I know it works fine. In the
    debugger it works OK. Is there a memory leak? Do you need to assign
    pointers to NULL after you use them? Should I free up the memory from
    the array of pointers before the program terminates? What is the best
    way handle pointers after your done with them?

    Thanks for all your help
    Neil
    Neil, Feb 15, 2007
    #1
    1. Advertising

  2. Neil

    Old Wolf Guest

    On Feb 15, 1:50 pm, "Neil" <> wrote:
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char string[50] = {"Have a nice day folks"};
    > char *line_ptr;
    > char *list[20] = { '\0' };


    { 0 } does the same thing and makes a bit more sense.

    > int word = 0;
    >
    > line_ptr = strtok(string, " ");
    >
    > do
    > {
    > list[word] = line_ptr;
    > word++;
    > line_ptr = strtok(NULL," ");
    > } while (line_ptr != NULL);
    >
    > return 0;}
    >
    > It's kinda weird, cause the program repeats itself when I run it.


    This program is fine. What do you mean by 'repeats itself' ?
    This program generates no output.

    > In the debugger it works OK. Is there a memory leak?


    No

    >Do you need to assign pointers to NULL after you use them?


    No

    > Should I free up the memory from the array of pointers before the
    > program terminates?


    No

    > What is the best way handle pointers after your done with them?


    Take no special action.

    > I left out the output section, cause I know it works fine.


    Apparently not...
    Old Wolf, Feb 15, 2007
    #2
    1. Advertising

  3. On 14 Feb 2007 16:50:11 -0800, "Neil" <> wrote:

    >Hello all!
    >
    >
    >I wrote program with a array of pointers, and I suspect they are
    >pointing at each other in the Do ...While loop.
    >Something is messed up with the increment variable word. A program
    >clip of what I'm talking about.
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >int main(void)
    >{
    >char string[50] = {"Have a nice day folks"};
    >char *line_ptr;
    >char *list[20] = { '\0' }; //Initialize the array of pointers to
    >NULL.


    This is why you should not use // style comments in usenet.

    If you want assign each of the 20 pointer in the array the NULL value,
    use NULL. While 0 and '\0' will both work, they are visually
    misleading. Someone might be tempted to think that the pointers point
    to a char containing '\0'.

    >int word = 0;
    >
    >line_ptr = strtok(string, " ");
    >
    >do
    > {
    > list[word] = line_ptr;
    > word++;
    > line_ptr = strtok(NULL," ");
    > } while (line_ptr != NULL);
    >
    >return 0;
    >}
    >It's kinda weird, cause the program repeats itself when I run it. I
    >left out the output section, cause I know it works fine. In the


    Define repeat.

    The archives are full of messages where the error was in the section
    omitted by the poster because "it works." Post a compilable example
    that demonstrates the behavior in question and let us help you find
    the problem.

    >debugger it works OK. Is there a memory leak? Do you need to assign


    You cannot have a memory leak without dynamic allocation.

    >pointers to NULL after you use them? Should I free up the memory from


    Unless you test a pointer for NULL, you never need to reset it to
    NULL.

    >the array of pointers before the program terminates? What is the best


    Any attempt to free memory that you did not allocate will invoke
    undefined behavior.

    >way handle pointers after your done with them?


    The same way you handle an object of any other type when you are done
    with it. In most cases, it is sufficient to simply not use it in any
    subsequent code.


    Remove del for email
    Barry Schwarz, Feb 15, 2007
    #3
  4. Neil

    Beej Guest

    On Feb 14, 4:50 pm, "Neil" <> wrote:
    > line_ptr = strtok(string, " ");


    Unless you're sure you're always going to have a token, you should
    probably check line_ptr for NULL here, too.

    Here's a compact form of that, if you don't mind assignments in your
    expressions:

    if ((p = strtok(string, " ")) != NULL) {
    do {
    printf("Token: %s\n", p);
    } while ((p = strtok(NULL, " ")) != NULL);
    }

    > do
    > {
    > list[word] = line_ptr;
    > word++;
    > line_ptr = strtok(NULL," ");
    > } while (line_ptr != NULL);


    Here is my output when I ran it.

    'Have'
    'a'
    'nice'
    'day'
    'folks'

    Looks fine to me. What's your output?

    -Beej
    Beej, Feb 15, 2007
    #4
  5. Neil wrote:

    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char string[50] = {"Have a nice day folks"};
    > char *line_ptr;
    > char *list[20] = { '\0' };  //Initialize the array of pointers to


    Cleaner version:

    #include <stdio.h>
    #include <string.h>

    enum constants {
    STRING_MAX = 50,
    WORD_MAX = 20
    };

    int main(void)
    {
    char string[STRING_MAX] = "Have a nice day folks";
    char *word[WORD_MAX];
    int i;

    for (i = 0; i < WORD_MAX; i++) {
    if ((word = strtok(i ? NULL : string, " ")) == NULL)
    break;
    fprintf(stdout, "word[%d] = %s\n", i, word);
    }

    return 0;
    }
    Christopher Layne, Feb 15, 2007
    #5
  6. Old Wolf said:

    > On Feb 15, 1:50 pm, "Neil" <> wrote:
    >>
    >> do
    >> {
    >> list[word] = line_ptr;
    >> word++;
    >> line_ptr = strtok(NULL," ");
    >> } while (line_ptr != NULL);
    >>
    >> return 0;}
    >>
    >> It's kinda weird, cause the program repeats itself when I run it.

    >
    > This program is fine.


    You think so? I don't think you read it carefully enough.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Feb 15, 2007
    #6
  7. "Neil" <> writes:

    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > char string[50] = {"Have a nice day folks"};
    > char *line_ptr;
    > char *list[20] = { '\0' }; /* Initialize the array of pointers to NULL.*/
    > int word = 0;
    >
    > line_ptr = strtok(string, " ");
    >
    > do
    > {
    > list[word] = line_ptr; /* MARK */
    > word++;
    > line_ptr = strtok(NULL," ");
    > } while (line_ptr != NULL);


    Other than and hint in a reply to another reply, no one has pointed
    out that you are in danger of accessing outside the bounds of your
    array "list".

    If it reasonable (is it ever?) to simply discard tokens that don't
    fit, you can write:

    if (word < 20) list[word] = line_ptr;

    where I put /* MARK */ in your code.

    I don't know of this is the source of your problem, because the
    description is rather vague.

    --
    Ben.
    Ben Bacarisse, Feb 15, 2007
    #7
  8. Neil

    Old Wolf Guest

    On Feb 15, 7:19 pm, Richard Heathfield <> wrote:
    > Old Wolf said:
    > > On Feb 15, 1:50 pm, "Neil" <> wrote:
    > >> do
    > >> {
    > >> list[word] = line_ptr;
    > >> word++;
    > >> line_ptr = strtok(NULL," ");
    > >> } while (line_ptr != NULL);

    >
    > >> return 0;}

    >
    > >> It's kinda weird, cause the program repeats itself when I run it.

    >
    > > This program is fine.

    >
    > You think so? I don't think you read it carefully enough.


    By 'fine' I mean that the OP's code doesn't contain any bugs.
    Of course I would not pedantic about the use of C99 features,
    or bugs introduced by wordwrapping during the posting process.

    Are you perhaps referring to the fact that the program might
    break if its source is modified to introduce a bug, as
    suggested by Ben Bacarisse?
    If not, then please be more explicit.
    Old Wolf, Feb 15, 2007
    #8
  9. "Old Wolf" <> writes:

    > Are you perhaps referring to the fact that the program might
    > break if its source is modified to introduce a bug, as
    > suggested by Ben Bacarisse?


    I'm not seeing it. Can you post a correction to my message (or
    explain it to me and I'll post a correction)?

    --
    Ben.
    Ben Bacarisse, Feb 15, 2007
    #9
  10. Neil

    Old Wolf Guest

    On Feb 16, 12:46 pm, Ben Bacarisse <> wrote:
    > "Old Wolf" <> writes:
    > > Are you perhaps referring to the fact that the program might
    > > break if its source is modified to introduce a bug, as
    > > suggested by Ben Bacarisse?

    >
    > I'm not seeing it. Can you post a correction to my message (or
    > explain it to me and I'll post a correction)?


    Your message appears to be saying that the program could
    break if the input string were modified to have more than
    20 words in it, which would be a bug. But the original
    post only had 4 words in the string, so there is no problem.

    (Of course it is not a bad idea to add in checking, as
    you suggested).
    Old Wolf, Feb 16, 2007
    #10
  11. Neil

    Neil Guest

    On Feb 14, 7:24 pm, Barry Schwarz <> wrote:
    > On 14 Feb 2007 16:50:11 -0800, "Neil" <> wrote:
    >
    >
    >
    >
    >
    > >Hello all!

    >
    > >I wrote program with a array of pointers, and I suspect they are
    > >pointing at each other in the Do ...While loop.
    > >Something is messed up with the increment variable word. A program
    > >clip of what I'm talking about.

    >
    > >#include <stdio.h>
    > >#include <string.h>

    >
    > >int main(void)
    > >{
    > >char string[50] = {"Have a nice day folks"};
    > >char *line_ptr;
    > >char *list[20] = { '\0' }; //Initialize the array of pointers to
    > >NULL.

    >
    > This is why you should not use // style comments in usenet.
    >
    > If you want assign each of the 20 pointer in the array the NULL value,
    > use NULL. While 0 and '\0' will both work, they are visually
    > misleading. Someone might be tempted to think that the pointers point
    > to a char containing '\0'.
    >
    > >int word = 0;

    >
    > >line_ptr = strtok(string, " ");

    >
    > >do
    > > {
    > > list[word] = line_ptr;
    > > word++;
    > > line_ptr = strtok(NULL," ");
    > > } while (line_ptr != NULL);

    >
    > >return 0;
    > >}
    > >It's kinda weird, cause the program repeats itself when I run it. I
    > >left out the output section, cause I know it works fine. In the

    >
    > Define repeat.


    It starts again from main(), There must be problem elsewhere.
    -Neil
    Neil, Feb 16, 2007
    #11
  12. Neil

    Neil Guest

    On Feb 15, 7:56 am, Ben Bacarisse <> wrote:
    > "Neil" <> writes:
    > > #include <stdio.h>
    > > #include <string.h>

    >
    > > int main(void)
    > > {
    > > char string[50] = {"Have a nice day folks"};
    > > char *line_ptr;
    > > char *list[20] = { '\0' }; /* Initialize the array of pointers to NULL.*/
    > > int word = 0;

    >
    > > line_ptr = strtok(string, " ");

    >
    > > do
    > > {
    > > list[word] = line_ptr; /* MARK */
    > > word++;
    > > line_ptr = strtok(NULL," ");
    > > } while (line_ptr != NULL);

    >
    > Other than and hint in a reply to another reply, no one has pointed
    > out that you are in danger of accessing outside the bounds of your
    > array "list".
    >
    > If it reasonable (is it ever?) to simply discard tokens that don't
    > fit, you can write:
    >
    > if (word < 20) list[word] = line_ptr;
    >
    > where I put /* MARK */ in your code.
    >
    > I don't know of this is the source of your problem, because the
    > description is rather vague.
    >
    > --
    > Ben.- Hide quoted text -
    >
    > - Show quoted text -


    Ben your probably right, but I didn't want to go through whole
    program, just the code clip I
    mentioned. Thanks for the help, I'll try that....

    -Neil
    Neil, Feb 16, 2007
    #12
  13. Neil

    Neil Guest

    On Feb 14, 8:45 pm, "Beej" <> wrote:
    > On Feb 14, 4:50 pm, "Neil" <> wrote:
    >
    > > line_ptr = strtok(string, " ");

    >
    > Unless you're sure you're always going to have a token, you should
    > probably check line_ptr for NULL here, too.
    >
    > Here's a compact form of that, if you don't mind assignments in your
    > expressions:
    >
    > if ((p = strtok(string, " ")) != NULL) {
    > do {
    > printf("Token: %s\n", p);
    > } while ((p = strtok(NULL, " ")) != NULL);
    >
    > }
    > > do
    > > {
    > > list[word] = line_ptr;
    > > word++;
    > > line_ptr = strtok(NULL," ");
    > > } while (line_ptr != NULL);

    >
    > Here is my output when I ran it.
    >
    > 'Have'
    > 'a'
    > 'nice'
    > 'day'
    > 'folks'
    >
    > Looks fine to me. What's your output?
    >
    > -Beej


    Beej the output section is a function that I left off, however the
    program does begin
    at the start, or the program crashes, from what I tried recently.
    I'm trying to isolate the problem. thanks.
    -Neil.
    Neil, Feb 16, 2007
    #13
  14. "Old Wolf" <> writes:

    > On Feb 16, 12:46 pm, Ben Bacarisse <> wrote:
    >> "Old Wolf" <> writes:
    >> > Are you perhaps referring to the fact that the program might
    >> > break if its source is modified to introduce a bug, as
    >> > suggested by Ben Bacarisse?

    >>
    >> I'm not seeing it. Can you post a correction to my message (or
    >> explain it to me and I'll post a correction)?

    >
    > Your message appears to be saying that the program could
    > break if the input string were modified to have more than
    > 20 words in it, which would be a bug. But the original
    > post only had 4 words in the string, so there is no problem.


    Ah, OK. Crossed wires.

    > (Of course it is not a bad idea to add in checking, as
    > you suggested).


    ....but probably not in any way a fix for the OP's problem if it failed
    with a four token string.

    --
    Ben.
    Ben Bacarisse, Feb 16, 2007
    #14
  15. Neil

    Neil Guest

    On Feb 14, 7:24 pm, Barry Schwarz <> wrote:
    This is why you should not use // style comments in usenet.


    Huh?....Didn't know that.

    Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
    this program, it's crashed a couple of times writing I what I think
    what looks like elementry code.

    If you don't intialize a pointer, but you declared it main() and just
    leave it.
    Does this present a problem after the program terminates?

    Can two char pointers in an array of pointers point to each other
    after the program terminates?

    Thanks for tip..

    -Neil
    Neil, Feb 16, 2007
    #15
  16. Neil

    Neil Guest

    On Feb 15, 7:18 pm, "Neil" <> wrote:
    > On Feb 14, 7:24 pm, Barry Schwarz <> wrote:
    > This is why you should not use // style comments in usenet.
    >
    > Huh?....Didn't know that.
    >
    > Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
    > this program, it's crashed a couple of times writing I what I think
    > what looks like elementry code.
    >
    > If you don't intialize a pointer, but you declared it main() and just
    > leave it.


    I ment inside main() :)

    -Neil
    Neil, Feb 16, 2007
    #16
  17. Neil

    Neil Guest

    On Feb 14, 9:00 pm, Christopher Layne <> wrote:
    > Neil wrote:
    > > #include <stdio.h>
    > > #include <string.h>

    >
    > > int main(void)
    > > {
    > > char string[50] = {"Have a nice day folks"};
    > > char *line_ptr;
    > > char *list[20] = { '\0' }; //Initialize the array of pointers to

    >
    > Cleaner version:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > enum constants {
    > STRING_MAX = 50,
    > WORD_MAX = 20
    >
    > };
    >
    > int main(void)
    > {
    > char string[STRING_MAX] = "Have a nice day folks";
    > char *word[WORD_MAX];
    > int i;
    >
    > for (i = 0; i < WORD_MAX; i++) {
    > if ((word = strtok(i ? NULL : string, " ")) == NULL)
    > break;
    > fprintf(stdout, "word[%d] = %s\n", i, word);
    > }
    >
    > return 0;


    Huh? ........Better then mine..........but OK.
    I'll try that.
    Thanks Chris..
    -Neil
    Neil, Feb 16, 2007
    #17
  18. On 15 Feb 2007 18:18:01 -0800, "Neil" <> wrote:

    >On Feb 14, 7:24 pm, Barry Schwarz <> wrote:
    >This is why you should not use // style comments in usenet.
    >
    >
    >Huh?....Didn't know that.
    >
    >Barry, I'm using an old Borland Turbo C++ for DOS Compiler to write
    >this program, it's crashed a couple of times writing I what I think
    >what looks like elementry code.


    Post a compilable example of the code that exhibits the behavior.

    >
    >If you don't intialize a pointer, but you declared it main() and just
    >leave it.
    >Does this present a problem after the program terminates?


    Unless you attempt to evaluate its value, the fact that a pointer is
    uninitialized (technically called indeterminate) cannot be a problem.

    >
    >Can two char pointers in an array of pointers point to each other
    >after the program terminates?
    >


    Unless you have a very strange operating system, after your program
    terminates none of your objects exist. Since they no longer exist,
    one of the many things they don't do is point anywhere, let alone to
    each other.

    While your program is running, it is still not possible for one char
    pointer to point to another. By definition, a char pointer must point
    to a char (if it points anywhere). It makes no difference whether the
    pointers are in an array or not.


    Remove del for email
    Barry Schwarz, Feb 16, 2007
    #18
  19. Neil

    Neil Guest

    On Feb 15, 9:28 pm, Barry Schwarz <> wrote:
    Post a compilable example of the code that exhibits the behavior.

    Barry, I would if I knew where the problem is in the program..
    Obviouly the pointers are not causing the problem.

    Thanks for all your help
    -Neil
    Neil, Feb 16, 2007
    #19
  20. Neil said:

    > On Feb 15, 9:28 pm, Barry Schwarz <> wrote:
    > Post a compilable example of the code that exhibits the behavior.
    >
    > Barry, I would if I knew where the problem is in the program..


    If you knew where the problem was, you wouldn't need to ask about it at
    all. Since you don't know where the problem was, it makes sense to post
    the whole program.

    > Obviouly the pointers are not causing the problem.


    But you just said you don't know where the problem is.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Feb 16, 2007
    #20
    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. GO
    Replies:
    4
    Views:
    513
  2. kj12345

    Refresh repeats last action

    kj12345, Sep 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    502
    Eliyahu Goldin
    Sep 29, 2004
  3. Mick
    Replies:
    1
    Views:
    703
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 29, 2004
  4. Amelyan
    Replies:
    2
    Views:
    3,999
    clintonG
    Dec 19, 2005
  5. cate
    Replies:
    1
    Views:
    203
    Evertjan.
    Jun 14, 2010
Loading...

Share This Page