question about array's

This is a discussion on question about array's within the C++ Programming forums, part of the General Programming Boards category; is it better to have an array on the stack or the heap? and why are the reasons for whatever ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    question about array's

    is it better to have an array on the stack or the heap? and why are the reasons for whatever answer you may give. thanks a lot

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It depends what you want to do, however if efficiency is a concern it's better to declare them on the stack if possible .

  3. #3
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    If you know the size of the array at compile time use the stack

    If the array size is unknown (i.e. if it depends upon the user's input) then you should use just the amount you need from the heap instead of declaring a huge array on the stack.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    i was under the impression that an array had to have a fixed size at compile time. could you give me an example of how to allow an array size to be user defined, as

    type ArrayName[x] doesn't work.

    thanks a lot.

  5. #5
    Unregistered
    Guest
    int num;

    cout << "how big an array do you want?" << endl;
    cin >> num;

    int *array = new array[num];

    cout << "you got it buddy." << endl;
    cout << "now to get rid of the memory after you are done I need to do a bit of behind the scenes magic." << endl;

    delete [] array;

    array is an array of size num that is declared on heap using user input and after being used for the last time (never here) delteted to prevent memory leaks.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Unregistered said:

    int *array = new array[num];
    I'm sure he meant to say:

    Code:
    int *array = new int[num];
    array then is essentially an array of num integers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Unregistered
    Guest
    boy am I screwing up today. Maybe I should just take a back seat and watch until I can get a few more zzzzzzzzzzzzzzzz's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM

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