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?.










share|improve this question


















  • 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















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?.










share|improve this question


















  • 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













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?.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 8:48

























        answered Nov 9 at 8:13









        P.W

        9,9332742




        9,9332742






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini