-1

I'm implementing a trie for pset6. load works properly as far as I can tell. (gdb, valgrind both fine with it, and the #of words loaded comes out right as well). check, however, generates a segmentation fault at the line:

if (current->child[i] == NULL)

Now, I've initialized node* root and node* current globally (to 0), as well as the node structure, and then within load (which calls before check) I malloc them. When the program gets to check, root (and current) don't seem to have values in them anymore. I thought the point of declaring them globally was so the values would persist and not disappear. How can I get the values for root to persist into the check function?

4
  • I assume you assign the values of root and current inside load. Do they by any chance get assigned to NULL before the function returns? If no, you may update your question with the pieces of load in which you assign the values of root and current!
    – kzidane
    Dec 22 '14 at 7:45
  • Yes thanks Kareem I'm about to do that-- can't seem to get this right and have been poring over the section code to figure it out... Dec 22 '14 at 8:24
  • When you finally figure that out, I recommend you update your question with the necessary information, post your own answer and accept it so that the question is still valuable.
    – kzidane
    Dec 22 '14 at 8:36
  • Sorry @AndrewVenezia about my failed "answer". I must have been asleep and read you question halfway;) If you declared those pointers globally (as in outside main()) they should be available for you to allocate them anywhere. You shouldn't have issues, provided you do allocate them before you try to access them (which you seem to be doing). If you get stuck don't hesitate to update your question with more detail about your code and I'll try to give you a (really useful, for a change;) hand.
    – abelinux
    Dec 22 '14 at 14:55
0

Oi!

Okay-- so I was re-initializing the pointers inside the function-- I had originally been declaring them there before I realized I had to make them global, and when I did that I did not un-initialize them within the function. That solved that problem!

Thanks for your help!

1
  • since this actually solved your problem, please mark this answer as "accepted"
    – abelinux
    Dec 27 '14 at 16:19
-1

It turns out that, as with any other variable, when you pass a pointer as an argument to a function, what you're really passing in is a copy of such pointer variable. And as with any other variable, it contains the same value as the original pointer.

So, most of the times, it seems to be the same as passing in the pointer itself to the function, since both (the original and the passed in copy) point to the same memory address.

But in this particular case, where you're allocating memory for a particular pointer inside the scope of a function, what you're really doing is allocate memory for the copy of the pointer.

Of course, once you return to main() frame, such copied pointer is lost, and with it, all access to the allocated memory.

You have two choices (that I can think of now;)

  • Either you declare, malloc and free memory for a given pointer, all within the same frame (e.g.: main())

  • If such simpler option is not possible, you can, more cryptically, pass to the function a pointer to the pointer (a.k.a.: "a double pointer") as an argument. Like:

    int* pInt;
    
    foo(&pInt);
    
    free(pInt);
    

    where:

    foo(int** dpInt)
    {
        *dpInt = (int*) malloc(20);
    }
    

HTH!

2
  • I don't think he explicitly stated that he passed these pointers to load. It's actually not allowed in terms of the pset specification since you're not allowed to change the signature of load.
    – kzidane
    Dec 22 '14 at 7:40
  • Indeed @Kareem , I must have been asleep =P. Not really sure whether to edit my "answer", or simply delete it. It's a pitty, cause I feel it's a nice answer... only for a different question ;)
    – abelinux
    Dec 22 '14 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .