Running out of heap space?

I'm trying to read in a file containing all the counties in the US and store them in a county object that I've created. I got the file to read correctly, but when I try to store them in an array of pointers the file read crashes after reading in only a few hundred lines. Am I running out of heap space? I didnt think that was possible. Any idea whats going on?

this is the countylist.cpp where I read in the file
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
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include "countylist.h"
#include "county.h"
using namespace std;

// Default constructor
countyList::countyList()
{
    int stateId = 0;
    int countyId = 0;
    int i = 0;
    char countyName[256];
    char state[3];
    
    // opens usfips file
    ifstream inFile;    
    inFile.open("usfips.txt");
    
    // checks to see if the file opened correctly    
    if (!inFile)
       cout << "Error opening file\n";
    else
    {
        // reads in and constructs all the county objects that are stored in counties[]
        // reads in each element and seperates by comma stops when there is no more valid data to read
        char comma;
        cout << "file read\n";
        while(inFile.good())
        { 
             inFile.getline(state, 4, ',');
             inFile >> countyId;
             inFile >> comma;
             inFile >> stateId;
             inFile >> comma;
             inFile.getline(countyName, 255, '\n');
             cout << "loop " << i << "\n" << "state" << state << "\n county id "<< countyId << "\n state id " << stateId << "\n county name " << countyName << "\n";
             counties[i] = new county(static_cast<string>(state), static_cast<string>(countyName), stateId, countyId);
            // cout << counties[i].getState() << counties[i].getCountyName() << counties[i].getStateId() << counties[i].getCoutyId();
        //      cout << "\nafter counties\n";
             i++;  
        };      
        cout << "\n out of fileread loop";          
        // closes the file
        inFile.close();       
        cout << "\n file closed"; 
   }
   // returns the size of the good elements of the array  
   cout << "\n before countiesSize initalized i = " << i << endl;
   countiesSize = i;
   cout << "\n countiesSize initalized to " << countiesSize << endl;;
}


this is countylist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef COUNTYLIST_H
#define COUNTYLIST_H

#include <string>
#include "county.h"
using namespace std;

class countyList
{
    private:   
       county* counties[3500];
       int countiesSize;
    
    public:       
       // constructor prototypes
       countyList();          
       
       // find county index by searching for Fips
       county* findCounty(int, int);  
};

#endif 



this is the county object's constructor
1
2
3
4
5
6
7
8
// Parameter Constructor
county::county(string s, string c, int sId, int cId)
{
	state = s;
	countyName = c;
	stateId = sId;
	countyId = cId;
}


and this is the 1st 300 lines of the list of counties usfips.txt
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
AL,01,001,Autauga
AL,01,001,Autauga
AL,01,003,Baldwin
AL,01,005,Barbour
AL,01,007,Bibb
AL,01,009,Blount
AL,01,011,Bullock
AL,01,013,Butler
AL,01,015,Calhoun
AL,01,017,Chambers
AL,01,019,Cherokee
AL,01,021,Chilton
AL,01,023,Choctaw
AL,01,025,Clarke
AL,01,027,Clay
AL,01,029,Cleburne
AL,01,031,Coffee
AL,01,033,Colbert
AL,01,035,Conecuh
AL,01,037,Coosa
AL,01,039,Covington
AL,01,041,Crenshaw
AL,01,043,Cullman
AL,01,045,Dale
AL,01,047,Dallas
AL,01,049,DeKalb
AL,01,051,Elmore
AL,01,053,Escambia
AL,01,055,Etowah
AL,01,057,Fayette
AL,01,059,Franklin
AL,01,061,Geneva
AL,01,063,Greene
AL,01,065,Hale
AL,01,067,Henry
AL,01,069,Houston
AL,01,071,Jackson
AL,01,073,Jefferson
AL,01,075,Lamar
AL,01,077,Lauderdale
AL,01,079,Lawrence
AL,01,081,Lee
AL,01,083,Limestone
AL,01,085,Lowndes
AL,01,087,Macon
AL,01,089,Madison
AL,01,091,Marengo
AL,01,093,Marion
AL,01,095,Marshall
AL,01,097,Mobile
AL,01,099,Monroe
AL,01,101,Montgomery
AL,01,103,Morgan
AL,01,105,Perry
AL,01,107,Pickens
AL,01,109,Pike
AL,01,111,Randolph
AL,01,113,Russell
AL,01,115,St. Clair
AL,01,117,Shelby
AL,01,119,Sumter
AL,01,121,Talladega
AL,01,123,Tallapoosa
AL,01,125,Tuscaloosa
AL,01,127,Walker
AL,01,129,Washington
AL,01,131,Wilcox
AL,01,133,Winston
AK,02,013,Aleutians East
AK,02,016,Aleutians West
AK,02,020,Anchorage
AK,02,050,Bethel
AK,02,060,Bristol Bay
AK,02,068,Denali
AK,02,070,Dillingham
AK,02,090,Fairbanks North Star
AK,02,100,Haines
AK,02,105,Hoonah-Angoon
AK,02,110,Juneau
AK,02,122,Kenai Peninsula
AK,02,130,Ketchikan Gateway
AK,02,150,Kodiak Island
AK,02,164,Lake and Peninsula
AK,02,170,Matanuska-Susitna
AK,02,180,Nome
AK,02,185,North Slope
AK,02,188,Northwest Arctic
AK,02,195,Petersburg
AK,02,198,Prince of Wales-Hyder
AK,02,220,Sitka
AK,02,230,Skagway
AK,02,240,Southeast Fairbanks
AK,02,261,Valdez-Cordova
AK,02,270,Wade Hampton
AK,02,275,Wrangell
AK,02,282,Yakutat
AK,02,290,Yukon-Koyukuk
AZ,04,001,Apache
AZ,04,003,Cochise
AZ,04,005,Coconino
AZ,04,007,Gila
AZ,04,009,Graham
AZ,04,011,Greenlee
AZ,04,012,La Paz
AZ,04,013,Maricopa
AZ,04,015,Mohave
AZ,04,017,Navajo
AZ,04,019,Pima
AZ,04,021,Pinal
AZ,04,023,Santa Cruz
AZ,04,025,Yavapai
AZ,04,027,Yuma
AR,05,001,Arkansas
AR,05,003,Ashley
AR,05,005,Baxter
AR,05,007,Benton
AR,05,009,Boone
AR,05,011,Bradley
AR,05,013,Calhoun
AR,05,015,Carroll
AR,05,017,Chicot
AR,05,019,Clark
AR,05,021,Clay
AR,05,023,Cleburne
AR,05,025,Cleveland
AR,05,027,Columbia
AR,05,029,Conway
AR,05,031,Craighead
AR,05,033,Crawford
AR,05,035,Crittenden
AR,05,037,Cross
AR,05,039,Dallas
AR,05,041,Desha
AR,05,043,Drew
AR,05,045,Faulkner
AR,05,047,Franklin
AR,05,049,Fulton
AR,05,051,Garland
AR,05,053,Grant
AR,05,055,Greene
AR,05,057,Hempstead
AR,05,059,Hot Spring
AR,05,061,Howard
AR,05,063,Independence
AR,05,065,Izard
AR,05,067,Jackson
AR,05,069,Jefferson
AR,05,071,Johnson
AR,05,073,Lafayette
AR,05,075,Lawrence
AR,05,077,Lee
AR,05,079,Lincoln
AR,05,081,Little River
AR,05,083,Logan
AR,05,085,Lonoke
AR,05,087,Madison
AR,05,089,Marion
AR,05,091,Miller
AR,05,093,Mississippi
AR,05,095,Monroe
AR,05,097,Montgomery
AR,05,099,Nevada
AR,05,101,Newton
AR,05,103,Ouachita
AR,05,105,Perry
AR,05,107,Phillips
AR,05,109,Pike
AR,05,111,Poinsett
AR,05,113,Polk
AR,05,115,Pope
AR,05,117,Prairie
AR,05,119,Pulaski
AR,05,121,Randolph
AR,05,123,St. Francis
AR,05,125,Saline
AR,05,127,Scott
AR,05,129,Searcy
AR,05,131,Sebastian
AR,05,133,Sevier
AR,05,135,Sharp
AR,05,137,Stone
AR,05,139,Union
AR,05,141,Van Buren
AR,05,143,Washington
AR,05,145,White
AR,05,147,Woodruff
AR,05,149,Yell
CA,06,001,Alameda
CA,06,003,Alpine
CA,06,005,Amador
CA,06,007,Butte
CA,06,009,Calaveras
CA,06,011,Colusa
CA,06,013,Contra Costa
CA,06,015,Del Norte
CA,06,017,El Dorado
CA,06,019,Fresno
CA,06,021,Glenn
CA,06,023,Humboldt
CA,06,025,Imperial
CA,06,027,Inyo
CA,06,029,Kern
CA,06,031,Kings
CA,06,033,Lake
CA,06,035,Lassen
CA,06,037,Los Angeles
CA,06,039,Madera
CA,06,041,Marin
CA,06,043,Mariposa
CA,06,045,Mendocino
CA,06,047,Merced
CA,06,049,Modoc
CA,06,051,Mono
CA,06,053,Monterey
CA,06,055,Napa
CA,06,057,Nevada
CA,06,059,Orange
CA,06,061,Placer
CA,06,063,Plumas
CA,06,065,Riverside
CA,06,067,Sacramento
CA,06,069,San Benito
CA,06,071,San Bernardino
CA,06,073,San Diego
CA,06,075,San Francisco
CA,06,077,San Joaquin
CA,06,079,San Luis Obispo
CA,06,081,San Mateo
CA,06,083,Santa Barbara
CA,06,085,Santa Clara
CA,06,087,Santa Cruz
CA,06,089,Shasta
CA,06,091,Sierra
CA,06,093,Siskiyou
CA,06,095,Solano
CA,06,097,Sonoma
CA,06,099,Stanislaus
CA,06,101,Sutter
CA,06,103,Tehama
CA,06,105,Trinity
CA,06,107,Tulare
CA,06,109,Tuolumne
CA,06,111,Ventura
CA,06,113,Yolo
CA,06,115,Yuba
CO,08,001,Adams
CO,08,003,Alamosa
CO,08,005,Arapahoe
CO,08,007,Archuleta
CO,08,009,Baca
CO,08,011,Bent
CO,08,013,Boulder
CO,08,014,Broomfield
CO,08,015,Chaffee
CO,08,017,Cheyenne
CO,08,019,Clear Creek
CO,08,021,Conejos
CO,08,023,Costilla
CO,08,025,Crowley
CO,08,027,Custer
CO,08,029,Delta
CO,08,031,Denver
CO,08,033,Dolores
CO,08,035,Douglas
CO,08,037,Eagle
CO,08,039,Elbert
CO,08,041,El Paso
CO,08,043,Fremont
CO,08,045,Garfield
CO,08,047,Gilpin
CO,08,049,Grand
CO,08,051,Gunnison
CO,08,053,Hinsdale
CO,08,055,Huerfano
CO,08,057,Jackson
CO,08,059,Jefferson
CO,08,061,Kiowa
CO,08,063,Kit Carson
CO,08,065,Lake
CO,08,067,La Plata
CO,08,069,Larimer
CO,08,071,Las Animas
CO,08,073,Lincoln
CO,08,075,Logan
CO,08,077,Mesa
CO,08,079,Mineral
CO,08,081,Moffat
CO,08,083,Montezuma
CO,08,085,Montrose
CO,08,087,Morgan
CO,08,089,Otero
CO,08,091,Ouray
CO,08,093,Park
CO,08,095,Phillips
CO,08,097,Pitkin
CO,08,099,Prowers
CO,08,101,Pueblo
CO,08,103,Rio Blanco
CO,08,105,Rio Grande
CO,08,107,Routt
CO,08,109,Saguache


Does anyone know whats going on? This seems so weird to me. I thought the heap was essentially infinite
Are you not allowed to use std::vector? It would make dealing with the "array" part so much easier and take the guesswork out of the dynamic allocation. You don't really don't need to static_cast from a cstring to a string (as string takes cstring as an argument in the ctor). Is the program segfaulting?

On a side-note (it may be personal preference) I always find it much easier to read the entire "line" out of the file and then parse it for what I need rather than using piecemeal input.
I've never used vectors before, so I'm just using what I know.

when I compile and run it says that the .exe has stopped working and doesn't give an error.
I tried removing the static _cast and its still doing the same thing
So it seems like vectors would be useful if I was using a truly dynamic array and resizing it etc, but that's not the case with this implementation. I know how long the list is, and we haven't been tough vectors yet, so I assume we're supposed to learn how to do it the hard way first like being forced to used c strings before learning about strings. Either way I'm more interested in getting this working as it is than re-writing it.
I think your biggest problem is trying to read the integers from the file with:
 
filestream >> var;


The file is a csv and there is no whitespace. I coded this up real quick and it seems to work ok. The csv I got from the web and is in a bit of different order but the solution is the same.
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
70
71
#include <cstdlib>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;

class county
{
  public:
    string stateName;
    string countyName;
    int stateId;
    int countyId;
  public:
    county(string s, string c, int sId, int cId)
    {
      stateName = s;
      countyName = c;
      stateId = sId;
      countyId = cId;
    }
};

county *counties[3500] = {0};

int main()
{
  char comma;
  char stateId[3] = {0};
  char countyId[4] = {0};
  char countyName[256] = {0};
  char state[3] = {0};
  int countiesSize = 0;

  ifstream inFile;
  inFile.open("counties.csv");

  // checks to see if the file opened correctly
  if (!inFile)
    cout << "Error opening file\n";
  else
  {
    // reads in and constructs all the county objects that are stored in counties[]
    // reads in each element and seperates by comma stops when there is no more valid data to read
    char comma;
    cout << "file read\n";
    while(inFile.good())
    {
      inFile.getline(state,3,',');
      inFile.getline(stateId,3,',');
      inFile.getline(countyId,4,',');
      inFile.getline(countyName,256,'\n');
      cout << "loop " << countiesSize << "\n" << "state " << state << "\n county id "<< countyId << "\n state id " << stateId << "\n county name " << countyName << "\n";
      counties[countiesSize] = new county(state,countyName, atoi(stateId), atoi(countyId));
        //      cout << "\nafter counties\n";
      countiesSize++;
    };
    cout << "\n out of fileread loop";
        // closes the file
    inFile.close();
        cout << "\n file closed";
  }
  cout << countiesSize << " Counties loaded." << endl;
  for(int idx = 0; idx < countiesSize; ++idx)
  {
    cout << "array " << idx << "\n" << "state " << counties[idx]->stateName << "\n county id "<< counties[idx]->countyId << "\n state id " << counties[idx]->stateId << "\n county name " << counties[idx]->countyName << "\n";
    delete counties[idx];
  }
  return 0;
}
Last edited on
Topic archived. No new replies allowed.