I'm just starting to scratch the surface of c++. I want to know how can you realloc memory in C++. (It is like in C ? use realloc function ?)
if i make:
it will keep the first values I enter?Code:int *x= new int[10]; x=new int[20];
I'm just starting to scratch the surface of c++. I want to know how can you realloc memory in C++. (It is like in C ? use realloc function ?)
if i make:
it will keep the first values I enter?Code:int *x= new int[10]; x=new int[20];
You'll need to copy the original array over (to another new-ed memory) and free it.
The snippet you have loses the contents of x and creates a memory leak, because you won't be able to delete the memory x pointed to before.
Since all this needs a lot of work and is easy to mess up, many people use std::vector from C++ standard libraries when they need a dynamically resizing array.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
We recently had a thread on it.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C