Memory limit exceeded


Please help as I don't understand why I am exceeding the memory limit when I add a new item to the array, is it exceeded due to something else? this is an excerpt from my code:


int min=1000000,max=0;

unsigned char tab1[min+1];


int n=0, input=0, b=0;

std::cin>>n;

for (int i = 0; i < n ; i++) {
std::cin >> input;
if(input < min) { min=input;}
if(input > max) { max=input;}
tab1[input]= '1';
}
Please use the code format tags to format your code.
 
unsigned char tab1[min+1];

This is not valid C++, it's not flagged as an error because this syntax was introduced in C99, and some compilers import C99/C11 features into C++. In my view, it shouldn't be allowed. I read somewhere in an interview with Dennis Ritchie that a bunch of features slipped into C99 that he wasn't happy with, but admits he didn't read the white papers closely enough, I think this is one of those features.

It means "allocate an array of size min+1 from the stack".

The problem is, the stack is a limited resource. Modern complex operating systems can grow the stack, but that's not generally true, and on embedded devices, the stack can be as little as 2k.

You've asked for a stack variable of size: 1000001. 1Mbytes is quite a lot.

The correct thing to do is get the memory from the heap, which in C++ can be:
1
2
3
4
5
6
7
8
#include <iostream>
#include <memory>

int main() {
    std::size_t min = 1000000;
    std::unique_ptr<char[]> tab1 = std::make_unique<char[]>(min + 1);
    // ...
}


A more direct and understandable, but less safe, way is:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
    std::size_t min = 1000000;
    char* tab1 = new char[min + 1];
    // ...
    delete [] tab1;
}


I've used char instead of unsigned char because it seems you mean char from tab[input] = '1';, as '1' is a char.
Last edited on
That code is non-standard c++ - as the size of tab1 is specified at run-time and not compile time.

If tab1 is created on the stack (which it usually would be for a c-style array), then there is a limit to the size that can be created on the stack.

Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {
	constexpr size_t maxsze {1'000'000};

	size_t min {maxsze}, max {};
	size_t n {}, input {};

	static unsigned char tab1[maxsze + 1];

	std::cin >> n;

	for (size_t i = 0; i < n; i++) {
		std::cin >> input;

		if (input <= maxsze) {
			if (input < min) min = input;
			if (input > max) max = input;
			tab1[input] = '1';
		} else
			std::cout << "Value above " << maxsze << '\n';
	}
}


which puts tab1 on the heap.
if its on a PC, you may be able to make the stack big enough to hold that by tampering with compiler settings. I would not do it that way, and I don't know the max size, but if you want to play with it...
Last edited on
https://docs.microsoft.com/en-us/windows/win32/procthread/thread-stack-size
1M is your default, so burning the whole lot on one line of code is just wasteful.

Sure you can fiddle with it, but you really need to use dynamic memory for larger data structures.
https://docs.microsoft.com/en-us/cpp/build/reference/stacksize?view=msvc-160

Linux has similar limits
$ ulimit -a | grep stack
stack size (kbytes, -s) 8192

Topic archived. No new replies allowed.