c++ “Run-Time Check Failure #2 - Stack around the variable was corrupted” error while reading values from...












1















I am trying to read values from a ppm image file and i get "Run-Time Check Failure #2 - Stack around the variable 'numbers' was corrupted" error. I am new to c++ and programming in general and i dont understand what is the problem, can someone help?



I dont know if it is important, but the code i have written in main i will later transfer in a method that will perform this task( i will do all the necessery adjustments), is a similar error going to occur inside the method?



(the code is written in visual studio in debug mode)



int main(int argc, char *argv) {

ifstream imagefile(argv[1], ios::binary);

if (!imagefile.is_open()) {
printf("error opening");
return 0;
}

string values[4];
int x;
for (int i = 0; i < 4; i++) {
string str;
char c = imagefile.get();

while (!isspace(static_cast<unsigned char>(c))) {
str = str + c;
c = imagefile.get();
if (str.size() > 10000) {
cout << "wrong" << endl; //for testing
goto stop;
}
}
x=i;
cout << x << endl; //for testing
values[i] = str;
cout << str << endl; //for testing
}
stop:
if (!values[0].compare("P6") == 0 || x!=3) {
cout << "wrong format" << endl;
system("pause");
return 0;
}

int numbers[3];
for (int i= 1; i <4; i++) {
stringstream str; //to convert string to integer
str << values[i];
int number;
str >> number;
numbers[i] = number;
cout << numbers[i] +1<< endl; //for testing
}

int start = imagefile.tellg();
imagefile.seekg(0, ios::end);
int end = imagefile.tellg();
int size = end - start;
imagefile.seekg(start, ios::beg);
float *pixels = new float[size];
for (int i = 0; i < size; i++) {
int c = imagefile.get();
float f = c / 255.0;
pixels[i] = f;
}

system("pause");

return 0;


}










share|improve this question























  • int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

    – drescherjm
    Nov 20 '18 at 15:17













  • Thankfully your run time tells you exactly where to look.

    – drescherjm
    Nov 20 '18 at 15:20











  • @drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

    – Yastanub
    Nov 20 '18 at 15:21











  • Yes sorry fixed.

    – drescherjm
    Nov 20 '18 at 15:21











  • thank you very much! i found the way to fix it

    – JDoe
    Nov 20 '18 at 15:26
















1















I am trying to read values from a ppm image file and i get "Run-Time Check Failure #2 - Stack around the variable 'numbers' was corrupted" error. I am new to c++ and programming in general and i dont understand what is the problem, can someone help?



I dont know if it is important, but the code i have written in main i will later transfer in a method that will perform this task( i will do all the necessery adjustments), is a similar error going to occur inside the method?



(the code is written in visual studio in debug mode)



int main(int argc, char *argv) {

ifstream imagefile(argv[1], ios::binary);

if (!imagefile.is_open()) {
printf("error opening");
return 0;
}

string values[4];
int x;
for (int i = 0; i < 4; i++) {
string str;
char c = imagefile.get();

while (!isspace(static_cast<unsigned char>(c))) {
str = str + c;
c = imagefile.get();
if (str.size() > 10000) {
cout << "wrong" << endl; //for testing
goto stop;
}
}
x=i;
cout << x << endl; //for testing
values[i] = str;
cout << str << endl; //for testing
}
stop:
if (!values[0].compare("P6") == 0 || x!=3) {
cout << "wrong format" << endl;
system("pause");
return 0;
}

int numbers[3];
for (int i= 1; i <4; i++) {
stringstream str; //to convert string to integer
str << values[i];
int number;
str >> number;
numbers[i] = number;
cout << numbers[i] +1<< endl; //for testing
}

int start = imagefile.tellg();
imagefile.seekg(0, ios::end);
int end = imagefile.tellg();
int size = end - start;
imagefile.seekg(start, ios::beg);
float *pixels = new float[size];
for (int i = 0; i < size; i++) {
int c = imagefile.get();
float f = c / 255.0;
pixels[i] = f;
}

system("pause");

return 0;


}










share|improve this question























  • int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

    – drescherjm
    Nov 20 '18 at 15:17













  • Thankfully your run time tells you exactly where to look.

    – drescherjm
    Nov 20 '18 at 15:20











  • @drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

    – Yastanub
    Nov 20 '18 at 15:21











  • Yes sorry fixed.

    – drescherjm
    Nov 20 '18 at 15:21











  • thank you very much! i found the way to fix it

    – JDoe
    Nov 20 '18 at 15:26














1












1








1








I am trying to read values from a ppm image file and i get "Run-Time Check Failure #2 - Stack around the variable 'numbers' was corrupted" error. I am new to c++ and programming in general and i dont understand what is the problem, can someone help?



I dont know if it is important, but the code i have written in main i will later transfer in a method that will perform this task( i will do all the necessery adjustments), is a similar error going to occur inside the method?



(the code is written in visual studio in debug mode)



int main(int argc, char *argv) {

ifstream imagefile(argv[1], ios::binary);

if (!imagefile.is_open()) {
printf("error opening");
return 0;
}

string values[4];
int x;
for (int i = 0; i < 4; i++) {
string str;
char c = imagefile.get();

while (!isspace(static_cast<unsigned char>(c))) {
str = str + c;
c = imagefile.get();
if (str.size() > 10000) {
cout << "wrong" << endl; //for testing
goto stop;
}
}
x=i;
cout << x << endl; //for testing
values[i] = str;
cout << str << endl; //for testing
}
stop:
if (!values[0].compare("P6") == 0 || x!=3) {
cout << "wrong format" << endl;
system("pause");
return 0;
}

int numbers[3];
for (int i= 1; i <4; i++) {
stringstream str; //to convert string to integer
str << values[i];
int number;
str >> number;
numbers[i] = number;
cout << numbers[i] +1<< endl; //for testing
}

int start = imagefile.tellg();
imagefile.seekg(0, ios::end);
int end = imagefile.tellg();
int size = end - start;
imagefile.seekg(start, ios::beg);
float *pixels = new float[size];
for (int i = 0; i < size; i++) {
int c = imagefile.get();
float f = c / 255.0;
pixels[i] = f;
}

system("pause");

return 0;


}










share|improve this question














I am trying to read values from a ppm image file and i get "Run-Time Check Failure #2 - Stack around the variable 'numbers' was corrupted" error. I am new to c++ and programming in general and i dont understand what is the problem, can someone help?



I dont know if it is important, but the code i have written in main i will later transfer in a method that will perform this task( i will do all the necessery adjustments), is a similar error going to occur inside the method?



(the code is written in visual studio in debug mode)



int main(int argc, char *argv) {

ifstream imagefile(argv[1], ios::binary);

if (!imagefile.is_open()) {
printf("error opening");
return 0;
}

string values[4];
int x;
for (int i = 0; i < 4; i++) {
string str;
char c = imagefile.get();

while (!isspace(static_cast<unsigned char>(c))) {
str = str + c;
c = imagefile.get();
if (str.size() > 10000) {
cout << "wrong" << endl; //for testing
goto stop;
}
}
x=i;
cout << x << endl; //for testing
values[i] = str;
cout << str << endl; //for testing
}
stop:
if (!values[0].compare("P6") == 0 || x!=3) {
cout << "wrong format" << endl;
system("pause");
return 0;
}

int numbers[3];
for (int i= 1; i <4; i++) {
stringstream str; //to convert string to integer
str << values[i];
int number;
str >> number;
numbers[i] = number;
cout << numbers[i] +1<< endl; //for testing
}

int start = imagefile.tellg();
imagefile.seekg(0, ios::end);
int end = imagefile.tellg();
int size = end - start;
imagefile.seekg(start, ios::beg);
float *pixels = new float[size];
for (int i = 0; i < size; i++) {
int c = imagefile.get();
float f = c / 255.0;
pixels[i] = f;
}

system("pause");

return 0;


}







c++ binary istream ppm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 15:16









JDoeJDoe

74




74













  • int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

    – drescherjm
    Nov 20 '18 at 15:17













  • Thankfully your run time tells you exactly where to look.

    – drescherjm
    Nov 20 '18 at 15:20











  • @drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

    – Yastanub
    Nov 20 '18 at 15:21











  • Yes sorry fixed.

    – drescherjm
    Nov 20 '18 at 15:21











  • thank you very much! i found the way to fix it

    – JDoe
    Nov 20 '18 at 15:26



















  • int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

    – drescherjm
    Nov 20 '18 at 15:17













  • Thankfully your run time tells you exactly where to look.

    – drescherjm
    Nov 20 '18 at 15:20











  • @drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

    – Yastanub
    Nov 20 '18 at 15:21











  • Yes sorry fixed.

    – drescherjm
    Nov 20 '18 at 15:21











  • thank you very much! i found the way to fix it

    – JDoe
    Nov 20 '18 at 15:26

















int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

– drescherjm
Nov 20 '18 at 15:17







int numbers[3]; then for (int i= 1; i <4; i++) { } is a bug. since you access numbers[3]. Note that the valid array indices are 0 to 2 not 1 to 3.

– drescherjm
Nov 20 '18 at 15:17















Thankfully your run time tells you exactly where to look.

– drescherjm
Nov 20 '18 at 15:20





Thankfully your run time tells you exactly where to look.

– drescherjm
Nov 20 '18 at 15:20













@drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

– Yastanub
Nov 20 '18 at 15:21





@drescherjm don't you mean "array indices are from 0 to 2 not from 1 to 3"?

– Yastanub
Nov 20 '18 at 15:21













Yes sorry fixed.

– drescherjm
Nov 20 '18 at 15:21





Yes sorry fixed.

– drescherjm
Nov 20 '18 at 15:21













thank you very much! i found the way to fix it

– JDoe
Nov 20 '18 at 15:26





thank you very much! i found the way to fix it

– JDoe
Nov 20 '18 at 15:26












0






active

oldest

votes











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%2f53396092%2fc-run-time-check-failure-2-stack-around-the-variable-was-corrupted-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53396092%2fc-run-time-check-failure-2-stack-around-the-variable-was-corrupted-error%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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?