memory allocation

This is a discussion on memory allocation within the C++ Programming forums, part of the General Programming Boards category; Hello! I'm working on a computer with Windows 7 and use Visual C++ Express. The computer has 4 G memory. ...

  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
    21,639
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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, 09: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, 03:32 PM
  5. Memory Allocation
    By DutchStud in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2001, 03:43 PM

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