Stack corruption error

Run-Time Check Failure #2 - Stack around the variable 'letters'

Page 1 of 1

5 Replies - 3006 Views - Last Post: 27 November 2008 - 09:31 AM Rate Topic: -----

#1 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Stack corruption error

Posted 27 November 2008 - 08:46 AM

I'm currently trying to complete Problem 59 from Project Euler, but I'm some problems doing the easy part, even though I know how to do the rest. I'm trying to put some comma-separated values into an array, and then print them just to check. It works fine when I try it on relativly small amounts, but as I get higher I get this error:
Run-Time Check Failure #2 - Stack around the variable 'letters' was corrupted.


What is causing this?

Here's my code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{	
	int letters[1200][1];
	string word;
	int numWord = 0;

	ifstream infile("C:/Users/Tom/Desktop/file.csv");
	while (getline(infile, word, ','))
	{
		cout << "Word: " << word << "\n";
		letters[numWord][0] = atoi(word.c_str());;
		numWord++;
	}

	for(int i = 0; i < 1200;i++){
		cout << letters[i][0] << endl;
	}

	system("PAUSE");
	return 0;

}


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Stack corruption error

#2 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Stack corruption error

Posted 27 November 2008 - 08:51 AM

I had this error with a char string, and it's because you're reaching beyond the array limit when writing to the array.


int letters[1200][1];


What's the point in including the [1]. An array of size 1 is not an array, it's a variable.

This post has been edited by Pwn: 27 November 2008 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Stack corruption error

Posted 27 November 2008 - 08:52 AM

Are you sure you aren't overrunning your array bounds? That is a common cause of stack corruption.

//overwriting array bounds up top but also:

 for(int i = 0; i < 1200;i++){ //problem is also here [1200] is 0-1999
        cout << letters[i][0] << endl;
    }


This post has been edited by KYA: 27 November 2008 - 08:53 AM

Was This Post Helpful? 1
  • +
  • -

#4 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Stack corruption error

Posted 27 November 2008 - 08:54 AM

Thanks very much, that fixed it. I must have made a mistake when counting the variables before.

And Pwn, I was assuming that the [1] made it a 1201 x 2 array, which I shall be using later.

This post has been edited by greatdragon: 27 November 2008 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Stack corruption error

Posted 27 November 2008 - 09:20 AM

View Postgreatdragon, on 27 Nov, 2008 - 07:54 AM, said:

I was assuming that the [1] made it a 1201 x 2 array.


When declaring an array, you put the size in brackets. When working with an array, it's uses a zero-indexed scheme, which means 0 is the first index of the array. So a size 2 array (array[2]) will have 2 variables, array[0] and array[1]. Some other programming languages have or used to have the option to start your index at 0, or 1 or something else. This was hard to follow and led to lots of run-time errors, so the standards committee decided to simplify it and require most languages to only have zero-indexed arrays.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Stack corruption error

Posted 27 November 2008 - 09:31 AM

arr[1200][2]; //0-1999, 0-1


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1