← Back to topics
Topic

Invalid memory reference (memory limit exceeded)

s
s-lime
http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100029394

Can someone please look why I got this error only at 4. case...
I'm sure that I didn't exceed the memory limit (16MB), because the field is max 1,000,000 tiles big...
p
picsel
DFS is not good enough for 4th test case, it does require more memory than available. You have to use BFS
a
adminModerator
You do use more than 16MB (think about how much stack memory you are using)
s
s-lime
Yes, but think, there are just 1,000,000 fields (maximum, because there are also fences)...
If every call consumes one 32 bit reference, and 3 32 bit longs, then we are there about 16MB. ..
However, i tried changing longs to shorts, but no success...
a
adminModerator
okay, let's see:

you have:
void barvaj(vector<vector <int> > *polje, short x, short y, int color)

that is 4 bytes for polje, 4 bytes for x and y, and 4 bytes for color

also remember for function call, you have to remember the return address, which is another 4 bytes..

that is 16 bytes per function call, so, just your recursion will eat 16M (in worst case)
a
adminModerator
theoretically, DFS can work, if you pack all x, y, and color into one 4byte int. and keep polje as global variable.
s
s-lime
Now, I've updated my function, so that it holds only two shorts (2x16 bit), and one long (32 bit)... So my program should use at most 8MB for function calls, and about 2 MB for table.... Can someone explain why is 2+8>16?
Can someone refer me to a BFS literature?
http://www.z-trening.com/new/www/html/submit.php?submit=7100029399&subm_code=1
a
adminModerator
again, it is not just 2+8

you have 12 per function call (4 bytes for return address) + 2 megs of the table = 14 megs

but!

#include <iostream>
#include <vector>

makes your code memory > 2megs (when statically compiled)