Segmentation fault using strcat

前端 未结 5 1618
孤独总比滥情好
孤独总比滥情好 2020-12-04 03:16

Here is my code :

char *name, name_log=\"log-\";

------getting \'name\' from user-----

strcat(name_log, name);
char ext[] =         


        
相关标签:
5条回答
  • 2020-12-04 03:49

    the name_log is pointed at a static place: "log-", which means is could not be modified, while as the first parameter in strcat(), it must be modifiable.

    try changing name_log's type char* into char[], e.g.

    char[20] name_log = "log-";
    
    0 讨论(0)
  • 2020-12-04 03:50

    A completely different solution would be this:

    const char *prefix = "log-";
    const char *suffix = ".log";
    // There's a "char *name" somewhere
    int size_needed;
    char *result;
    
    size_needed = snprintf(NULL, 0, "%s%s%s", prefix, name, suffix);
    result = malloc(size_needed + 1);
    snprintf(result, size_needed + 1, "%s%s%s", prefix, name, suffix);
    
    // "result" now contains the desired string.
    

    The nice thing about snprintf is that it returns the number of characters it would write if there was enough space. This can be used by measuring upfront how much memory to allocate which makes complicated and error-prone calculations unnecessary.

    If you happen to be on a system with asprintf, it's even easier:

    char *result = NULL /* in case asprintf fails */;
    asprintf(&result, "log-%s.log", name);
    // "result" must be released with "free"
    
    0 讨论(0)
  • 2020-12-04 03:58

    string literals get allocated a fixed amount of memory, generally in a read only section, you instead need to use a buffer.

    char buffer[64] = "log-";
    strncat(buffer,".log",32);
    

    On a side note, strcat is generally unsafe, you need to use something that that checks the size of the buffer it uses or with limits on what it can concatenate, like strncat.

    0 讨论(0)
  • 2020-12-04 04:01

    For a start, if this is your code:

    char *name, name_log="log-";
    

    then name_log is a char, not a char pointer.

    Assuming that's a typo, you cannot append to string literals like that. Modifications to string literals are undefined behaviour.

    For a variable sized string, as user appears to be, probably the safest option is to allocate another string large enough to hold the result, something like:

    char *name, *name_log = "log-", *ext = ".log";
    // Do something to allocate and populate name
    char *buffer = malloc (strlen (name_log) + strlen (name) + strlen (ext) + 1);
    if (buffer == NULL) {
        // Out of memory.
    } else {
        strcpy (buffer, name_log);
        strcat (buffer, name);
        strcat (buffer, ext);
        // Do something with buffer.
        free (buffer);
    }
    

    The malloc ensures you have enough space to do all the string operations safely, enough characters for the three components plus a null terminator.

    0 讨论(0)
  • 2020-12-04 04:05

    you need to allcocate memory. You cannot add to a string in this way as the string added goes to memory which hasnt been allocated.

    you can do

    char[20] strarray;
    
    strcat(strarray, "log-");
    strcat(strarray, "abcd");
    
    0 讨论(0)
提交回复
热议问题