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.