Arrays vs. Pointers

This is a discussion on Arrays vs. Pointers within the C++ Programming forums, part of the General Programming Boards category; Why do arrays have to be statically allocated while pointers may be allocated dynamically?...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    Question Arrays vs. Pointers

    Why do arrays have to be statically allocated while pointers may be allocated dynamically?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Because pointers aren't really allocated. Pointers are assigned, just like an integer variable would be assigned to a value. Arrays do not have to be statically allocated. Try this:

    Code:
    int var = 15;
    char*  arry = new arry[var];
    
    //... later
    delete arry;
    This little bit of code illustrates dynamic memory allocation with the new operator and pointer assignment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    You can create a dynamic array as follows
    Code:
    int size=0;
    int *val;   <--pointer
    
    cout << "enter arr size";
    cin >> size;
    val = new int[size];
    originally size was zero but now you have an array that has memory dynamically allocated to it

    Sophie
    simple is always an understatement.....

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by PorkyChop
    Because pointers aren't really allocated. Pointers are assigned, just like an integer variable would be assigned to a value. Arrays do not have to be statically allocated. Try this:

    Code:
    int var = 15;
    char*  arry = new arry[var];
    
    //... later
    delete arry;
    This little bit of code illustrates dynamic memory allocation with the new operator and pointer assignment.
    adding on to this:

    you need to delete a dynamically allocated array as follows:

    delete []arry;

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Another addition: an array in the C sense can still only be allocated statically. The dynamic method makes use of pointers again.

    But it doesn't matter if you think in computer terms. It's just memory...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: Arrays vs. Pointers

    Originally posted by mrcowflops
    Why do arrays have to be statically allocated while pointers may be allocated dynamically?
    That is the specifications of the C++ programming language.
    when you declare a pointer, you only reserve space to store one memory address, then if you should allcoate more space to have an array associated with this pointer.

    BTW: I don't like this type of titles for threads!
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 09:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21