Trouble with memory allocation

I have an application that uses a char** to store char* returned from a function.

char* someFunction(int size)
char* someChar=new char[size];
do something
return (someChar);

I declare my char** at the global level like this:

char **arr;

I allocate memory to the array like this:

template <typename T>
T **AllocateDynamicArray( int nRows, int nCols)
T **dynamicArray;

dynamicArray = new T*[nRows];
for( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new T [nCols];

return dynamicArray;

arr=AllocateDynamicArray<char>( int nRows, int nCols);

I then try looping through the array assigning the char* to arr[x] like this:

for (x=0;x<something;x++)...where something is <=nRows

when I output the values in arr[x], all of the values are the same...

What seems to be the disconnect...

I'm a novice with forte is with Java.

Thanks in advance.
Last edited on
Is there a reason you are not using std::string and std::vector here?
My someFunction calls another function that builds a char* randomly character by character with no term \0... not sure how i would apply the string or vector to this algorithm...

any insight would be greatly appreciated...


edit: are you suggesting using a vector<char*> vs the char**?

thanks again.
Last edited on
He's suggesting you use vector<string>.

What do you mean it builds it character by character? Do you keep reallocating space for new characters? std::string does this for you.

If you come from Java, realize that C++ has no garbage collection, so anything allocated with NEW has to be DELETEed.

Because of the above, and many other problems, try to avoid raw pointers as much as possible. Use containers like vector, which handle allocation and deletion internally.

Don't use global variables.

And your problem is probably that your randomization function isn't actually random.

Thanks for the reply.

I understand the garbage collection vs freeing the memory in code...and the pseudorandom character generation works adequately for my purpose...the char* that is produced is exactly what i'm looking for.

The problem is when I assign the returned char* to the char** via arr[x]= i get the same char* in every element of arr[x]. I did try using the vector<char*> and it basically worked the same as the char** except without having to resize obviously.

on a few runs the char* were different in the arr[x] but did not match the char* produced by the someFunction, but did include pieces of the prior two char*s produced...

I realize i"m doing this in a more C way than C++ but am looking for the computational speed advantage this produces.

any other insight would be appreciated...

Can you post the char generation code?
I realize i"m doing this in a more C way than C++ but am looking for the computational speed advantage this produces.

What makes you think there is a "computational advantage"?

Your structure (new[]'d array of pointers to first elements of different new[]'d arrays of char) is the same as vector<string> or vector<vector<char>> internally, but is a lot harder to use. It also cannot provide exception safety.
Start with vector<string>, then if accesses actually become a bottleneck, find out why (if you have a lot of column-wise traverses, e.g. the data is used as a matrix, then a matrix library could improve a few things). Don't use "new".
Last edited on
Thanks for the replies...

i switched to using vector<char> and a vector<vector<char> > and is it working perfectly as was recommended by all

thanks again...
Topic archived. No new replies allowed.