Thread: Making the size of an array variable

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Question Making the size of an array variable

    Hello to everyone! I've just started studying C++ on the college, and in the program I'm working on I have this:

    Code:
    int num = 0;
    cin << num;
    int array[num];
    Before you ask, I did included any necessary libs. I get Visual Studio saying the error is in the last line: "... constant value expected..."

    What the hell is going on? I know this is simple, but I can't figure it out .

    Thanx in advance! C ya!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use new to allocate the memory. BTW: your use of cin is incorrect.
    Code:
    int num = 0;
    cin >> num;
    int * array  = new int[num];

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Sorry about the cin kinda hurry. Now, maybe you have mistaken or something (I shouldn't use keywords in the code ). Here's the code I wanted:

    Code:
    int num = 0;
    cin >> num;
    int numbers[num];
    Ok, now this is what I've got with your help:

    Code:
    int num = 0;
    cin >> num;
    int * numbers = new int[num];
    Now just one doubt: in the Visual Studio, the window LOCALS (where I can see my variables values), the numbers int is shown an error, that the expression cannot be evaluated...

    Will I have an array of the size of NUM with that anyway? Thanx 'til now!

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    new will allocate an array of num number of integers. you can access variable numbers the same way you would had you hardcoded the size
    Code:
    int * numbers = new int[num];
    for(int i = 0; i < num; i++)
       numbers[i] = 0;

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Allrighty! Thanx a lot man!!!

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    please don't tell people how to use new without telling them about delete...

    you need to delete (or free up) the memory you've allocated with new... so for example:
    Code:
    int*numbers=new int[num];
    //other code goes here
    //when you're done with the array use this next line
    delete[]num;
    if you forget to delete, you run into what's called a memory leak... it can cause big problems in some cases.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Ok, thanx for the note!

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    now that you know how to use new and delete for arrays....


    don't.

    use a vector instead, and avoid the pain of trying to match new/delete pairs.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by lfbang87
    Hello to everyone! I've just started studying C++ on the college, and in the program I'm working on I have this:

    Code:
    int num = 0;
    cin << num;
    int array[num];
    Before you ask, I did included any necessary libs. I get Visual Studio saying the error is in the last line: "... constant value expected..."

    What the hell is going on? I know this is simple, but I can't figure it out .

    Thanx in advance! C ya!
    When using a pre set value to set the size of an array - the integer that you use must be of type constant. IE:
    Code:
     const int num = 10;
    int array[num];
    I hope that explains your error.
    Oh my goodness.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by ChaosEngine
    now that you know how to use new and delete for arrays....


    don't.

    use a vector instead, and avoid the pain of trying to match new/delete pairs.
    what's so hard about matching new and delete?

    vectors take care of all that for you, but I still prefer new and delete (personal preference)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by major_small
    what's so hard about matching new and delete?
    What if you want to resize the array? or concatenate 2 arrays together? suddenly the problem is far bigger than just matching new[] and delete[] ...

  12. #12
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I believe you need vector just when you want dynamic resize of a vector, i.e., use vector instead of realloc.

    Just an opinion...

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Bench82
    What if you want to resize the array? or concatenate 2 arrays together? suddenly the problem is far bigger than just matching new[] and delete[] ...
    how so? just allocate a second (or third) array, put the data into that, and delete the original (two) array(s).
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by major_small
    how so? just allocate a second (or third) array, put the data into that, and delete the original (two) array(s).
    That's OK if you don't mind code fulll of new's and delete's, aswell as copying algorithms and temporary objects. But all that could easily be cut down to a line or two of STL code, and be much easier to read and debug IMO, especially when it comes to someone else trying to understand your code.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it's all still pretty much the same amount of code, the only thing that really changes is who writes it
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM