memory and arrays

This is a discussion on memory and arrays within the C++ Programming forums, part of the General Programming Boards category; If I am creating a large array such as: Code: struct example { string word1, word2, word3, word4; int num1, ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    memory and arrays

    If I am creating a large array such as:

    Code:
    struct example
    { 
        string word1, word2, word3, word4;
        int num1, num2, num3;
    };
    
    example example1[100];
    would I be better off leaving this as is or should I allocate memory from the heap for it and returning it when I am done such as:

    Code:
    example* example1 = new example[100];
    //........
    delete example1;
    I think I have the syntax right?

    R W Marsh

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    yes, since you normally have less stack space available than heap space. when using dynamic arrays remember that you must use 'delete [] example1' to ensure the destructor is called for each object in the array.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I would use a vector instead. This puts the memory on the heap, but manages the memory for you and calls the destructors automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Clarification of arrays and memory use
    By OOPboredom in forum C Programming
    Replies: 4
    Last Post: 03-25-2004, 01:25 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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