Array Crashes After 7 Entries

I am not a programmer, but I've been given a programmer's task, so forgive any crudeness in my code. I need to take a text file and read it to an array. The text file looks like this:

S,San Bernardino
R,Riverside
(etc.)


This is the code that does what I "want".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int CountySize=0;
string CountyArray[CountySize][1];
string acronym;
string name;
ifstream CountiesFile;

CountiesFile.open("counties.txt");
while (CountiesFile.good()){
    getline(CountiesFile, acronym, ','); 
    CountyArray[CountySize][0]=acronym; 
    getline(CountiesFile, name); 
    CountyArray[CountySize][1]=name;  
    cout << "[" << CountyArray[CountySize][0] << "] " << CountyArray[CountySize][1] << "\n"; 
    CountySize++;
}
CountiesFile.close();


The problem is that after the code prints out CountyArray[5][1], it seems to crash. I don't know why it would suddenly stop.
Last edited on
1
2
int CountySize=0;
string CountyArray[CountySize][1];


Your array is of size zerro, so how do you expect to fill the array whose size is zerro?

Initializing such array and reading from it is undefined behavior.

btw, why are you solving programing tasks if you're not an programer?
btw, why are you solving programing tasks if you're not an programer?

I do wonder the answer to that question myself sometimes. lol

Why would it start printing out everything up until [6] if the array was not initialized properly?

My file currently reads:
1
2
3
4
5
6
7
J,Jackson County
R,Riverside
S,San Bernardino
W,Wolf River
S,Sean
B,Ben
S,Sam


What the code spits out is:
1
2
3
4
5
6
[J] Jackson County
[R] Riverside
[S] San Bernardino
[W] Wolf River
[S] Sean
[B] Ben

Followed by a crash. If I remove "S,Sam" the code continues on as normal.

Also, isn't initializing an array of Array[0] the same as an array with one spot? I would figure that since I can call on Array[0] to spit out something, that initializing Array[0] is OK.

Changing CountySize to 1 crashes the program outright.
Last edited on
Also, isn't initializing an array of Array[0] the same as an array with one spot?

No it is not,
Array[0] is of size zerro and is thus unusable, you can't write to it nor your can read from such array.
closed account (zb0S216C)
Sean Morrow wrote:
"isn't initializing an array of Array[0] the same as an array with one spot?"

No.

On line 12 & 13, you're attempting to access the second std::string of the first row (which doesn't exist). Since there's no second object, you're entering unknown memory; possibly memory that your program doesn't own.

Wazzak
But would that actually cause the array to crash after a few inputs? Wouldn't it crash it outright?

I changed string CountyArray[CountySize][1] to string CountyArray[200][1] and it works. I think I will just go with it for now, but is it odd that using a dynamically changing int as the size of your array would crash it, or is that expected?
Last edited on
array size must be constant expresion know at compile time:

for example:
static const int CountySize = 10;

If you want resizable array able to resize at runtime then use vector.
Thanks for the reply. That's what I get for trying to be "clever" I suppose.
closed account (zb0S216C)
Sean Morrow wrote:
"But would that actually cause the array to crash after a few inputs? Wouldn't it crash it outright? "

No. Sometimes, the operating system doesn't instantly detect segmentation violations. By the time the OS detects something, you could've written into 2-4 locations.

Wazzak

Topic archived. No new replies allowed.