Thread: global pointer problem?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    global pointer problem?

    I have a struct defined globally in global.h as follows:

    Code:
    typedef struct entry {
            char *lexeme;           // id name
           struct entry *next;     // pointer to next entry
    } ENTRY;
    Also in global.h, I have:
    Code:
    ENTRY *symPtr=NULL;
    Now in my main file, I have a fn that creates a node and adds it to a linked list:
    Code:
    ENTRY *addEntry(char *str)
    {
        ENTRY *pTmp=symPtr;
        ENTRY *new;
        new=(ENTRY *)malloc(sizeof(ENTRY));
        if(!new) {
            fprintf(stderr, "Could not add a new entry to symbol table\n");
            exit(1);
        }
    
        // Set default values.
        new->lexeme=str; 
        
      if(!pTmp) {
          symPtr=new;
          fprintf(stderr, "added %s\n", str);
      }
      else {
              while(pTmp->next)
                  pTmp=pTmp->next;
          pTmp->next=new;
          fprintf(stderr, "%s added\n",str);
      }
        return new;
    }
    My problem is that symPtr always is pointing to null upon entry to this fn since when I tried to print out the linked list starting from symPtr, it only prints the last node that was added. Can anybody help? Thanks...

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You do not initialise the 'next' pointer, the other code assumes that it is set to NULL:
    Code:
        // Set default values.
        new->lexeme=str;
        new->next = NULL;  // <-- add this line
    ... and now it works (on my machine at least and within my
    understanding of what you want it to do )
    DavT
    -----------------------------------------------

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    im not sure but if that was the prob
    would not ...

    while( pTmp->next ) {
    pTmp = pTmp->next;
    }

    cause a segmentation fault?

    the NULL should be set but my compiler (gcc) seems to do it automaticly, and the original code posted runs perfectly on my computer?
    Last edited by Kinasz; 11-25-2003 at 04:51 AM.
    "Assumptions are the mother of all **** ups!"

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you shouldn't declare variables in a header file. I don't know if that's your problem but it could cause you not to work if you use that header file in more than one module
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    >would not ... <snip> cause a segmentation fault?

    It did under my compiler (MSVCv6). I do not have gcc here to
    test what that does. K&R states that malloc() does not initialise
    the memory that it allocates - calloc() does. Any other behaviour
    (e.g. initialising memory to zero) is compiler specific. The fact that
    this code happens to work when compiled with gcc is actually
    bad marks for gcc since it encourages non-portable code. For
    once (!) MSVC does the sensible thing and crashes out in a very
    obvious manner.

    If the question was how to print out the whole structure every
    time then this should work:
    Code:
    ENTRY *addEntry(char *str)
    {
      ENTRY *pTmp=symPtr;
      ENTRY *new;
      new=(ENTRY *)malloc(sizeof(ENTRY));
      if(!new) {
        fprintf(stderr, "Could not add a new entry to symbol table\n");
        exit(1);
      }
    
      // Set default values.
      new->lexeme=str;
      new->next = NULL;
      
      if(!pTmp) {
        symPtr=new;
        fprintf(stderr, "added %s\n", str);
      }
      else {
        while(pTmp->next)
          pTmp=pTmp->next;
        pTmp->next=new;
        fprintf(stderr, "%s added\n",str);
      }
    
      // print out entire list
      fprintf(stderr, "list now contains:\n");
      for (pTmp = symPtr; pTmp != NULL; pTmp=pTmp->next)
        fprintf(stderr, "%s\n",pTmp->lexeme);
    
      return new;
    }
    DavT
    -----------------------------------------------

  6. #6
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    if the error being received on Lilgirls comp was not a segmentation fault then her compiler may be doing the same thing... whether it should or shouldnt be doing so.

    it only prints the last node that was added
    There is probably another error elsewhere in the program that is causing this fault!
    Last edited by Kinasz; 11-25-2003 at 06:18 AM.
    "Assumptions are the mother of all **** ups!"

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    new->lexeme=str;
    I think this is your problem. You are assigning a pointer. Next time you read a name this value (lexeme) will also change because they are both pointing to the same menory area. You need to copy the value to lexeme (allocate memory and copy) to get it working. For example use strdup (don't forget to free memory when deleting the node).

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Monster
    Code:
    new->lexeme=str;
    I think this is your problem. You are assigning a pointer. Next time you read a name this value (lexeme) will also change because they are both pointing to the same menory area. You need to copy the value to lexeme (allocate memory and copy) to get it working. For example use strdup (don't forget to free memory when deleting the node).
    Entirely possible. However, if 'str' is a string allocated elsewhare, this works fine. Also, if 'str' is a string literal, this will also work just fine.
    Code:
    addEntry( "This" );
    addEntry( "works" );
    addEntry( "just" );
    addEntry( "fine" );
    addEntry( "." );
    addEntry( "No" );
    addEntry( "allocation" );
    addEntry( "needed" );
    addEntry( "." );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  3. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  4. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM