Segmentation fault using strcat

我与影子孤独终老i 提交于 2020-01-09 05:43:04

问题


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


回答1:


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.




回答2:


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.




回答3:


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"



回答4:


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");



回答5:


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-";


来源:https://stackoverflow.com/questions/8269199/segmentation-fault-using-strcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!