Exforsys
+ Reply to Thread
Results 1 to 2 of 2

array & dynamic memory allocation

This is a discussion on array & dynamic memory allocation within the C and C++ forums, part of the Programming Talk category; You can use array and pointer equivalently. How can you handle array dynamically?...

  1. #1
    usha ranawade is offline Junior Member Array
    Join Date
    Nov 2006
    Answers
    2

    Thumbs up array & dynamic memory allocation

    You can use array and pointer equivalently. How can you handle array dynamically?


  2. #2
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128
    Dynamic array allocation is actually a combination of pointers and dynamic memory allocation.While static arrays are declared prior to runtime and are reserved in stack memory, dynamic arrays are created in the heap and released from the heap and in C++ this is handled by using the new[ ] and delete[ ] operators.

    int *example;

    This C++ statement simply declares an integer pointer. A pointer is a variable that holds a memory address. Declaring a pointer doesn't reserve any memory for the array and that will be accomplished with new[ ].

    The following C++ statement requests 15 integer-sized elements be reserved in the heap with the first element address being assigned to the pointer example:

    example = new int[15];

    The new[ ] operator is requesting 15 integer elements from the heap.Dynamic array allocation is nice because the size of the array can be determined at runtime and then used with the new[ ] operator to reserve the space in the heap.


Latest Article

Network Security Risk Assessment and Measurement

Read More...