3

I'm very new to C++ so I'm somewhat confused about how static arrays work. I know in C# the array isn't placed into memory until it's first accessed which can be problematic if you want it to be instantly accessible. However, I'm working on converting a Perlin class to C++ and I'd like to have multiple static arrays of which only one may be used during runtime or any number of them. In reality, it's not really that big of a memory issue as none of them will be more than 50kb, however, I'd rather know if it's possible to ensure the array isn't loaded into memory unless I ask for it. Is there a way to ensure a static array defined in source code isn't loaded into memory unless asked for? It's a pretty nitpicky thing (esp w/ x64), but I'd prefer to be as optimized about it as possible. I hate the idea of taking up memory with something that isn't going to be used.

Or maybe static arrays aren't even the way to go - just dynamical class object wrapped arrays?

I guess the real question is: what is the most efficient solution for implementing table-lookups in c++ that might not all be used?

3
  • If you make the arrays be const arrays (with any sub-objects const) then they can live in the code area , i.e. they won't require any extra allocation of memory. The code area of the executable will be mapped into the process's virtual address space and things can be read directly out of it. Commented Aug 1, 2014 at 2:58
  • So static const array is faster than just static? To me the compiled program is magic voodoo stuff (I.E. I don't understand the underlying compiled structure very well). Compiled code of a program is fully loaded into memory then while the program executes? There is no disk access? Commented Aug 1, 2014 at 3:00
  • it's system dependent. I only know the details for ones I'm familiar with (which are not x86 type systems!) Commented Aug 1, 2014 at 3:20

3 Answers 3

5

Static arrays will be in your memory space, with no way to omit or free them, but that is not the same thing as 'in memory'. Leave it up to Windows virtual memory manager. When you first access an array Windows will bring it from disk into RAM.

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

1 Comment

I suppose I should have used the term "RAM" - but - yes, that was my general question. I will probably accept the other guy's answer though as he offered an alternative via runtime packing of an array from file which I suppose ought be "more accepted" than the other correct answer. Thank you.
2

No, you cannot do that: statically initialized structures and arrays in C++ are loaded into memory along with the rest of your code, so you cannot influence the time at which it gets loaded.

If you must load your static array at runtime, consider changing your strategy to placing the data into a separate file, and adding an initialization function to read the file into a static vector object. This strategy results in placing the data into the dynamic memory area, while the vector object itself could remain static.

2 Comments

50KB of address space is nothing to get worked up about. That's smaller than a 128x128 bitmap. And it's just address space not even memory.
I know that - but now I know if I want to have a "maxint" table look up, that I ought to load it from a file. (This is the first time I've written x64 code as well, so I'm used to being a little more concerned about extra memory in general). I might have a lot of these static arrays with a different selection of gradient vectors for different noise effects, hence I was wondering how the memory worked in a little more detail.
2

Both Windows and Linux uses "demand loading", which means that code and data is loaded when the code reaching it actually needs the data. So assuming the data is constant and global (e.g. static const int x[number] = { ... }), the data will not be loaded. [The typical granularity for this is 4KB or some multiple thereof, but if you have, say, several hundred 50KB blocks of data that aren't being used, you should not see them in memory, and thus no delay in loading the program itself].

As always when it comes to performance and optimisations, it's best to NOT overcomplicate things by trying to predict problems in an area (aka "premature optimisation"), and make sure that what you think MAY be a problem actually is a problem before you optimise it.

4 Comments

Every extra unnecessary processing step is a "problem" in noise generators - but I understand what you mean in general - but I disagree with your "don't overthink optimization" on algorithms that may be pulling a million or more samples a second. I just don't want to get too committed to a particular algorithm structure if C++ behaves in some way that I don't quite understand. I've only been writing on it for a month coming from C#.
Yes, but adding extra code to handle a problem that may not actually be a problem is definitely a potential for slowing things down. So don't do that UNLESS you know you have to.
Not adding new code - looking for ways to reduce steps and slow operations in algorithms I need to have to maintain versatility while ensuring speed. The look-up tables are necessary for a fast Perlin algorithm, just a matter of knowing how to most efficiently implement them. I definitely understand what you're saying though and have at times slowed something down trying to speed it up, but I've learned the difference between optimizing and making things worse while trying to optimize. Though I do tend to write very "all-purpose" classes which results in slower but more portable operations.
I hate rewriting a class object that were designed in a proprietary fashion for use in something else so I look for ways to keep my opinions open for portability.

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.