Problem with realloc in the following function

This is a discussion on Problem with realloc in the following function within the C Programming forums, part of the General Programming Boards category; I'm trying to write a function that doubles the size of an array of strings. The function also initializes all ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Problem with realloc in the following function

    I'm trying to write a function that doubles the size of an array of strings. The function also initializes all the new elements in the array to 100 char strings. However, the function doesnt work at all. I was wondering if someone could tell me what is wrong with it. Thanks.

    Code:
    char **doubleStringArray(char **stringArray, int* size){
    
    	int i;
    	stringArray = (char**) realloc(stringArray, 2*(*size));
    	for (i = (*size); i < 2*(*size); i++){
    		stringArray[i] = (char*) malloc(100);
    	}
    	*size = 2 * (*size);
    
    	return stringArray;
    
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    There is nothing functionally wrong with it. You are probably using it wrong.

    gg

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > stringArray = (char**) realloc(stringArray, 2*(*size));
    1. lose the casts - there is no need to cast memory allocation functions in C. At worst, it results in incorrect code because you hide your failure to include stdlib.h, and hence prototype the function properly.

    2. the size of 2 is a big assumption about the size of pointers
    It should be
    sizeof(char*) * (*size )

    3. If realloc fails, you lose your old allocation as well
    Do it like so
    Code:
    char **t = realloc ( stringArray, sizeof(char*) * (*size) );
    if ( t != NULL ) {
        stringArray = t;
    } else {
        // realloc failed, but stringArray is still usable
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    There is nothing functionally wrong with it.
    And by that I meant it compiles......yeah, that's the ticket.

    gg

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    I multiply by 2 because I am doubling the size of the array, thats why I do that

    And it still doesnt work after doing all that you said

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > I multiply by 2 because I am doubling the size of the array
    Then it's
    2 * (*size) * sizeof(char*)

    red is how many you want
    blue is how big each one is
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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