-2

So I have been getting this error every time, I cant seem to figure out where to solve it.

This is where it always returns null:

    wordData * ptr;
    ptr = (wordData*)malloc(sizeof(wordData)); //returns null every time
    if(NULL == ptr)
    {
       printf("\n Node creation failed in add list\n");
       return NULL;
    }

This is my struct:

    typedef struct _wordData
    {
        int iWordID;
        char * cWord;
        struct _wordData *next;

     } wordData;

How ever when I create the headnode in my list earlier on the exact same code works! EDIT: elaboration:

    printf("\n creating list with headnode as [%d] %s\n",iWordID,cWord);
    wordData *ptr;
    ptr = (wordData*)malloc(sizeof(wordData));
    if(NULL == ptr)
    {
       printf("\n Node creation failed in create list \n");
       return NULL;
    }

So the above code actually creates my headnode.

EDIT: on memory: I have loads of memory available :)

EDIT: more code:

    void clearStr() // adding word into list with a struct containing word,wordID
    {
      add_to_list(iID,cStr,true);
      memset(cStr, '\0', sizeof(cStr) );
      iCounter = 0;
    }


    wordData* add_to_list(int iWordID, char * cWord, bool add_to_end)
    {
      char temp[strlen(cWord)+1];
      strcpy(temp, cWord);
      if(NULL == head)
      {
          return (create_list(iWordID, temp));
      } 

      if(add_to_end)
      printf("\n Adding node to end of list with iWordID [%d] %s\n",iWordID, cWord);
      else
      printf("\n Adding node to beginning of list with iWordID [%d]\n",iWordID);

      int sizeWordData = sizeof(wordData);
      printf("Size wordData is: %d",sizeWordData); //12 every time

      wordData * ptr;
      ptr = (wordData*)malloc(sizeof(wordData)); //waarom is dit null?
      if(NULL == ptr)
      {
         printf("\n Node creation failed in add list\n");
         return NULL;
      }

      ptr->iWordID = iWordID;
      ptr -> cWord,temp;
      strcpy(ptr -> cWord, temp);
      ptr->next = NULL;

      if(add_to_end)
      {
        curr->next = ptr;
        curr = ptr;
        printf("pointers geset");
      }
      else
      {
        ptr->next = head;
        head = ptr;
      }
      return ptr;
      }

I have searched everything but none of the give solutions have helped me, so help would be appreciated!

flexzican
  • 97
  • 1
  • 12

3 Answers3

3

You actually have destroyed the heap with buffer overruns earlier.

strcpy(ptr -> cWord, temp);//without allocating anything to cWord

This WILL cause malloc to misbehave. Do:

ptr -> cWord = malloc(strlen(temp) +1);//allocate
strcpy(ptr -> cWord, temp);//then copy
Agent_L
  • 4,960
  • 28
  • 30
2

There is not enough information in your question to answer it properly, I'm afraid.

This:

ptr = (wordData*)malloc(sizeof(wordData)); //returns null every time

is better written as:

ptr = malloc(sizeof *ptr);

It's shorter, has less repetition, removes the cast, and generally is more full of win.

However, since you say the same code works earlier in the program flow, it's likely that your code is doing the right thing here anyway.

How many nodes do you allocate? Could it be that some recursion is getting out of hand, allocating millions of nodes and exhausting your memory?

Also note that your title is contradicted by your text, making this question even more confusing.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
0

From the man page for malloc() there are two times when malloc() can return NULL:

  • If size is 0 it can return NULL
  • If the request for memory failed

You said How ever when I create the headnode in my list earlier on the exact same code works so that means something is missing from your example which could contain the problem:

ptr = (wordData*)malloc(sizeof(wordData)); 
                             ^
                         this should never be 0, so that shouldn't be the problem

Are there any loops or recursive calls in your program? How many times do you make the allocation?

EDIT:
You say you're just reading from a file, so try this:

main()
  open file
  loop for length of file
     allocate your memory
  free memory

If this minimal code example fails post the code and a sample of your text file, if it works, then there's something else in your larger program that's causing the problem, you can "build back up" from there and probably find it.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • the size is 12, im doing this for each word i read from a txt file. the file contains not more than 20 words – flexzican Nov 30 '12 at 12:12
  • @flexzican - Is there a more complete (minimal) code sample you can share that shows the problem? We can run it and see if the results are the same? See the **edit** in my post for an example of how to start solving this issue. – Mike Nov 30 '12 at 12:16