need help with implementing realloc

This is a discussion on need help with implementing realloc within the C Programming forums, part of the General Programming Boards category; Hello all, I currently am reading input from the command line. everything entered needs to be then put into a ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    Question need help with implementing realloc

    Hello all,

    I currently am reading input from the command line.
    everything entered needs to be then put into a 2 dimensional array.
    using strtok to separate each item of the string by a " " (space) char.

    i never know how many items the array will need to hold, or how big each element will be, they will always be char however.

    at the mo i am using malloc to just assign enough space for the whole string entered.

    but i keep getting this error

    Code:
    malloc: *** error for object 0x100101d18: incorrect checksum for freed object - object was probably modified after being freed.
    *** set a breakpoint in malloc_error_break to debug
    Abort trap

    any ideas to get me thinking?

    thanks for reading this far!

    techevo

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Do you have any code to show us? The error means what it says, you probably tried to access memory that had already been free'd.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    surely if memory has been freed i can use it again? :S

    here is the code

    Code:
                    array = malloc(sizeof(string));	
    		array[0] = strtok(string," ");		
    			
    		for(i=1; i<= word_count-1;i++)
    		{
    			
    			array[i] = strtok(NULL, " ");
    		}

    (word count counts the number of spaces there are in a string)


    techevo

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
                    array = malloc(sizeof(string));
    This is surely not right. How is string declared?

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    char *string;

    then use readline to take input from the command line storing it into string.

    techevo

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Edit: you need to add a cast before malloc. array = (char *) malloc(sizeof(string));

    Malloc returns a void pointer.
    Last edited by Tool; 02-28-2010 at 04:10 PM.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    ok i understand that

    how can i assign memory to array dynamically though?

    or am i misunderstanding something else? :S

    techvo

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    ok i understand that

    how can i assign memory to array dynamically though?

    or am i misunderstanding something else? :S

    techvo

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Please show me the whole code.

    It seems like youre trying to allocate space for an allready statically allocated array. You cant do that.

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by Tool View Post
    Edit: you need to add a cast before malloc. array = (char *) malloc(sizeof(string));

    Malloc returns a void pointer.
    You should never cast the return of malloc, all it will do is hide the fact that you haven't included stdlib.h in your program

    If string is defined as char * string; that means sizeof(string) will likely be 4 or whatever the size of a pointer to char is on your system.

    You need to make string big enough to fit whatever data you're putting in, ie if string was:
    Code:
    char * string = "some words here and some words";
    your malloc would then need to allocate enough memory to store 6 pointers each of varying sizes, or just find the largest word and allocate 6 (6 words) char arrays each of the maximum size little bit more difficult than you thought, eh?
    Last edited by nonpuz; 02-28-2010 at 04:18 PM.

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    So if we were to go with your earlier code, it would look something like this:
    Code:
                    char * temp = NULL;
                    char ** array = NULL;
    ...
    ...
                    array = malloc(word_count);	/* word_count should be number of pointers to char we need in array */
    		temp = strtok(string," ");		
    		array[0] = malloc(strlen(temp) + 1);	
                    strcpy(array[0],temp);
    		for(i=1; i<= word_count-1;i++)
    		{
                            temp = strtrok(NULL," ");
    			array[i] = malloc(strlen(temp) + 1);
                            strcpy(array[i],temp)
    		}
    something like that with lots more error checking, this is a total minimalist almost pseudo code, as you can see from the work thats being done (this without ANY error checking) this is very sloppy approach.

    One other thing, it's usually not a good idea to call strtok on a string defined as char * string = "blah blah blah" because strtok modifies the string its called on.
    Last edited by nonpuz; 02-28-2010 at 04:56 PM. Reason: expanded explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 04:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 03:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 01:10 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