Segmentation fault :S

Hey guys, I've been trying to nut this one out for the last few weeks but I'll finally admit I have nfi... If you can help me/tell me why my program gives me segmentation faults I'll buy you a hypothetical beer (Y)
Below I'll keep it as simple as possible but if you need more info feel free to ask or grab the entire source code from the svn repo at https://dtedconverter.svn.sourceforge.net/svnroot/dtedconverter

level1DataRecordDescription.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char cDATASET[25977614];
class level1DataRecordDescription {
    private:
         unsigned char cRecSent;
         unsigned char cBlockCount[3];
         signed int    nBlockCount;
         unsigned char cLonCount[2];
         signed short  hLonCount;
         unsigned char cLatCount[2];
         signed short  hLatCount;
         unsigned char cElevation[2402];
         signed short  hElevation[1201];
         unsigned char cCheckSum[4];
         unsigned int  nCheckSum;
    public:
         void set_dataRecord(int,char*);
...
};


level1DataRecordDescription.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
void level1DataRecordDescription::set_dataRecord(int i, char* charArray){
	for ( int j=0; j<2414; j++){
		if ( j<1 ){
		      	 cRecSent = charArray[j+i*2414];
		} else if ( 0<j && j<4 ){
		      	 cBlockCount[j-1] = charArray[j+i*2414];
		} else if ( 3<j && j<6 ){
		      	 cLonCount[j-3] = charArray[j+i*2414];
		} else if ( 5<j && j<8 ){
		      	 cLatCount[j-6] = charArray[j+i*2414];
		} else if ( 7<j && j<2410 ){
		      	 cElevation[j-8] = charArray[j+i*2414];
		} else if ( 2409<j && j<2414 ){
			 cCheckSum[j-2410] = charArray[j+i*2414];
		} // if
	} // for
} // level1DataRecordDescription::set_dataRecord(char*)
...


convertLevel1_Level2.cpp (which causes seg fault)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "level1DataRecordDescription.h"

extern char* cDATASET;
extern char* cUHLLONINTERVAL;
extern char* cUHLLATINTERVAL;
extern char* cUHLLONCOUNT;
extern char* cUHLLATCOUNT;
extern char* cDSIDTEDLEVEL;
extern char* cDSILONINTERVAL;
extern char* cDSILATINTERVAL;
extern char* cDSILONCOUNT;
extern char* cDSILATCOUNT;

void convertLevel1_Level2(){
    // Create a level1DataSet and level2DataSet
    level1DataRecordDescription level1DataSet[1201];
    level2DataRecordDescription level2DataSet[3601];

    // Load all the data into the DataSet
    for ( int a=0; a<1201; a++){ // for each data record (W->E):
        // set level1 datarecord
        level1DataSet[a].set_dataRecord(a,cDATASET);
        // decode record
        level1DataSet[a].decode_data();
        
        // extract the level 1 data into the level2DataSet
        level2DataSet[a*3].set_cRecSent(level1DataSet[a].get_cRecSent());
        level2DataSet[a*3].set_nBlockCount(a*3);
        level2DataSet[a*3].set_hLonCount(level1DataSet[a].get_hLonCount());
        level2DataSet[a*3].set_hLatCount(level1DataSet[a].get_hLatCount());
        for ( int j=0; j<1201; j++){
            level2DataSet[a*3].set_short(j*3, level1DataSet[a].get_hElevation(j));
            if ( j != 0 ){
                level2DataSet[a*3].set_short(j*3-2, level1DataSet[a].get_hElevation(j-1)+((level1DataSet[a].get_hElevation(j)-level1DataSet[a].get_hElevation(j-1))/3));
                level2DataSet[a*3].set_short(j*3-1, level1DataSet[a].get_hElevation(j-1)+((2*(level1DataSet[a].get_hElevation(j)-level1DataSet[a].get_hElevation(j-1)))/3));
            } // if ( j != 0)
        } // for ( int k=0; k<1201; k++ )
//FIXME: THIS DOES NOT CREATE A PROPER CHECKSUM!
        level2DataSet[a*3].set_nCheckSum(level1DataSet[a].get_nCheckSum());

        if ( a != 0 ){
            // if its not the first data record, fill in the gaps
            //behind it
            for ( int b=0; b<3601; b++ ){ // extrapolate, filling W->E gaps
                level2DataSet[b].set_nBlockCount(b);
                level2DataSet[a*3-2].set_short(b,(level1DataSet[a-1].get_hElevation(b)+(level1DataSet[a].get_hElevation(b)-level1DataSet[a-1].get_hElevation(b))/3));
                level2DataSet[a*3-1].set_short(b,(level1DataSet[a-1].get_hElevation(b)+(2*(level1DataSet[a].get_hElevation(b)-level1DataSet[a-1].get_hElevation(b)))/3));
            } // for ( int b=0; b<3601; b++ )
        } // if ( a != 0)
    } // for ( a=0; a<1201; a++)

    for ( int b=0; b<3601; b++){
        // encode level2 data
        level2DataSet[b].encode_data();
        // put data records into cDATASET for writing to file
        level2DataSet[b].set_dataRecord(b, cDATASET);
    } // for ( int b=0; b<3601; b++)

    // Correct UHL/DSI/ACC information to DTED Level 2
    cUHLLONINTERVAL[2] = '1';
    cUHLLATINTERVAL[2] = '1';
    cUHLLONCOUNT[0]='3';cUHLLONCOUNT[1]='6';
    cUHLLATCOUNT[0]='3';cUHLLATCOUNT[1]='6';
    cDSIDTEDLEVEL[4] = '2';
    cDSILONINTERVAL[2] = '1';
    cDSILATINTERVAL[2] = '1';
    cDSILONCOUNT[0]='3';cDSILONCOUNT[1]='6';
    cDSILATCOUNT[0]='3';cDSILATCOUNT[1]='6';
}; // convertlevel1_level2() 


The crash is caused at line 20:
for ( int a=0; a<1201; a++){ // for each data record (W->E):
which from playing around I've worked it down to the
level1DataSet[a].set_dataRecord(a,cDATASET);
I have read about pointers again but its really really annoying me that I can't figure out what I've done wrong.

Any help is appreciated!

Aaron Brown
On closer inspection, one of the function calls within a for loop contained a for loop using the same variable to loop, thus going out of the bounds of the array. (if that makes sense to anyone else..?)

Hope this helps someone else in the future
yeah. for(int a= 0;
just declare

int a;
for(a=0;...
Topic archived. No new replies allowed.