any reason why this wouldn't work?

Started by
4 comments, last by billybob 21 years, 2 months ago
inline void * MyRealloc(void * Mem, long size) { char * temp = new char[size]; memcpy(temp, Mem, size); delete [] Mem; return (void*)temp; } something isn''t working, and i suspect its this, so i want to rule it out as a possible cause. pretty simple, if anything i think its the delete [] Mem, but i''m not sure.
Advertisement
What exactly are you trying to acheive?

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=
you are copying "size" bytes from the old array to the new, that means that the new array must be smaller or equal to the old array, or the program will missbehave.
that is prolly not what you want

oh ok, i thought it would just copy memory that doesn''t matter into the new array.
Assuming the new array is larger than the old, it will only read from outside the old array, not write, so you won''t mess up other things. However, reading outside an array is as much no-no as writing outside it. You''re not guaranteed to have read access outside that array. It may work sometimes, sometime it may not, so you better make it right from the beginning.
I have something like this in my program too. Try this:
inline void * MyRealloc(void * Mem, long old_size, long new_size){    // take the smaller size of the 2    long copy_size = new_size >= old_size ? old_size : new_size;    char * temp = new char[size];    memcpy(temp, Mem, copy_size);    delete [] Mem;    return (void*)temp;} 


I think this is right... tell me if its wrong so I can fix mine

thanks
Evillive2

This topic is closed to new replies.

Advertisement