2

I need to create a very large array. Let us say 50 megabytes.

Can I be safe to create it as a normal static array? Will the compiler put it on the stack (possibly causing a stack overflow), or will it be smart enough to put it on the heap?

If there is no way to do so, is there an easy way to do it with malloc or "new" when program starts, but automatically free it when program ends?

2
  • 4
    Do you mean static as in keyword static or as in 'not dynamic?' Commented Jan 31, 2011 at 23:06
  • static as in keyword. I assume this also means it's not dynamic! Commented Jan 31, 2011 at 23:29

5 Answers 5

3

As I understand it, static variables don't live on the stack. If they did, where would they go when you pop the stack frame they live in? Static function variables need to keep their state between calls, so logically, the static data should be kept on the heap.

Also, when the program ends, everything is automatically deallocated.

Sign up to request clarification or add additional context in comments.

7 Comments

Ah, I guess they won't be on the stack then.
Everything is automatically deallocated on the stack, not on the heap.
Aren't they both within the memory allocated for the process? Once the program ends, it's released back to the OS.
@Twisol: They memory will be deallocated, but the destructors of the elements won't be called.
@Twisol: not running destructors is a major decision: it's very nice to have the ability to perform some statistics, logging etc. there, and some objects may use shared memory, temporary files or other resources that aren't automatically deallocated (on their OS). Further, habitually deleting memory makes it easier to spot actual memory leaks in valgrind, purify, insure etc..
|
2

The easy way to do this is by using std::vector

std::vector   data;
data.reserve(<Number of Elements);

or potentially std::deque (depending on your usage).

Will the compiler put it on the stack (possibly causing a stack overflow), or will it be smart enough to put it on the heap?

A stack overflow is when the theoretical stack and theoretical heap collide and intermingle. If the stack was going to overfow then the heap is also going to fail.

Some systems have a maximum size of stackframe (this is compiler and platform specific) see your compiler documentation for details. As a result it is usually better to allocate huge structures dynamically (though not directly).

std::vector does this (probably). It has a small local object presence but the main payload (usually) is implemented as a dynamic heap allocation.

14 Comments

I don't want to use std::vector. I guess I can take advantage of destructors and just wrap it in a class though.
Because I asked about arrays.
@Pepe: How is a std::vector different from an Array in the usage you want to perform?
@Charles: What overhead does std::vector have that one's own class with a dynamically allocated array wouldn't have? Or even just a plain dynamically allocated array, for that matter?
@Pepe: comment +1 because it cracked me up
|
2

It's better, in my experience, to allocate such a large array on the heap (thus via new) - I have seen a program core dump on a unix system after allocating 2 MB on the stack... If you want automatic deletion, you could use a smart pointer (say, boost::scoped_array). But, since you mention "deleting it automatically when the program ends", you actually don't have to do anything - the operating system will reclaim all of your process's memory when it terminates.

Anyway, you really should use std::vector instead of a raw array.

Comments

1

50 megabytes is not too much by today standards.

You can allocate it at the start of your program using C++ new operator, and deallocate it with delete[] at end (or at the start/end of a defined program section).

If this array represents e.g some file to be loaded, it's better to allocate it of course when the file is loaded into memory. Optimally, you can only map only a section of the file into memory (e.g: 1MB, 2MB, or another logical "unit" you want to use) (see MapViewOfFile in Windows and mmap in UNIX systems). This way you can load very large files without exhausting your virtual memory.

3 Comments

Manually invoking new and delete is a ticket to Leakapalooza.
If I didn't want a ticket to leakapalooza then I would be writing code in baby java!
@FredOverflow: you can carefully track resources (RIAA) if you need C++ 2) program in .NET or Java.
1

If you allocate it statically, it'll be allocated statically. In a typical case, there will be a record of some sort in the executable that specifies that a particular variable should be a zero-initialized block of size N. The loader will normally honor that, just like it allocates space for the program's code and such (e.g., it'll allocate address space but quite often not actual memory to back it until/unless you actually read/write that memory).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.