Why is in the following similar codes one giving runtime error and other is not











up vote
0
down vote

favorite












Well this is really weird

Consider the following two codes :-



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];

for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}

for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}


AND



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];




cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;


}


The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .










share|improve this question


















  • 1




    int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
    – Ruks
    22 hours ago












  • @Ruks well it's correct, I suppose as its an array of pointers
    – Ayush Mangal
    22 hours ago












  • what's the error message?
    – rsjaffe
    22 hours ago






  • 1




    Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
    – Ruks
    21 hours ago








  • 1




    Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
    – user4581301
    21 hours ago















up vote
0
down vote

favorite












Well this is really weird

Consider the following two codes :-



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];

for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}

for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}


AND



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];




cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;


}


The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .










share|improve this question


















  • 1




    int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
    – Ruks
    22 hours ago












  • @Ruks well it's correct, I suppose as its an array of pointers
    – Ayush Mangal
    22 hours ago












  • what's the error message?
    – rsjaffe
    22 hours ago






  • 1




    Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
    – Ruks
    21 hours ago








  • 1




    Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
    – user4581301
    21 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Well this is really weird

Consider the following two codes :-



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];

for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}

for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}


AND



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];




cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;


}


The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .










share|improve this question













Well this is really weird

Consider the following two codes :-



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];

for(int i = 0; i < 5; i++)
{
*ptr[i]=a[i];
}

for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}


AND



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];
*ptr[3]=a[3];
*ptr[4]=a[4];




cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;
cout<<*(ptr[3])<<endl;
cout<<*(ptr[4])<<endl;


}


The first one gives runtime errorr , while the seconde one gives 1 ,2 ,3 ,4 ,5 output , I can't find the difference between the two codes , can anyone help me to find the difference .







c++ runtime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 22 hours ago









Ayush Mangal

94




94








  • 1




    int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
    – Ruks
    22 hours ago












  • @Ruks well it's correct, I suppose as its an array of pointers
    – Ayush Mangal
    22 hours ago












  • what's the error message?
    – rsjaffe
    22 hours ago






  • 1




    Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
    – Ruks
    21 hours ago








  • 1




    Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
    – user4581301
    21 hours ago














  • 1




    int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
    – Ruks
    22 hours ago












  • @Ruks well it's correct, I suppose as its an array of pointers
    – Ayush Mangal
    22 hours ago












  • what's the error message?
    – rsjaffe
    22 hours ago






  • 1




    Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
    – Ruks
    21 hours ago








  • 1




    Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
    – user4581301
    21 hours ago








1




1




int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
– Ruks
22 hours ago






int* ptr[5]; doesn't seem right to me... Shouldn't it be int ptr[5];? Well if you really like to stick with pointers then you SHOULD do *(ptr[i]) etc. etc...
– Ruks
22 hours ago














@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago






@Ruks well it's correct, I suppose as its an array of pointers
– Ayush Mangal
22 hours ago














what's the error message?
– rsjaffe
22 hours ago




what's the error message?
– rsjaffe
22 hours ago




1




1




Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
– Ruks
21 hours ago






Simple answer, You have not allocated the pointer... So ptr[i] = &a[i] is the correct answer...
– Ruks
21 hours ago






1




1




Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago




Or with less sarcasm, nothing says bad code that invokes Undefined behaviour will cause a crash.
– user4581301
21 hours ago












3 Answers
3






active

oldest

votes

















up vote
0
down vote













#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];





cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;




}


the above code runs fine and gives output 1, 2
but



#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];


*ptr[0]=a[0];
*ptr[1]=a[1];
*ptr[2]=a[2];




cout<<*(ptr[0])<<endl;
cout<<*(ptr[1])<<endl;
cout<<*(ptr[2])<<endl;




}


this code gives runtime error on codeblocks(gcc) , I am just getting more confused






share|improve this answer





















  • I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
    – Ruks
    21 hours ago












  • yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
    – Ayush Mangal
    21 hours ago










  • Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
    – Ruks
    21 hours ago












  • its showing gcc 5.1.0
    – Ayush Mangal
    21 hours ago










  • Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
    – Ruks
    21 hours ago




















up vote
0
down vote













I have both runtime errors.
The result of recoding with the pointer variable below.





#include <iostream>
using namespace std;

int main() {
int a={1,2,3,4,5};
int* ptr[5];

for(int i = 0; i < 5; i++)
{
ptr[i]=&a[i];
}

for(int i = 0; i < 5; i++)
{
cout<<*(ptr[i]);
}
}





share|improve this answer










New contributor




JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • From the question, it looks like the OP wants an array of pointers...
    – Ruks
    21 hours ago










  • yes I do want an array of pointers
    – Ayush Mangal
    21 hours ago










  • I modified to pointer array
    – JY Lee
    21 hours ago


















up vote
0
down vote













It is already established that



*ptr[0] = a[0];


with unallocated pointer is undefined behavior.



The solution



ptr[0] = &a[0];


is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want



ptr[0] = new int(a[0]);


This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).



#include <memory>
#include <vector>
#include <iostream>

int main() {
int a={1,2,3,4,5};
std::vector<std::unique_ptr<int>> ptrs;

for(auto i: a)
{
ptrs.emplace_back(std::make_unique<int>(i));
}

for(auto& i: ptrs)
{
std::cout << *i;
}
}





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%2f53183951%2fwhy-is-in-the-following-similar-codes-one-giving-runtime-error-and-other-is-not%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];





    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;




    }


    the above code runs fine and gives output 1, 2
    but



    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];
    *ptr[2]=a[2];




    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;
    cout<<*(ptr[2])<<endl;




    }


    this code gives runtime error on codeblocks(gcc) , I am just getting more confused






    share|improve this answer





















    • I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
      – Ruks
      21 hours ago












    • yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
      – Ayush Mangal
      21 hours ago










    • Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
      – Ruks
      21 hours ago












    • its showing gcc 5.1.0
      – Ayush Mangal
      21 hours ago










    • Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
      – Ruks
      21 hours ago

















    up vote
    0
    down vote













    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];





    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;




    }


    the above code runs fine and gives output 1, 2
    but



    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];
    *ptr[2]=a[2];




    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;
    cout<<*(ptr[2])<<endl;




    }


    this code gives runtime error on codeblocks(gcc) , I am just getting more confused






    share|improve this answer





















    • I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
      – Ruks
      21 hours ago












    • yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
      – Ayush Mangal
      21 hours ago










    • Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
      – Ruks
      21 hours ago












    • its showing gcc 5.1.0
      – Ayush Mangal
      21 hours ago










    • Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
      – Ruks
      21 hours ago















    up vote
    0
    down vote










    up vote
    0
    down vote









    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];





    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;




    }


    the above code runs fine and gives output 1, 2
    but



    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];
    *ptr[2]=a[2];




    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;
    cout<<*(ptr[2])<<endl;




    }


    this code gives runtime error on codeblocks(gcc) , I am just getting more confused






    share|improve this answer












    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];





    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;




    }


    the above code runs fine and gives output 1, 2
    but



    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];


    *ptr[0]=a[0];
    *ptr[1]=a[1];
    *ptr[2]=a[2];




    cout<<*(ptr[0])<<endl;
    cout<<*(ptr[1])<<endl;
    cout<<*(ptr[2])<<endl;




    }


    this code gives runtime error on codeblocks(gcc) , I am just getting more confused







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 21 hours ago









    Ayush Mangal

    94




    94












    • I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
      – Ruks
      21 hours ago












    • yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
      – Ayush Mangal
      21 hours ago










    • Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
      – Ruks
      21 hours ago












    • its showing gcc 5.1.0
      – Ayush Mangal
      21 hours ago










    • Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
      – Ruks
      21 hours ago




















    • I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
      – Ruks
      21 hours ago












    • yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
      – Ayush Mangal
      21 hours ago










    • Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
      – Ruks
      21 hours ago












    • its showing gcc 5.1.0
      – Ayush Mangal
      21 hours ago










    • Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
      – Ruks
      21 hours ago


















    I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
    – Ruks
    21 hours ago






    I think your compiler is broken... what you are trying to do is illegal! You should NEVER dereference a pointer without allocating it!!
    – Ruks
    21 hours ago














    yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
    – Ayush Mangal
    21 hours ago




    yes I also know , but using such a standard GNU GCC compiler shouldn't have a bug , and even IDEONE websites compiler is having a problem then
    – Ayush Mangal
    21 hours ago












    Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
    – Ruks
    21 hours ago






    Which version of GNU GCC is it? Looks like they allocate the pointer automatically somehow?... Look at this if you think I am wrong...
    – Ruks
    21 hours ago














    its showing gcc 5.1.0
    – Ayush Mangal
    21 hours ago




    its showing gcc 5.1.0
    – Ayush Mangal
    21 hours ago












    Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
    – Ruks
    21 hours ago






    Tried your first code in WandBox and set GCC version to 5.1.0 and it gives segmentation fault...
    – Ruks
    21 hours ago














    up vote
    0
    down vote













    I have both runtime errors.
    The result of recoding with the pointer variable below.





    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];

    for(int i = 0; i < 5; i++)
    {
    ptr[i]=&a[i];
    }

    for(int i = 0; i < 5; i++)
    {
    cout<<*(ptr[i]);
    }
    }





    share|improve this answer










    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • From the question, it looks like the OP wants an array of pointers...
      – Ruks
      21 hours ago










    • yes I do want an array of pointers
      – Ayush Mangal
      21 hours ago










    • I modified to pointer array
      – JY Lee
      21 hours ago















    up vote
    0
    down vote













    I have both runtime errors.
    The result of recoding with the pointer variable below.





    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];

    for(int i = 0; i < 5; i++)
    {
    ptr[i]=&a[i];
    }

    for(int i = 0; i < 5; i++)
    {
    cout<<*(ptr[i]);
    }
    }





    share|improve this answer










    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • From the question, it looks like the OP wants an array of pointers...
      – Ruks
      21 hours ago










    • yes I do want an array of pointers
      – Ayush Mangal
      21 hours ago










    • I modified to pointer array
      – JY Lee
      21 hours ago













    up vote
    0
    down vote










    up vote
    0
    down vote









    I have both runtime errors.
    The result of recoding with the pointer variable below.





    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];

    for(int i = 0; i < 5; i++)
    {
    ptr[i]=&a[i];
    }

    for(int i = 0; i < 5; i++)
    {
    cout<<*(ptr[i]);
    }
    }





    share|improve this answer










    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    I have both runtime errors.
    The result of recoding with the pointer variable below.





    #include <iostream>
    using namespace std;

    int main() {
    int a={1,2,3,4,5};
    int* ptr[5];

    for(int i = 0; i < 5; i++)
    {
    ptr[i]=&a[i];
    }

    for(int i = 0; i < 5; i++)
    {
    cout<<*(ptr[i]);
    }
    }






    share|improve this answer










    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer








    edited 21 hours ago





















    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 21 hours ago









    JY Lee

    112




    112




    New contributor




    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    JY Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.












    • From the question, it looks like the OP wants an array of pointers...
      – Ruks
      21 hours ago










    • yes I do want an array of pointers
      – Ayush Mangal
      21 hours ago










    • I modified to pointer array
      – JY Lee
      21 hours ago


















    • From the question, it looks like the OP wants an array of pointers...
      – Ruks
      21 hours ago










    • yes I do want an array of pointers
      – Ayush Mangal
      21 hours ago










    • I modified to pointer array
      – JY Lee
      21 hours ago
















    From the question, it looks like the OP wants an array of pointers...
    – Ruks
    21 hours ago




    From the question, it looks like the OP wants an array of pointers...
    – Ruks
    21 hours ago












    yes I do want an array of pointers
    – Ayush Mangal
    21 hours ago




    yes I do want an array of pointers
    – Ayush Mangal
    21 hours ago












    I modified to pointer array
    – JY Lee
    21 hours ago




    I modified to pointer array
    – JY Lee
    21 hours ago










    up vote
    0
    down vote













    It is already established that



    *ptr[0] = a[0];


    with unallocated pointer is undefined behavior.



    The solution



    ptr[0] = &a[0];


    is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want



    ptr[0] = new int(a[0]);


    This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).



    #include <memory>
    #include <vector>
    #include <iostream>

    int main() {
    int a={1,2,3,4,5};
    std::vector<std::unique_ptr<int>> ptrs;

    for(auto i: a)
    {
    ptrs.emplace_back(std::make_unique<int>(i));
    }

    for(auto& i: ptrs)
    {
    std::cout << *i;
    }
    }





    share|improve this answer

























      up vote
      0
      down vote













      It is already established that



      *ptr[0] = a[0];


      with unallocated pointer is undefined behavior.



      The solution



      ptr[0] = &a[0];


      is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want



      ptr[0] = new int(a[0]);


      This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).



      #include <memory>
      #include <vector>
      #include <iostream>

      int main() {
      int a={1,2,3,4,5};
      std::vector<std::unique_ptr<int>> ptrs;

      for(auto i: a)
      {
      ptrs.emplace_back(std::make_unique<int>(i));
      }

      for(auto& i: ptrs)
      {
      std::cout << *i;
      }
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        It is already established that



        *ptr[0] = a[0];


        with unallocated pointer is undefined behavior.



        The solution



        ptr[0] = &a[0];


        is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want



        ptr[0] = new int(a[0]);


        This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).



        #include <memory>
        #include <vector>
        #include <iostream>

        int main() {
        int a={1,2,3,4,5};
        std::vector<std::unique_ptr<int>> ptrs;

        for(auto i: a)
        {
        ptrs.emplace_back(std::make_unique<int>(i));
        }

        for(auto& i: ptrs)
        {
        std::cout << *i;
        }
        }





        share|improve this answer












        It is already established that



        *ptr[0] = a[0];


        with unallocated pointer is undefined behavior.



        The solution



        ptr[0] = &a[0];


        is also already given. This works, but it is limited because now, the ptr array is tied to a, which you not always want. In that case you might want



        ptr[0] = new int(a[0]);


        This initializes the array with values from a but keeps it independent. However, this introduces manual memory management and this can become tricky in more complex code (memory leaks, double deallocations, dangling pointers), so a more C++ like solution would be (assuming the pointers point to something more interesting than an integer, because now there seems to be no need for pointers).



        #include <memory>
        #include <vector>
        #include <iostream>

        int main() {
        int a={1,2,3,4,5};
        std::vector<std::unique_ptr<int>> ptrs;

        for(auto i: a)
        {
        ptrs.emplace_back(std::make_unique<int>(i));
        }

        for(auto& i: ptrs)
        {
        std::cout << *i;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 18 hours ago









        stefaanv

        10.8k12339




        10.8k12339






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183951%2fwhy-is-in-the-following-similar-codes-one-giving-runtime-error-and-other-is-not%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            横浜市

            Rostock

            Europa