I'm having a little difficulty understanding why a piece of my code cannot be modified to work with stack instead of heap.
Well that is just a descriptiom, I'm aware there are no gurantees where my data will be stored.

As it stands my working code is...

//u_char = unsigned char
int HEADOFFSET = 12;
int headlen = data->headlen; // size of the data I am copying from
int raw_len = headlen - HEADOFFSET; // size of the data array I will be copying to
u_char * newdata = new u_char[raw_len]; 
memcpy(newdata,&data[HEADOFFSET], raw_len); // copy data from point of offset

Parse(newdata); // just a function to parse the data

// here I'm just printing it out while debugging (it is all ascii chars)
string std_str(newdata, newdata + raw_len);
cout << std_str << endl; // Prints all data as expected
delete[] newdata;
newdata = 0;

My Problem starts when I try to use a fixed newdata array instead of dynamically creating and deleting one.
My reason for wanting to do this is that the above code is within an extrememly tight loop, the whole code works reasonably well, and it's now time for me to try to optimize it.

//u_char = unsigned char
int HEADOFFSET = 12;
int headlen = data->headlen; // size of the data I am copying from
int raw_len = headlen - HEADOFFSET; // size of the data array I will be copying to
u_char * newdata[1200] = {NULL}; // Also tried moving this declaration outside loop
memcpy(newdata,&data[HEADOFFSET], raw_len); // copy data from point of offset

Parse(newdata); // just a function to parse the data

// here I'm just printing it out while debugging (it is all ascii chars)
string std_str(newdata, newdata + raw_len);
cout << std_str << endl; // Prints garbage <<<<<<<<<<<<<
//delete[] newdata;
//newdata = 0;

So now my program breaks, and is printing garbage.
I'm hoping someone might help me understand why that is, and perhaps make a suggestion.

Thank you for reading.

(edit) As usual, I seem to have realized the problem after imparting it.
the problem was a simple '*' :(
u_char * newdata[1200]; became u_char newdata[1200]; (no pointer needed)

I would appreciate it if someone would confirm, that this will increase efficiency.
In my search I was led to believe that creating dynamic arrays on the heap cost a considerable penalty in speed.

Recommended Answers

All 2 Replies

u_char * newdata[raw_len]

C++ does not allow variable length arrays, that is when declaring an array like this, the length must be a constant. So raw_len is not allowed as the length. There is no standard way to create arrays of unknown size on the stack. However some C++ compilers do allow variable length arrays as an extension and many libcs also contain an alloca function that allocates memory on the stack instead of the heap.

PS: If the above code did work, it would declare an array of u_char pointers, not an array of u_chars. It should be u_char newdata[raw_len] to create an array of u_chars.

Thanks sepp2k.

u_char * newdata[raw_len] was an error after copy patse, I just modified the code from this forum editor to illustrate my problem.

I appreciate your answer and time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.