1

Given the following code:

vector<int> A[1000000];
for( int i = 0; i < 1000000; i++ ){
    A[i].clear();
}

I am running it on an automated terminal that has test cases run with my code, so I can't get full debug messages. I don't get any errors, and the program seems to run fine with single test cases. However when I run against the full set I pass/fail test cases randomly (one time I may pass the first 3, another I pass everything but them, another I only pass the second, etc). I want to make sure this kind of initialization would get rid of "undefined" values on the array (eg char *buffer = new char[100]; buffer[0] = 0;) so that it is not a cause of random crashes.

sizeof(A) == 24000000 (~23MB), sizeof(int) == 4. I only add up to 1 million integers to these vectors (in total), so in the worst tests, each vector could be a one element vector, or a single vector could have 1 million integers, while the rest remains empty.

Do I have to call a destructor? I assumed not since I never called new, but I'm new to STL. Finally it could be not an issue with my code but the tester, but still, want to make sure this is fine on my side.

7
  • 1
    This code is OK, there could be a bug elsewhere. Or a stack overflow Commented Jan 10, 2017 at 5:10
  • Have you tried, vector<int> A(1000000,0);? This will fill the vector with zeros. Commented Jan 10, 2017 at 5:13
  • @tinstaafl that is a single vector, OP code makes a million vectors (which I assume is intentional) Commented Jan 10, 2017 at 5:17
  • 1
    check for sizeof(int) in your compiler, if it's 2 byte then the for loop is culprit. Commented Jan 10, 2017 at 5:19
  • @M.M Yes. it is 1 million vectors (that will hold 1 million ints between all of them, so that shouldnt be the cause of an out of memory), not a single 1 million element vector sadly. I'll guess its the tester code then... Commented Jan 10, 2017 at 5:20

2 Answers 2

6

I think this is a stack overflow, as M.M suggested. The line

std::vector<int> A[1000000];

creates a million vector objects on the stack, which is too much. On my machine, the example failed for one million vectors, but it worked fine for one thousand.

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

7 Comments

Are you sure? sizeof(A) returns 24000000 to me, thats only 23MB I will only add 1 million integers to these vectors (in total), so each vector could be a one element vector, or a single vector could have 1 million, while the rest remains empty.
Default stack size in the Visual Studio for example is 1 MB. Create your array of vectors on the heap using new operator instead.
don't use new ... vector of vectors, or unique_ptr of vector array would both be preferable
23MB is certainly too big for the stack, so you have to allocate on the heap. I would try to avoid using new in this case. Instead, you could use a vector of vectors: std::vector<std::vector<int> > A(1000000);.
@cthl well it is possible to modify stack size to be in the gigabytes. Seems likely to exceed common compilers' default stack size though.
|
0

In place of for loop use below code

std::fill(A.begin(), A.end(), 0);

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.