![]() |
|
#1
|
|||
|
|||
C 2D array problem. Noob.Hi all,
Apologies for covering old ground AGAIN... I'm having trouble with a 2D array, pointers, malloc and free (a classic combo). I'm getting a variety of core dumps when running the code below... can anyone spot the problem? The core dumps are for reasons such as: "free(): invalid next size (fast)". So I guess I'm freeing something I shouldn't be, or corrupting something with overflow. Help! Firstly, I have declared the structures in my software as follows: CPP / C++ / C Code:
I am creating the initial pointer as follows: CPP / C++ / C Code:
The problematic code follows (by the time I get here, I have char arrays param1, param2 and param3 which are all less than 256 characters and are null terminated) : CPP / C++ / C Code:
It's supposed to be an implementation of a map of char-arrays to 'lists' of other char-arrays. param1 is the 'key' and param2 is the char-array to be stored in the 'list'. param3 is kind of irrelevent, but needs to be stored beside the 'key'. It core dumps if I run the problematic chunk as a function with a whole bunch of test data, and it varies how far through the test data I get. Help! Much appreciated! |
|
#2
|
|||
|
|||
Re: C 2D array problem. Noob.Quote:
The following allocates memory that you are going to use as an array of pointers to your structs. CPP / C++ / C Code:
For each pointer, you have to allocate an array of structs (memory for all of the structs that are going to be on that row) before you have anything that you can use. Maybe something like CPP / C++ / C Code:
Now you have the equivalent of a 2-D array of structs. You can access its members by CPP / C++ / C Code:
i is greater than or equal to zero and less than number_of_rows j is greater than or equal to zero and less than however_many_structs_there_are_for_row_number_i Now, make sure that you don't change the values of new_struct or any of the new_struct[i] values anywhere in your program. Then when you are through with the array, deallocate the storage by something like the following: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: C 2D array problem. Noob.Quote:
CPP / C++ / C Code:
and, for de-allocating: CPP / C++ / C Code:
I regret the error. Regards, Dave |
|
#4
|
|||
|
|||
Re: C 2D array problem. Noob.Hi Dave,
Thanks for the reply... I may have wrongly described it as a '2d array' when in fact it's more like an array of pointers to structures that have arrays of pointers to char arrays. I was also looking more for the bug in my code... which I've now found! CPP / C++ / C Code:
should be CPP / C++ / C Code:
So I've been allocating my last struct into unmalloced space, which is why it's been going mental! Thanks for the help though, Weasel. |
|
#5
|
|||
|
|||
Re: C 2D array problem. Noob.Obviously without the bold tags around the (found+1), i was trying to emphasize the change!
|
Recent GIDBlog
Toyota - 2008 August Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with filling a char array in a windows GUI app. | ryver | C++ Forum | 1 | 13-Aug-2006 08:22 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 03:59 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| problem reading to a dynamic array | noamfrie | C Programming Language | 9 | 02-Jan-2005 18:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The