   copying dynamic arrays problem
  void input_engine::addInput(int dir, int n) {    count++;    int *temp = new int [count];    int *temp2 = new int [count];    int i = 0;    for(i = 0; i < count - 1; i++) {   temp = nIDs;   temp2 = dirs;    }    temp[count - 1] = n;    temp2[count - 1] = dir;    delete [] nIDs;    delete [] dirs;    nIDs = temp;    dirs = temp2;}This is my code to add dir and n onto the end of a 'int *' array, i create a new array bigger than the old one and copy all data over before deleting the old one then assigning the pointers to the new array.I have a problem in my code where the array elements aren't initialised (they are numbers like 1347768 etc) so I was wondering if anyone sees anything blatantly obviously wrong about this code that I don't know about. :)
Quote:Original post by BeaverusIVI was wondering if anyone sees anything blatantly obviously wrong about this code that I don't know about. :)Yes - you're not using std::vector.Also, copying all of the array elements in order to increase the size by one is extremely inefficient if you are adding many elements - you need to differentiate between your dynamic array's size and its capacity. Of course, std::vector does this for you.   ;  Temp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.And yes.. it's much better to use std::vector as the above reply states   ;Quote:Original post by MaegaTemp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.Nope. nIDs and dirs are assigned to point to the arrays "new"ed at the beginning of the function. So, they do hold valid values, and there is no memory leak.   ;Quote:Original post by bakery2k1Quote:Original post by MaegaTemp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.Nope. nIDs and dirs are assigned to point to the arrays "new"ed at the beginning of the function. So, they do hold valid values, and there is no memory leak.Hmm, are you sure? I always assumed they died at the end of the function. Oh well :)   ;  Thanks guys, I've used vectors in other parts of my code, I don't why I didn't use them :SI thought the whole HULLifying thing was happening but wasn't sure.   ;Quote:Original post by MaegaTemp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.I'm tempted to simply reply with an "uh, no," but I think it's more fun to do this instead:How do you, Maega, suggest reallocation be implemented instead? Further, how do you imagine it to be handled within std::vector?   ;Quote:Original post by MaegaQuote:Original post by bakery2k1Quote:Original post by MaegaTemp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.Nope. nIDs and dirs are assigned to point to the arrays "new"ed at the beginning of the function. So, they do hold valid values, and there is no memory leak.Hmm, are you sure? I always assumed they died at the end of the function. Oh well :)Positive. What "dies" at the end of the function?Local variables go out of scope, but here that includes only temp, temp2 and i.   ;  Is it okay to return and pass vectors? I'm guessing not.   ;Quote:Original post by OluseyiQuote:Original post by MaegaTemp and Temp2 are local. Their scope is only in the local function.nIDs and dirs effectively get set to null pointers at the end of the function since temp and temp2 are destroyed (but leak memory) at the end of the function.I'm tempted to simply reply with an "uh, no," but I think it's more fun to do this instead:How do you, Maega, suggest reallocation be implemented instead? Further, how do you imagine it to be handled within std::vector?I've already realized I was wrong. No need to rub it in :P   