Official RPG Codex Discord Server

  1. Welcome to rpgcodex.net, a site dedicated to discussing computer based role-playing games in a free and open fashion. We're less strict than other forums, but please refer to the rules.

    "This message is awaiting moderator approval": All new users must pass through our moderation queue before they will be able to post normally. Until your account has "passed" your posts will only be visible to yourself (and moderators) until they are approved. Give us a week to get around to approving / deleting / ignoring your mundane opinion on crap before hassling us about it. Once you have passed the moderation period (think of it as a test), you will be able to post normally, just like all the other retards.
    Dismiss Notice

I need a workaround for C++ limitations on fixed array size.

Discussion in 'Codex Workshop' started by desocupado, Dec 5, 2014.

  1. desocupado Magister

    desocupado
    Joined:
    Nov 17, 2008
    Messages:
    1,802
    So, you guys know this:
    double happy_array[multiple*happiness];
    is not allowed in C++. Only:
    double fuck_you_array[const_value_of_dicks];
    is.

    Thing is, I have a class that takes a double* as argument, but I really need the number of elements to be defined at run-time.

    Any ideas?
     
    ^ Top  
  2. desocupado Magister

    desocupado
    Joined:
    Nov 17, 2008
    Messages:
    1,802
    Derp. I think using new[] should do the trick.
    Maybe.

    EDIT: NEVERMIND, new[] did the trick. Sorry to bother, but for some reason I can't delete the thread.

    Dumb questions, that's what you get for programming when your brainpower is already spent.
     
    Last edited: Dec 5, 2014
    • Brofist Brofist x 1
    ^ Top  
  3. DraQ Prestigious Gentleman Arcane

    DraQ
    Joined:
    Oct 24, 2007
    Messages:
    32,825
    Location:
    Chrząszczyżewoszyce, powiat Łękołody
    Is there any reason for not using std::vector<double>?

    If you do insist on using plain ol' dynamic array, remember to delete[] it when you no longer need it.
     
    ^ Top  
  4. Declinator Arbiter

    Declinator
    Joined:
    Apr 1, 2013
    Messages:
    542
    Alternatively, if C++11 features are available, you could use a std::unique_ptr<double> array and not need to worry about deletion.

    Still, a vector is probably the better option in almost all cases. You can reserve the needed space immediately so as to avoid multiple allocations with reserve().
     
    ^ Top  
  5. Norfleet Moderator

    Norfleet
    Joined:
    Jun 3, 2005
    Messages:
    10,887
    Yes, remember to delete it when you're done, though. If you new things and don't delete them, they hang around in memory forever, lost in space, and your program leaks memory until it runs out and goes boom. Don't do that. C++ is an oldschool language, not that newfangled popamole managed stuff.
     
    ^ Top  
  6. Twiglard Poland Stronk Patron Sad Loser

    Twiglard
    Joined:
    Aug 6, 2014
    Messages:
    5,263
    Location:
    Poland
    Strap Yourselves In
    Actually with C++11 it's idiomatic to have the memory automatically managed, std::make_shared and friends. Modern C++ doesn't look like "C with classes" anymore.
     
    ^ Top  

As an Amazon Associate, rpgcodex.net earns from qualifying purchases.