NO

Author Topic: Dynamic array  (Read 3480 times)

colddax

  • Guest
Dynamic array
« on: July 11, 2007, 03:08:51 AM »
Hello Everyone (again).

I hate to be a pest, but I have a segment of code that I need a little guidance with.

I basically have a dynamic array that I am inserting strings into. Upon assignment everything looks fine. It is when I wish to iterate through the array members that my program crashes.

Code: [Select]
char ** image_ranges = NULL; // holds all image ranges to use

... later on in the program ...

Basically I take a string like A0100-103 .. break it out to
A0100, A0102, A0103 and insert each piece into the array


_itoa( beg_num, begin_number, 10);
number_length = strlen(image_prefix) + strlen(zero_pad) + strlen(begin_number) + 5;

// allocate space in the image_ranges array and insert image into array
image_ranges = (char**) realloc (image_ranges, (image_ranges_index +number_length) * sizeof (char * ) ); // create space for a pointer to the image
image_ranges[image_ranges_index++] = _strdup(image_prefix) ; // allocate space for the image and store a pointer to it into the image_ranges array
strcat( image_ranges[image_ranges_index-1]  , zero_pad );
strcat( image_ranges[image_ranges_index-1]  , begin_number );
strcat( image_ranges[image_ranges_index-1]  , ".txt\0" );

The above code appears to work. It is when I try to iterate through the members that I get a problem.

Code: [Select]
int counter;

for(counter = 0; counter < image_ranges_index; counter++){
  printf(" \n Removed :: %d :: %s \n", strlen(image_ranges[counter]), image_ranges[counter] );
free(image_ranges[counter]);
}

The members like E100 do not print all of the characters. I have tried increasing the size of the realloc call but it does not work. I noticed that if I do a strlen of the set I iterate through it is one less than it should be.

Am I just missing some simple thing here?

Please let me know if more information is required.

Many thanks for any help that can be provided.



JohnF

  • Guest
Re: Dynamic array
« Reply #1 on: July 11, 2007, 10:15:58 AM »
You should supply code that has all necessary variables declared and initialized. In other words, code that can be pasted and that will compile without someone having to fill in stuff that is missing.

Have you tried walking through the code with the debugger?

John
« Last Edit: July 11, 2007, 11:53:54 AM by JohnF »

colddax

  • Guest
Re: Dynamic array
« Reply #2 on: July 11, 2007, 02:52:35 PM »
Ok, I think I have it solved.

I was using the realloc call incorrectly.

I declaired a new pointer, and copied everything into that. I then copied the new pointer to the array.

Code: [Select]
char *testptr = NULL;

.. Later ..

testptr = (char*) malloc ( number_length * sizeof(char) );

strcpy(testptr, image_prefix);
strcat(testptr, zero_pad);
strcat(testptr, begin_number);
strcat(testptr, ".txt\0");
// allocate space in the image_ranges array and insert image into array

image_ranges = (char**) realloc (image_ranges, (image_ranges_index + 1) * sizeof (char * ) ); // create space for a pointer to the image
image_ranges[image_ranges_index++] = _strdup(testptr) ; // allocate space for the image and store a pointer to it into the image_ranges array
free(testptr);

Sorry for the bother, and thanks for the help!