简体   繁体   中英

Segmentation Fault with string array

I'm experiencing some problems with an array of strings that seem to be invading a reserved space of memory. The code is too large to post here, so I'll post the important part below:

int main (  ){

 int i = 0, j = 0, k = 0, count = 0, numLinhas = 0, l = 0;
 char string[100][100];
 char line [17];
 char str[4];
 char str1[5];
 char str2[4];
 char str3[4];

 FILE *p;
 p = fopen("text.txt", "r");
 while(fgets(line, sizeof line, p)!=NULL){
  printf("%s", line);
  strncpy(string[i], line, 17);
  i++;
  numLinhas++; 
  }  
  fclose(p);
  char *temp[numLinhas]; 

After that it goes into a loop in which stores in string [i] the meaning of the statement contained in the file. The beginning of the for and three examples are shown below:

    for (i = 0; i<numLinhas; i++){
    sscanf( string[i], "%s %s %s %s" ,str1, str,str2, str3);
    if(str[0]=='0' && str[1] == '0' && str[2]!= 'd') {
    temp[i] = "NOP";
    count++;
    }
    if(str[0]=='0'&& str[1] == '6' && str[2]!= 'd') {
    sprintf(temp[i],"%s,%s" , "MVI B", str2);
    count = count+2;
    }

    if(str[0]=='0'&& str[1] == '7' && str[2]!= 'd') {
    temp[i] = "RLC";
    count++;
    }

The error is casual - it does not always happen. And it usually occurs when there is a call to sprintf. Ah yes! And below is the txt file I'm loading as an example:

0000 21a 11r 00r
0003 7Ea
0004 21a 12r 00r
0007 46a
0008 80a
0009 21a 13r 00r
000C 77a
000D 3Ea 01a
000F 3Da
0010 76a
0011 0Ad
0012 03d
0013 01d

Just saw something new. here's the compile window i'm getting:

marcos@john:~/Desktop$ ./paraler
0000 21a 11r 00rValor de l: 16

Valor de l: 1
0003 7Ea
Valor de l: 9
0004 21a 12r 00rValor de l: 16

Valor de l: 1
0007 46a
Valor de l: 9
0008 80a
Valor de l: 9
0009 21a 13r 00rValor de l: 16

Valor de l: 1
000C 77a
Valor de l: 9
000D 3Ea 01a
Valor de l: 13
000F 3Da
Valor de l: 9
0010 76a
Valor de l: 9
0011 0Ad
Valor de l: 9
0012 03d
Valor de l: 9
0013 01d
Valor de l: 9
string:0000 21a 11r 00r
string:

string:0003 7Ea

string:0004 21a 12r 00r
string:

string:0007 46a

string:0008 80a

string:0009 21a 13r 00r
string:

string:000C 77a

string:000D 3Ea 01a

string:000F 3Da

string:0010 76a

string:0011 0Ad

string:0012 03d

string:0013 01d

Segmentation fault

The strange thing is that I get some empty spaces of the string array... Does it have anything to do with the error?

Are you allocating memory for each temp[i] before calling sprintf ? If not, there's your problem.

if (!strncmp(str, "06", 2) && str[2] != 'd')
{
  temp[i] = malloc(5 + strlen(str2) + 2);  // Thanks, philippe
  if (temp[i])
    sprintf(temp[i], "%s,%s", "MVI B", str2);
}

Although now you'll have to keep track of which elements of temp were allocated with malloc so you can free them later.

Edit

At the end of your program, you can loop through the temp array and check the contents of each element against the leading part of your string above, and if they match, deallocate that element using free :

for (i = 0; i < numLinhas; i++)
{
  if (strcnmp(temp[i], "MVI B", strlen("MVI B")) == 0)
    free(temp[i]);
}

You do not need to do this for the array elements that were assigned "NOP" or "RLC"; in those cases, you simply copied the address of the string literal to the array element. No new memory was allocated for those elements, so you don't have to worry about deallocating them.

There are some issues I see immediately:

edit the first point might not apply to your environment, but you should keep it in mind

  • You are trying to create char *temp[numLinHas] from an integer whose value will be determined at run-time. This is permissible in C99, or may be provided via a compiler extension, but in older C standards, array sizes must be known at compile-time. Your code might really be doing this:

     int numLinHas = 0; char *temp[numLinHas]; 
  • Another problem is that when you do sprintf , you are attempting to copy to something in temp without allocating memory for the pointer to store the string.

if(str[0]=='0'&& str[1] == '6' && str[2]!= 'd') {
    /* allocate memory to store instruction - to be freed later */  
    temp[i] = malloc(5 + strlen(str2) + 2); /* +2 for the comma */
    sprintf(temp[i],"%s,%s" , "MVI B", str2);
    count = count+2;
    }

The problem arises when a mnemonic starting with 06 and no ending with d is encountered; the sprintf() writes in a non-allocated area as temp[i] is not initialized . You shall allocate some space to store the result of sprintf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM