Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I just want a basic rundown of how to declare and resize a dynamic array in C++ (I looked at the help file, and theres nothing specifically on this, I just need to know which function to use for the resizing and the declaration syntax)
Athlon XP 1800+
512Mb DDR266 RAM
Leadtek Geforce 6800
Thermaltake 360W PSU
56X CD-ROM
52x24x52 Samsung CD-RW
20 GB Maxtor HDD
80 GB Hitachi HDD
Gigabyte GA-7DX+ mobo

Hi,
As much as I remember you use "new" method to declare an array, as in:
new int[10];
I don't know of any pure C++ function to resize arrays, but as each C++ program is also a C program you may also use the "malloc" (for allocating an array) "realloc" (for resizing it) and "calloc" to declare an initialized array.IMPORTANT: If you are using the "new" method you should use "delete" for releasing the memory, or "delete[]" for an array. If you are using the "malloc"\"realloc"\"calloc" functions you should use "free" to release. Mixing those (e.g., allocating with "new" and releasing with "free" my cause memory leaks.
One more thing, as a pointer may point for both a variable as well as an array,
( int* p;
p = new int[10]; //legal
p = new int; //also legal )
you might get some problems deciding wether to use "delete" or "delete[]", as well as this might cause memory leaks. The sollution is simple: define the variable as a "1" size array.You can also find further info regarding the above functions and methods at the MSDN site located at http://msdn.microsoft.com/
Good luck, Itai.

Great, thanks a lot!
I'll try it out as soon as possible.Athlon XP 1800+
512Mb DDR266 RAM
Leadtek Geforce 6800
Thermaltake 360W PSU
56X CD-ROM
52x24x52 Samsung CD-RW
20 GB Maxtor HDD
80 GB Hitachi HDD
Gigabyte GA-7DX+ mobo

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |