Segmentation fault using strcat

2020-01-29 21:39发布

Here is my code :

char *name, name_log="log-";

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

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

What i need to end up with is name_log = "log-'name'.log" but Im getting a segmentation fault error :((. What am I doing wrong and how can I fix it ? Thx

5条回答
聊天终结者
2楼-- · 2020-01-29 21:49

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"
查看更多
萌系小妹纸
3楼-- · 2020-01-29 21:52

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-29 21:54

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-";
查看更多
The star\"
5楼-- · 2020-01-29 22:03

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");
查看更多
虎瘦雄心在
6楼-- · 2020-01-29 22:05

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.

查看更多
登录 后发表回答