what is causing this seemingly simple app to crash?

This is a discussion on what is causing this seemingly simple app to crash? within the C++ Programming forums, part of the General Programming Boards category; what is causing this to crash? Code: #include <iostream> #include <fstream> #include <vector> #include <conio.h> using namespace std; struct vertex ...

  1. #1
    Shadow12345
    Guest

    what is causing this seemingly simple app to crash?

    what is causing this to crash?

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <conio.h>
    
    using namespace std;
    struct vertex {
    	float  x;
    	float  y;
    	float  z;
    };
    struct triangle {
    	vertex v1;
    	vertex v2;
    	vertex v3;
    };
    
    vector<vertex> vertices;
    
    int main(void) {
    int numverts = 1;
    
    cout.setf(ios::fixed);
    char type = 'v';
    float x, y, z;
    ifstream fin;
    fin.open("temp.n3d", ios::in);
    vertices.resize(numverts);
    while(fin.good() && type == 'v') {
    	
    	fin >> type >> x >> y >> z;
    	vertices[numverts].x = x;
    	vertices[numverts].y = y;
    	vertices[numverts].z = z;
    	
    	cout << vertices[numverts].x << " " << vertices[numverts].y << " " << vertices[numverts].z << endl;
    	cout << type << endl;
    	cout << x << " " << y << " " << z << endl;
    	numverts++;
    	vertices.resize(numverts);
    }
    
    getch();
    return 0;
    
    }
    it makes me cry, this seems so simple too

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because you're doing:

    vertices[numverts]

    which is going to be 1 past the end of the vector

    IE

    int Array[5];

    Array[5]; // This is one past the end cuz counting starts at 0.

    Also, it's not too wise to resize the vector on every single iteration of the loop. Every time you resize the program is internally reallocating a new array and copying all of the previous data into it. It'll slow things down a lot. You're better off putting the amount of vertices there are at the top of the file you load and then resize once to that amount.

  3. #3
    Shadow12345
    Guest
    this doesn't work either, they both compile fine, but both give me run time errors and im not exactly sure why

    Code:
    int main(void) {
    int numverts = 1;
    
    cout.setf(ios::fixed);
    char type = 'v';
    float x, y, z;
    FILE *file = fopen("temp.ncb","rb");
    vertices.resize(numverts);
    
    while(type == 'v') {
    	fread(&type, sizeof(char), 1, file);
    	fread(&vertices[numverts], sizeof(vertex), 1, file);	
    }
    
    getch();
    return 0;
    
    }

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    that's because you didn't do what i said:

    you're still doing:

    vertices[numverts]

    Which is trying to access data that's out of bounds of the vector. IE You you can do ( numverts - 1 )

    Also in your updated example you were writing to the same location in memory everytime. you have to set up a variable that gets incremented until it's greater than or equal to numverts and write to the offset defined by that variable.

  5. #5
    Shadow12345
    Guest
    ya i see what you were saying

    i've earned this silly mistake though, i spent half my day on the computers at school programming, and then i came home at 2:10 and have been programming ever since

    i can't even believe i made that mistake i am sucking right now
    i wish i could stop programming
    but i cant
    you had better stick close by to clean up after my other stupid mistakes that are bound to follow in 15 mins

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    ha, i know how that is. i often find myself writing very complex applications and all of a sudden i do something really silly like that. I can look over the code many many times and not notice what's wrong.

    Having another person checking your code for errors is always great because in order to understand what's going on, they have to analyze each line of code while the programmer himself makes assumptions based on what he's programmed and what he assumes is working (whether it be a valid or invalid assumption).

  7. #7
    Shadow12345
    Guest
    well i pay you on the back for finding my error, you seem like a god right now bcuz honestly i scanned my code for a good 5 mins before posting

    5 mins isn't long but considering the length of my proggie...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple write to a file causing seg-fault??
    By Cell in forum C Programming
    Replies: 11
    Last Post: 05-20-2009, 09:07 AM
  2. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  3. Simple Script causing error
    By CougarElite in forum C++ Programming
    Replies: 9
    Last Post: 12-22-2004, 10:26 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21