unexpected change of variable

Hello,
I have a problem with global declared variable type INT called aa. After global declaration, I fill it with 0 in main() and after that,it is filled with number of rows of data file, which is loaded into array. When I am filling another array with another data file, aa is suddenly rewritten with huge unknown number...but aa is no longer used in code for writting to it. I need aa to pass number of rows to some function out of main() and I donĀ“t know why aa is rewritten.

Here is part of code:
declaration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <time.h>

#define PI 3.14159265
#define E 2.718281828
using namespace std;

float mapa[100][6];
float zdroje[50][11];
float wprij[10][6];
float alfa[4][6];
float vzdch[5]; 
int aa,bb,cc,dd,xmax,zmax,xte,zte;//xte zte predavany ven z paprsku
float wl,uhl;//predavano ven z paprsku   

filling aa with number of rows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string radek1;
  ifstream soubor ("vstup1.txt");
  if (soubor.is_open() )
  {
    while (! soubor.eof() )
    {
    getline (soubor,radek1);
    aa++;
    }
  soubor.close();
  }
  else
    {
       cout << "nenasel jsem soubor"<<endl;
       cout << "ukoncuji";
       cin>>cekam;
       return 0;
    }

filling array with data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     FILE * vst1;
      vst1 = fopen("vstup1.txt","r");
      for (int m=0;m<aa;m++)
    {
      fscanf (vst1, "%f", &a);
      fscanf (vst1, "%f", &b);
      fscanf (vst1, "%f", &c);
      fscanf (vst1, "%f", &d);
      fscanf (vst1, "%f", &e);
      fscanf (vst1, "%f", &f);
      mapa[m][0]=a;
      mapa[m][1]=b;
      mapa[m][2]=c;
      mapa[m][3]=d;
      mapa[m][4]=e;
      mapa[m][5]=f;
    } 
    fclose (vst1);

code written befor is used repeatedly to fill another arrays, with another names and also there are used another variables to count number of rows in data files.

In row with coments..there error occurs...it`s position, where I identified change of aa:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 string radek5;
  ifstream soubor5 ("vzduch.txt");
  if (soubor5.is_open()) //jen pro overeni existence souboru
    soubor5.close();
  else
    {
       cout << "nenasel jsem soubor"<<endl;
       cout << "ukoncuji";
       cin>>cekam;
       return 0;
    }
      FILE * vst5;
      vst5 = fopen("vzduch.txt","r");
      fscanf (vst5, "%f", &a);
      fscanf (vst5, "%f", &b);
      fscanf (vst5, "%f", &c);
      fscanf (vst5, "%f", &d);
      fscanf (vst5, "%f", &e);
      fscanf (vst5, "%f", &f);
      vzdch[0]=a;
      vzdch[1]=b;
      vzdch[2]=c;
      vzdch[3]=d;
      vzdch[4]=e;
      cout<<aa<<" ";
      vzdch[5]=f;   //!!!ERROR aa is changed to very high number!!!
      cout<<aa<<" ";
      fclose (vst5);

Code has about 250lines, so I am not sure, if is it possible to post it here. I am using Dev-C++ 4.9.9.2.
I am new in c++, so I hope, that there is some funny mistake. Thank you for your help.
It is a buffer overrun problem I belive.

1
2
float vzdch[5];         //Array declared with 5 elements, 0 to 4
int aa,bb,cc,dd,xmax,zmax,xte,zte;


1
2
3
4
5
      
vzdch[4]=e;          //This is the fith element
cout<<aa<<" ";
vzdch[5]=f;          //This is out of bounds
cout<<aa<<" ";


The compiler has allocated memory for aa directly after vzdch, so the out of bounds assignment overwrites aa.

Just change float vzdch[5]; to float vzdch[6]; to resolve.
Last edited on
Thank you very much. You was right. Now it works.
I was looking on it all the night and didn`t see anything.
No problems, glad to help.
Topic archived. No new replies allowed.