Redimensioning Arrays
-
How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6
-
How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6
You have two options. 1/ USe a pointer. Then you can do this:BYTE * pByte = new BYTE[256]; // use it delete [] pByte; pByte = new BYTE[300]; Of course every time you resize, you lose all the contents of the array. 2/ Use vector. Like this: #include vector vecBytes; for (int i = 0; i < 256; ++i) vecBytes.push_back(i); now you have an array of 256 values that can be accessed using [n] notation if you like, or the .at function. But you can also delete from the ends and add new items dynamically at will. If you want to delete from the middle you want list, but beware, list has the problem of slower access to individual fields ( as it's a linked list it must step through to the element you want ) Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6
Hi use malloc and realloc ex. void *pV = NULL ; ... ... .. //now u want to allocate 100 bytes pV = malloc( 100 ) ; ... .. .. //Now u want to make the same memory 200 bytes. pV = realloc(pV, 200); .. .. //Now u want to make it 50 bytes pV = realloc(pV, 50) ;
-
Hi use malloc and realloc ex. void *pV = NULL ; ... ... .. //now u want to allocate 100 bytes pV = malloc( 100 ) ; ... .. .. //Now u want to make the same memory 200 bytes. pV = realloc(pV, 200); .. .. //Now u want to make it 50 bytes pV = realloc(pV, 50) ;
Do NOT use malloc/realloc if you can at all avoid it. They are C functions, and new/delete is highly preferred, especially for complex types. The best reason for this is the fact that new/delete call constructors/destructors and the C functions do not. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
Do NOT use malloc/realloc if you can at all avoid it. They are C functions, and new/delete is highly preferred, especially for complex types. The best reason for this is the fact that new/delete call constructors/destructors and the C functions do not. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
i use globalalloc() and globalfree() and globalrealloc() a lot and they work just fine if you remember to free the memory when u finished with it whats the problem with this? --- "every year we invent better idiot proof systems and every year they invent better idiots ... and the linux zealots still aren't being sterilized"
-
i use globalalloc() and globalfree() and globalrealloc() a lot and they work just fine if you remember to free the memory when u finished with it whats the problem with this? --- "every year we invent better idiot proof systems and every year they invent better idiots ... and the linux zealots still aren't being sterilized"
lauren wrote: whats the problem with this? There's no *problem* per se, but if you mix C and C++ memory allocationj calls, you need to be careful not to free something you new'd. You also don't get the benefit of constructors and destructors. If you need to realloc, you should instead use a container like vector. Your code will obviously work, but the way of doing things I am suggesting is *better*. To read Stroustrup on this ( and I've pretty much quoted him, so you don't need to as such ), turn to page 577 of the third edition of the C++ Programming Language. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now