Double Pointers and dynamic memory allocation

This is a discussion on Double Pointers and dynamic memory allocation within the C Programming forums, part of the General Programming Boards category; Hello I am new to C and we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    Double Pointers and dynamic memory allocation

    Hello I am new to C and we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not working. when I compile with commented code (Main() works fine) I get a segmentation fault. Could I get guidance of where I have flukes?

    This is the purpose of getScrabbleWords:
    char **getScrabbleWords(char **allWords, char letters[]): This function takes an array of char* values (i.e. strings) representing all the words read from wordlist.txt. Each of these words is tested by callingcanWeMakeIt as a helper function, and pointers to the words that can be made are put into an array, myWords. Note, copies of the words are not made! In order to indicate the end of myWords, we terminate with a NULL pointer. Thus, if N words can be made from letters then myWords should have length N+1.
    Attached Images Attached Images  

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you sure the error is here? After all, you know you have a NULL pointer hanging around -- if a loop somewhere else goes awry and tries to use that NULL pointer, then that would be the (or a, at least) problem.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    The error has to be there. So I should check to see if I have a null pointer in the loop and from there fix whatever is occurring? Be in a general sense is my looping and structure of my for-loop good?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is an error in that your for loop will not go through your entire allWords array -- i will never go past 1, because sizeof(allWords) and sizeof(allWords[0]) will be the same number, specifically the size of a pointer on your system.

    There is no reason (based on what you've said) to believe that the only error is here. (I did read where you said that it ran when you commented out this code; that's not particularly relevant.)

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Oh i saw that. Thanks well you are saying it might be other areas of the code. Could you exam what I have and lead me in the right direction? I can post pics of the code or could upload the code if you are willing to assist me further.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The function here looks well-formed in that when you get to the end you've got exactly what the specs say you should have -- an array of pointers with the last one being NULL. There don't appear to be any problematic lines here. My instinct is that your segfault is from some other code not respecting the boundaries and trying to follow that NULL pointer.

    Your debugger should be telling you what line caused the segfault (and if you're not using a debugger, now would be an excellent time to start), and that will most likely give you some clues.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Okay Ill try to install a debugger. Thanks Man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation using pointer to pointers
    By mp252 in forum C++ Programming
    Replies: 12
    Last Post: 06-22-2013, 05:34 PM
  2. dynamic memory and double pointers
    By lexitron in forum C Programming
    Replies: 2
    Last Post: 11-10-2011, 01:38 AM
  3. Pointers and Dynamic memory allocation help needed
    By dave_the_bear10 in forum C Programming
    Replies: 3
    Last Post: 11-09-2011, 03:23 PM
  4. Link List,Structures,Dynamic Memory Allocation,Pointers
    By Tanuj_Tanmay in forum C Programming
    Replies: 2
    Last Post: 04-24-2009, 08:55 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 02:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21