Unknown error

Hello....
I've tried adding the name of some countries into country list. So I have to allocate a layered address of char (pointer) to do this properly. And run the program... Crash !!! We are sorry about this inconvenience...??????
No compling & linking errors & warnings, why this caused??
I'm don't know how to make my code become correct. Perhaps something in my code makes my program sad and... cry !!!

For more details, I post my code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// What are your favorite countries?
char ** strCountrylist;
char strInp[256]; //Input 
int nCount = 0;
bool bAllow = true; //Allows to continue parsing (inputing) or not
int main(){
strCountrylist = new char*[256]; //allocating the buffer
// Start the interview
while(bAllow){
printf("Your favorite country  :");
scanf("%s", strInp);
printf("\n");
if(!stricmp(strInp, "none"))bAllow = false;
else{
strcpy(strCountrylist[nCount], strInp);nCount++;}
}
//printing the results
printf("Your favorite countries you have added :");
for(int i = 0; i < nCount;i++)
printf(strCountrylist[i]);
return 0;
}

And... It's.. Crashed !!!
Does anyone know this crash?? The code seems is wrong somewhere.
I haven't seen this error before so I don't know how to solve it...
Last edited on
This code is pretty bad
First off why are you making it a pointer of a pointer? You dont need a 2d array for this
Also you arent deleting the array
On line 15, you copy to strCountrylist[nCount] which has not been allocated yet. Any input over 255 characters will probably break the program as well.
Then, what is correct code???
Thanks !!!
Ah... Yes !! The error has been detected & solved !!!

This is (maybe) the correct code :
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
// What are your favorite countries?
char ** strCountrylist;
char strInp[9999]; //Redundant buffer
int nCount = 0;
bool bAllow = true; //Allows to continue parsing (inputing) or not

int main(){
strCountrylist = new char*[9999]; //allocating the (redundant) buffer
// Start the interview
while(bAllow){
printf("Your favorite country  :");
scanf("%s", strInp);
printf("\n");

if(!stricmp(strInp, "none"))bAllow = false;
else{if(nCount > 256)break;
if(strlen(strInp) <= 256)){
strCountrylist[nCount] = new char[256];
strcpy(strCountrylist[nCount], strInp);nCount++;}
else printf("Out of memory !!!\n\n");
}
}
//printing the results
printf("Your favorite countries you have added :");
for(int i = 0; i < nCount;i++){printf("%s,",strCountrylist[i]);delete strCountrylist[i];}
delete strCountrylist;
return 0;
}


Is this correct ???
Last edited on
Topic archived. No new replies allowed.