C++ crash after printing 2 dimensional array












-2















After printing a 2-dimensional array, my program crashes and I don't know why. The program crashes before "test2" is printed:



//initialising
int** matrix = new int*[x * y];
for (int i = 0; i < x; i++){
matrix[i] = new int[y];
}
//filling with 0
for (int row = 0; row < x; row++){
for (int cols = 0; cols < y; cols++){
matrix [row][cols] = 0;
}
}
//printing
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j){
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;









share|improve this question




















  • 2





    What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

    – Matthieu Brucher
    Nov 18 '18 at 16:04






  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Matthieu Brucher
    Nov 18 '18 at 16:06











  • FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

    – user1118321
    Nov 18 '18 at 16:30











  • @user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

    – Bo R
    Nov 18 '18 at 17:13








  • 1





    Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

    – Bo R
    Nov 18 '18 at 17:20


















-2















After printing a 2-dimensional array, my program crashes and I don't know why. The program crashes before "test2" is printed:



//initialising
int** matrix = new int*[x * y];
for (int i = 0; i < x; i++){
matrix[i] = new int[y];
}
//filling with 0
for (int row = 0; row < x; row++){
for (int cols = 0; cols < y; cols++){
matrix [row][cols] = 0;
}
}
//printing
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j){
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;









share|improve this question




















  • 2





    What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

    – Matthieu Brucher
    Nov 18 '18 at 16:04






  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Matthieu Brucher
    Nov 18 '18 at 16:06











  • FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

    – user1118321
    Nov 18 '18 at 16:30











  • @user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

    – Bo R
    Nov 18 '18 at 17:13








  • 1





    Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

    – Bo R
    Nov 18 '18 at 17:20
















-2












-2








-2








After printing a 2-dimensional array, my program crashes and I don't know why. The program crashes before "test2" is printed:



//initialising
int** matrix = new int*[x * y];
for (int i = 0; i < x; i++){
matrix[i] = new int[y];
}
//filling with 0
for (int row = 0; row < x; row++){
for (int cols = 0; cols < y; cols++){
matrix [row][cols] = 0;
}
}
//printing
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j){
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;









share|improve this question
















After printing a 2-dimensional array, my program crashes and I don't know why. The program crashes before "test2" is printed:



//initialising
int** matrix = new int*[x * y];
for (int i = 0; i < x; i++){
matrix[i] = new int[y];
}
//filling with 0
for (int row = 0; row < x; row++){
for (int cols = 0; cols < y; cols++){
matrix [row][cols] = 0;
}
}
//printing
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j){
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;






c++ matrix multidimensional-array crash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 23:54









Shrikanth N

510211




510211










asked Nov 18 '18 at 16:00









B.abyfaceB.abyface

43




43








  • 2





    What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

    – Matthieu Brucher
    Nov 18 '18 at 16:04






  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Matthieu Brucher
    Nov 18 '18 at 16:06











  • FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

    – user1118321
    Nov 18 '18 at 16:30











  • @user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

    – Bo R
    Nov 18 '18 at 17:13








  • 1





    Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

    – Bo R
    Nov 18 '18 at 17:20
















  • 2





    What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

    – Matthieu Brucher
    Nov 18 '18 at 16:04






  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Matthieu Brucher
    Nov 18 '18 at 16:06











  • FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

    – user1118321
    Nov 18 '18 at 16:30











  • @user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

    – Bo R
    Nov 18 '18 at 17:13








  • 1





    Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

    – Bo R
    Nov 18 '18 at 17:20










2




2





What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

– Matthieu Brucher
Nov 18 '18 at 16:04





What's time? And size? Why are you allocating int** matrix = new int*[x*y]; but then only allocate x subpointers?

– Matthieu Brucher
Nov 18 '18 at 16:04




1




1





Possible duplicate of How do I declare a 2d array in C++ using new?

– Matthieu Brucher
Nov 18 '18 at 16:06





Possible duplicate of How do I declare a 2d array in C++ using new?

– Matthieu Brucher
Nov 18 '18 at 16:06













FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

– user1118321
Nov 18 '18 at 16:30





FWIW, When I build and run the code you have posted (supplying my own values for x and y), it runs without issue. I also ran the static analyzer on it and no issues were found. I ran it using Address Sanitizer and it did not cause any issues. I ran it using Guard Malloc, Malloc Scribble, and Malloc Guard Edges together and no issues were found. It would seem the problem isn't with the above cod snippet, so perhaps some more context would reveal the problem?

– user1118321
Nov 18 '18 at 16:30













@user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

– Bo R
Nov 18 '18 at 17:13







@user1118321 I too ran the address sanitizer and Valgrind memtool. Both reported memory leaks.

– Bo R
Nov 18 '18 at 17:13






1




1





Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

– Bo R
Nov 18 '18 at 17:20







Your initilizing code is allocating too much. Should be int **matrix = new int *[x]; (instead of x*y). And hopefully you are deallocating later on. for (int i = 0; i < x; i++) delete matrix[i]; delete matrix;

– Bo R
Nov 18 '18 at 17:20














1 Answer
1






active

oldest

votes


















1














 #include<iostream>

void func(int x, int y)
{
// initialising
int **matrix = new int *[x];
for (int i = 0; i < x; i++)
{
matrix[i] = new int[y];
}
// filling with 0
for (int row = 0; row < x; row++)
{
for (int cols = 0; cols < y; cols++)
{
matrix[row][cols] = 0;
}
}
// printing
for (int i = 0; i < (x); ++i)
{
for (int j = 0; j < (y); ++j)
{
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;

for(int i = 0; i < x; i++)
deletematrix[i]; // clean up each y

deletematrix; // clean up x
}

int main()
{
func(5, 5);
}


your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new you must call delete on each pointer allocated by new to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn






share|improve this answer


























  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

    – user1118321
    Nov 18 '18 at 17:52











  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

    – johnathan
    Nov 18 '18 at 17:59













  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

    – user1118321
    Nov 18 '18 at 18:00











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',
autoActivateHeartbeat: false,
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%2f53362815%2fc-crash-after-printing-2-dimensional-array%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









1














 #include<iostream>

void func(int x, int y)
{
// initialising
int **matrix = new int *[x];
for (int i = 0; i < x; i++)
{
matrix[i] = new int[y];
}
// filling with 0
for (int row = 0; row < x; row++)
{
for (int cols = 0; cols < y; cols++)
{
matrix[row][cols] = 0;
}
}
// printing
for (int i = 0; i < (x); ++i)
{
for (int j = 0; j < (y); ++j)
{
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;

for(int i = 0; i < x; i++)
deletematrix[i]; // clean up each y

deletematrix; // clean up x
}

int main()
{
func(5, 5);
}


your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new you must call delete on each pointer allocated by new to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn






share|improve this answer


























  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

    – user1118321
    Nov 18 '18 at 17:52











  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

    – johnathan
    Nov 18 '18 at 17:59













  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

    – user1118321
    Nov 18 '18 at 18:00
















1














 #include<iostream>

void func(int x, int y)
{
// initialising
int **matrix = new int *[x];
for (int i = 0; i < x; i++)
{
matrix[i] = new int[y];
}
// filling with 0
for (int row = 0; row < x; row++)
{
for (int cols = 0; cols < y; cols++)
{
matrix[row][cols] = 0;
}
}
// printing
for (int i = 0; i < (x); ++i)
{
for (int j = 0; j < (y); ++j)
{
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;

for(int i = 0; i < x; i++)
deletematrix[i]; // clean up each y

deletematrix; // clean up x
}

int main()
{
func(5, 5);
}


your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new you must call delete on each pointer allocated by new to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn






share|improve this answer


























  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

    – user1118321
    Nov 18 '18 at 17:52











  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

    – johnathan
    Nov 18 '18 at 17:59













  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

    – user1118321
    Nov 18 '18 at 18:00














1












1








1







 #include<iostream>

void func(int x, int y)
{
// initialising
int **matrix = new int *[x];
for (int i = 0; i < x; i++)
{
matrix[i] = new int[y];
}
// filling with 0
for (int row = 0; row < x; row++)
{
for (int cols = 0; cols < y; cols++)
{
matrix[row][cols] = 0;
}
}
// printing
for (int i = 0; i < (x); ++i)
{
for (int j = 0; j < (y); ++j)
{
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;

for(int i = 0; i < x; i++)
deletematrix[i]; // clean up each y

deletematrix; // clean up x
}

int main()
{
func(5, 5);
}


your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new you must call delete on each pointer allocated by new to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn






share|improve this answer















 #include<iostream>

void func(int x, int y)
{
// initialising
int **matrix = new int *[x];
for (int i = 0; i < x; i++)
{
matrix[i] = new int[y];
}
// filling with 0
for (int row = 0; row < x; row++)
{
for (int cols = 0; cols < y; cols++)
{
matrix[row][cols] = 0;
}
}
// printing
for (int i = 0; i < (x); ++i)
{
for (int j = 0; j < (y); ++j)
{
std::cout << (matrix[i][j]) << ", ";
}
std::cout << std::endl;
}

std::cout << "test2" << std::endl;

for(int i = 0; i < x; i++)
deletematrix[i]; // clean up each y

deletematrix; // clean up x
}

int main()
{
func(5, 5);
}


your x array only needs to be x long. each of your x pointers point to an array that is y long. when calling new you must call delete on each pointer allocated by new to prevent memory leaks. Here is verification of the code https://ideone.com/UL2IJn







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 19:52

























answered Nov 18 '18 at 17:45









johnathanjohnathan

2,158819




2,158819













  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

    – user1118321
    Nov 18 '18 at 17:52











  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

    – johnathan
    Nov 18 '18 at 17:59













  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

    – user1118321
    Nov 18 '18 at 18:00



















  • While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

    – user1118321
    Nov 18 '18 at 17:52











  • @user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

    – johnathan
    Nov 18 '18 at 17:59













  • Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

    – user1118321
    Nov 18 '18 at 18:00

















While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

– user1118321
Nov 18 '18 at 17:52





While this is all true, it's not obvious to me that any of this was the cause of the crash. While the array had space for a bunch of extra pointers that weren't allocated, it also didn't ever read or write to them. So what caused the crash?

– user1118321
Nov 18 '18 at 17:52













@user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

– johnathan
Nov 18 '18 at 17:59







@user1118321 i do not have access to the OP's machine. Programatically there is nothing that, given the narrow scope of their snippet, would indicate an exception would be thrown other than potentially std::bad_alloc, which is why i also supplied a reasonable matrix size of 5 by 5.

– johnathan
Nov 18 '18 at 17:59















Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

– user1118321
Nov 18 '18 at 18:00





Thanks @johnathan. Just wanted to make sure I wasn't missing something in what you wrote. Things like this intrigue me. :-)

– user1118321
Nov 18 '18 at 18:00




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362815%2fc-crash-after-printing-2-dimensional-array%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