question on pointers....

i have defined my struct as:

1
2
3
4
5
struct coord{
	double _long;
	double _lat;
	double _height;
};


then i did this:
1
2
3
coord *KElist = new coord [];
for (i = 1; !LOSFile.eof (); i++) 
	LOSFile >> (KElist+i)->_long >> (KElist+i)->_lat >> (KElist+i)->_height;


everytime there is a (KElist+i) involved the will be an run time error. firstime just stops somewhere in xlocal
1
2
3
4
			_BEGIN_LOCK(_LOCK_LOCALE)
				if (_Refs < (size_t)(-1))
					++_Refs;
			_END_LOCK()


the the second time the code encounters the KElist+i further down the code:
xiobase
1
2
3
4
	virtual __CLR_OR_THIS_CALL ~ios_base()
        {
        _Ios_base_dtor(this);
        }


saying some sort of corruption of heap..;.what can i do?
What are you trying to create with coord *KElist...? Make an array of coords or something?
yep! *KElist is an array of coords

but i do not know how many variables are in there. (how many coords i have)

the top part of the code reads as follows:

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
	coord *KElist = new coord [];
	coord temp; //holding variable

	fstream LOSFile, HeightFile;

	LOSFile.open ("LOS_Violation.txt", ifstream::in);
	HeightFile.open ("height.txt", ifstream::in);

	if (!LOSFile.is_open ())	return 1;
	if (!HeightFile.is_open ())	return 2;

	int i , j, k;

//read start pos//
	HeightFile >> KElist->_long >> KElist->_lat >> KElist->_height;
	
//read los violations//
	for (i = 1; !LOSFile.eof (); i++) {
		LOSFile >> (KElist+i)->_long >> (KElist+i)->_lat >> (KElist+i)->_height;
	}

//read end pos//
	for (j = 0; !HeightFile.eof (); j++)
		HeightFile >> temp._long >> temp._lat >> temp._height;
	i++;
	*(KElist+i) = temp;

then after that is data manipulation and closing of files eg LOSFile.close();
i ran the program thru a console debug...
and if i changed the

(KElist+i) to (KElist+(i*k)) where k = sizeof (coord)

it works.

But got a problem in line 24.
the data in my heightfile is like this:
103.749 1.36522 26
103.759 1.36661 125
103.769 1.368 37
103.779 1.36939 51
103.789 1.37078 42
103.799 1.37218 53
103.809 1.37357 42
103.819 1.37496 42
103.829 1.37635 37
103.839 1.37774 32

but after reading line6, the filepointer looks like it has stopped moving and i keep getting constant values and never the EOF bit. thus resulting in a infinite loop... im baffled....
I keep getting _long = 103.809 _lat = 1.00000 _height = 53

purpose of this code is to get the 1st line of data from HeightFile, den all the entries in the LOSFile and then the last entry from HeightFile...
Last edited on
I'm not sure how big the new coord []; array would be without specifying the length, I've never tried it. I suspect your problem is something to do with this but I haven't quite got a handle on it yet.

EDIT: I suspect you are going off the end, and overwriting something else. I think you need to do a linked list for this, or malloc/realloc or an STL container.
Last edited on
Agreed. new coord[]; is not valid. Your not telling the OS how much memory to allocate. This is bound to cause a runtime error due to buffer-overflow.

Have a look at the STL Vector container.
e.g
1
2
3
4
vector<coord> vList;

coord newCoord = coord();
vList.push_back(NewCoord);


You can store pointers in there vector<coord*>; but you must de-allocate the memory from each pointer manually.
Topic archived. No new replies allowed.