1

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?

I am writing a very simple program where I am trying to concatenate two string in a for loop. The first string in the string concatenation is fixed and the second string is obtained by using itoa funciton. The program is building up successfully but when I am trying to run the program then it is not able to run and getting stopped. I just debugged the program and while debugging I realized that the program is getting stuck in the string concatenation operation. I am posting the program below. Thanks for all your support :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[])
{
    char *str="NULL" ,dec[] = "NULL";
    int i,num;

    printf("Enter a number : \n");
    scanf("%d",&num);

    for (i=0;i<num;i++)
    {   str = "test_file_num_"; 
        itoa(i,dec,10);
        strcat(str,dec);
         printf("%s\n",str);
    }
 return 0;
}
Community
  • 1
  • 1
duttasankha
  • 717
  • 2
  • 10
  • 32

2 Answers2

2

Writing to str is wrong, its memory is not accessible to write. When you do this char *str="NULL" for starters you are merely allocating 5 bytes (you will need more when you start concatenating), and you are also doing it on read only memory (might be on .data or might be on .text), in some cases it will work but it is undefined behavior.

If you can't use dynamic memory try to do something like this instead:

char str[128]="NULL"

This allocates 128 bytes on the stack (you might need more than that). It is not the best way to go, but it will be the smallest change that will fix your problem.

EDIT:

Same problem with this line str = "test_file_num_"; It points str to a string literal that resides on read only memory. Change that for this:

strcpy(str, "test_file_num_");

This will copy the string literal to the stack allocated buffer.

Finally, I think you should replace all the code inside the loop with simply this:

printf("test_file_num_%d\n", i);
imreal
  • 10,178
  • 2
  • 32
  • 48
  • Hi! Thanks for your support. When I declared char *str to char str[128] I am getting an error in the statement str = "test_file_num_"; and the error is error C2106: '=' : left operand must be l-value – duttasankha Jan 14 '13 at 23:18
  • Change that for `strcpy(str, "test_file_num_");`. – imreal Jan 14 '13 at 23:19
  • perfect!!! Works wonderfully. Will you change your answer a bit and add this one as well so that I could accept it as an answer. Thanks again. – duttasankha Jan 14 '13 at 23:21
2

char *str = "NULL"; declares str as a char pointer and initializes it with address of string literal "NULL" which is read-only. Modifying a string literal is an undefined behavior, that is why you get a segmentation fault in strcat (which in turn is a sign of undefined behavior). Not to mention that the initial allocation of memory (4 bytes for 'NULL' + 1 byte for '\0'=5 bytes) will not fit for the concatenate of the additional string.

Instead allocate memory dynamically using malloc(), something like that:

char *str=(char*) malloc(100*sizeof(char));

Or if you don't want to you can always allocate an array as a previous answer said.

Note: If you dynamically allocate memory don't forget to free() it after you are done with the data, otherwise your program will leak memory. If you want to read more about dynamically allocating memory you can refer here.

Theocharis K.
  • 1,281
  • 1
  • 16
  • 43