Bad memory allocation in vector
up vote
3
down vote
favorite
Have a look at this code:
int main()
{
int m;
cin >> m;
vector<int> cnt(m +1,0);
}
Now if i take m=999999298(which is an int,right?). Why am I getting an"bad memory allocation" error in the vector?.
c++ vector memory-management
|
show 1 more comment
up vote
3
down vote
favorite
Have a look at this code:
int main()
{
int m;
cin >> m;
vector<int> cnt(m +1,0);
}
Now if i take m=999999298(which is an int,right?). Why am I getting an"bad memory allocation" error in the vector?.
c++ vector memory-management
5
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.
– Scheff
Nov 9 at 8:12
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54
|
show 1 more comment
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Have a look at this code:
int main()
{
int m;
cin >> m;
vector<int> cnt(m +1,0);
}
Now if i take m=999999298(which is an int,right?). Why am I getting an"bad memory allocation" error in the vector?.
c++ vector memory-management
Have a look at this code:
int main()
{
int m;
cin >> m;
vector<int> cnt(m +1,0);
}
Now if i take m=999999298(which is an int,right?). Why am I getting an"bad memory allocation" error in the vector?.
c++ vector memory-management
c++ vector memory-management
asked Nov 9 at 8:05
nicks_4317
506
506
5
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.
– Scheff
Nov 9 at 8:12
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54
|
show 1 more comment
5
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.
– Scheff
Nov 9 at 8:12
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54
5
5
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.– Scheff
Nov 9 at 8:12
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.– Scheff
Nov 9 at 8:12
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
vector<int> cnt(m +1,0);
The declaration of vector
you have tries to allocate 999999299 integer elements each of which has value 0. Considering the size of integer as 4 bytes, this is about 3.7 GB of memory. It appears that your application is not allowed that much memory. That is why you get the "bad memory allocation" error.
As to why there is such a limit, you can read this question and its answers.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
vector<int> cnt(m +1,0);
The declaration of vector
you have tries to allocate 999999299 integer elements each of which has value 0. Considering the size of integer as 4 bytes, this is about 3.7 GB of memory. It appears that your application is not allowed that much memory. That is why you get the "bad memory allocation" error.
As to why there is such a limit, you can read this question and its answers.
add a comment |
up vote
8
down vote
accepted
vector<int> cnt(m +1,0);
The declaration of vector
you have tries to allocate 999999299 integer elements each of which has value 0. Considering the size of integer as 4 bytes, this is about 3.7 GB of memory. It appears that your application is not allowed that much memory. That is why you get the "bad memory allocation" error.
As to why there is such a limit, you can read this question and its answers.
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
vector<int> cnt(m +1,0);
The declaration of vector
you have tries to allocate 999999299 integer elements each of which has value 0. Considering the size of integer as 4 bytes, this is about 3.7 GB of memory. It appears that your application is not allowed that much memory. That is why you get the "bad memory allocation" error.
As to why there is such a limit, you can read this question and its answers.
vector<int> cnt(m +1,0);
The declaration of vector
you have tries to allocate 999999299 integer elements each of which has value 0. Considering the size of integer as 4 bytes, this is about 3.7 GB of memory. It appears that your application is not allowed that much memory. That is why you get the "bad memory allocation" error.
As to why there is such a limit, you can read this question and its answers.
edited Nov 9 at 8:48
answered Nov 9 at 8:13
P.W
9,9332742
9,9332742
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221889%2fbad-memory-allocation-in-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
are you building a 32-bit application? You are allocating 4GB of memory, you might only be allowed 2GB per application.
– Alan Birtles
Nov 9 at 8:09
What if you take a much smaller value? Does it work then? If so, then what's the threshold at which it stops working?
– Blaze
Nov 9 at 8:11
sizeof (int)
is nowadays usually 4 (32 bit). 999999298 * 4 = 3999997192 Bytes = 3906247.26 KB = 3814.69 MB = 3.72 GB. If you are on 32 bit platform this is probably not allocatable. Even on 64 bit, there might be not enough contiguous memory available.– Scheff
Nov 9 at 8:12
@Scheff: That's theoretically possible, of course, but it would require 4 billion prior allocations with 3.71 GB holes in between them.
– MSalters
Nov 9 at 11:02
@MSalters OK. Forget about what I told about 64 bit. OP didn't mention OS and platform but stated about bad-alloc issue. So, it's probably a 32 bit platform. (It would be easier if OP could add this info.)
– Scheff
Nov 9 at 11:54