1) you should always post the error you get.
2) I do not know if "p_array -= size" is correct, from a sleepy look it looks ok but I need to test it to be sure.
3) realloc should allocate enough bytes to hold size integers eg
Code:p_array =(int *) realloc(p_array, sizeof(int) * size);
Aslaville must have misread the code.Originally Posted by thachdovan
That is simply error prone. Follow Salem's advice:Originally Posted by thachdovan
Instead, use a different pointer to point to where you want.Originally Posted by Salem
Furthermore, you should not be declaring global variables. Declare local variables only for now.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
My application need to allocate dynamic memory and change it in runtime. I try to use it as a local variable but I can not (I put it in some another and use extern to process). So my question is why I should not be use as local variables, could you give me some relative links or documents about this problem.Furthermore, you should not be declaring global variables. Declare local variables only for now.
Thank everyone!
Because global variables can confuse and obtuse code. They are harder to
keep track of, and because they can be modified within any function without being
passed via the parameter it's possible an overlooked piece of executing code can
modify the value by accident resulting in invalid results.
A more reliable type that is similar is to make said variable static. But of course,
there are important differences between the two.
Double Helix STL