Thread: memory allocation

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    memory allocation

    Hello!

    I'm working on a computer with Windows 7 and use Visual C++ Express. The computer has 4 G memory.
    I'm doing a simulation study at the university and need to store large amount of data. I want to store the data in the memory so I can get a fast simulationprocess. If I store the data as an array, " int data [D]", I can use max D=250,000. If I store the data as " vector <int> data; data.reserve(D)", I can use max D=390,000,000. I've noticed that the computer works much faster with arrays than vectors.
    Why can I only use D=250,000 with "int data[D]"? Can I somehow change the code so I can allocate more memory with arrays?

    Lars-Erik

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lars-erik
    I've noticed that the computer works much faster with arrays than vectors.
    Make sure that you turn on optimisation by the compiler and turn off features like checked iterators.

    Quote Originally Posted by lars-erik
    Why can I only use D=250,000 with "int data[D]"?
    You reached the limit for allocating on the stack.

    Quote Originally Posted by lars-erik
    Can I somehow change the code so I can allocate more memory with arrays?
    It may be possible to raise the limit, to a point, but generally the solution is to either use dynamic memory allocation (as you did with std::vector) or declare the array to be global and/or static (which may not be appropriate).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why can I only use D=250,000 with "int data[D]"?
    Well the problem is as laserlight has already said - you're out of stack space.

    The easy replacement is
    Code:
    int *data = new int [D];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would say a vector is a better solution since you don't have to worry about deallocation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on memory allocation(?)
    By adameye in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 02:31 PM
  2. memory allocation
    By afotoohi in forum C Programming
    Replies: 1
    Last Post: 03-04-2003, 10:21 PM
  3. Memory Allocation
    By Strut in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 05:15 PM
  4. Memory allocation
    By Leiken in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 04:32 PM
  5. Memory Allocation
    By DutchStud in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2001, 04:43 PM