Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
lotios611
Chaos Rift Regular
Posts: 160 Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++
Post
by lotios611 » Thu Dec 31, 2009 1:42 pm
Well, I was just screwing around with file loading, and this weird bug happened. I've fixed the bug, but I just wanted to post what happened.
Here's main.cpp:
Code: Select all
#include <string>
#include <fstream>
#include <iostream>
int main()
{
std::string temp[5];
int i = 0;
std::fstream file;
file.open("Dictionary.txt");
if (file.is_open())
{
while (!file.eof())
{
std::getline(file, temp[i++]);
std::cout << temp[i++] << std::endl;
}
file.close();
}
else
{
std::cout << "Unable to load file";
}
return 0;
}
And here's Dictionary.txt:
Code: Select all
Hello - A word
Testing - Testing
APPLES - ANOTHER WORD
banana - a fruit
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
dani93
Chaos Rift Newbie
Posts: 38 Joined: Mon Apr 13, 2009 9:38 am
Location: Austria
Post
by dani93 » Thu Dec 31, 2009 1:51 pm
Your loop is wrong. It should be:
Code: Select all
for (int i = 0; !file.eof(); i++)
{
std::getline(file, temp[i]);
std::cout << temp[i] << std::endl;
}
You got a too high index cause you incremented "i" wrong -> segmentation fault
lotios611
Chaos Rift Regular
Posts: 160 Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++
Post
by lotios611 » Thu Dec 31, 2009 2:00 pm
Like I said, I already fixed it. So that's what a segmentation fault is...
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
short
ES Beta Backer
Posts: 548 Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US
Post
by short » Thu Dec 31, 2009 4:04 pm
lotios611 wrote: Like I said, I already fixed it. So that's what a segmentation fault is...
Segment faults (on the low level) happen when you try and de-reference an invalid pointer. Since arrays are after all pointers, when you try to index that is larger then you declared your array size to be, you are indeed de-referencing an invalid pointer.
lotios611
Chaos Rift Regular
Posts: 160 Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++
Post
by lotios611 » Fri Jan 01, 2010 9:03 am
I actually never bothered to look "Segmentation fault" up. You learn something new every day.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown