reallocing a char** doesn't seem to do anything

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sydneytroz
    New Member
    • May 2007
    • 7

    reallocing a char** doesn't seem to do anything

    Hi

    I'm sure the solution to the problem I'm having is really simple, but I can't seem to see it. I'm using C, and have a char** which I'm trying to realloc to twice it's original size. I'm using the xrealloc function described in Libc's info page. However, after calling xrealloc(), the size of the array hasn't changed -- I know this because when stepping through with gdb and printing the values of the elements of the array (which is only 3 elements long), when accessing the 4th element I get an <Address 0x?????? out of bounds> before and after the realloc.

    The really weird thing here is that the code in question worked fine all of yesterday and today until 5 minutes ago. The only thing I changed was I added a function to remove duplicates from the array, but I know that function works fine, I've tested it.

    xreallox():
    Code:
    void *
    xrealloc (void *ptr, size_t size) {
    	register void *value = realloc(ptr, size);
    	if (value == 0)
    		abort(); /*fatal("virtual memory exhausted");*/
    	return value;
    }
    calling code:
    Code:
    retn = (char **)xrealloc(retn, 2 * *num_mutations * sizeof(char *));
    where retn is the array and num_mutations is a pointer to the size of the array.

    I know this seems kinda vague, but if anyone has any suggestions/questions I'd be appreciative/happy to answer.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Err...
    Originally posted by sydneytroz
    retn = (char **)xrealloc(ret n, 2 * *num_mutations * sizeof(char *));
    Isn't the size of a char array the number of element * sizeof(char) and not sizeof (char*) ??

    And what's with the char** ?? If you have an array of 3 char, you have a char*.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by weaknessforcats
      Isn't the size of a char array the number of element * sizeof(char) and not sizeof (char*) ??

      And what's with the char** ?? If you have an array of 3 char, you have a char*.
      You have assumed array of 3 char, sydneytroz has not said that anywhere. If he is using an array of 3 char * they the types in the xrealloc function are correct.

      On the other hand sydneytroz if you are using an array of char * then those pointers need allocating too, when you realloc retn do you also allocate data for the new pointers to point to?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Banfa
        You have assumed array of 3 char,
        I did. I did. Thank you for pointing that out.

        Comment

        Working...