Computing.Net > Forums > Programming > C++ Dynamic Array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Dynamic Array

Reply to Message Icon

Name: livingdecay
Date: October 17, 2004 at 00:11:11 Pacific
OS: Windows XP Professional
CPU/Ram: AMD Athlon XP 1800+/512Mb
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Gargamel
Date: October 17, 2004 at 16:38:56 Pacific
Reply:

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.


0

Response Number 2
Name: livingdecay
Date: October 19, 2004 at 10:42:45 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Dynamic Array

C++ Dynamic arrays sparse matrix? www.computing.net/answers/programming/c-dynamic-arrays-sparse-matrix-/11530.html

c++ dynamic array www.computing.net/answers/programming/c-dynamic-array/6431.html

Dynamic Arrays in C www.computing.net/answers/programming/dynamic-arrays-in-c/3396.html