本文介绍了strcat的()的执行工作,但会导致核心转储末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我实施的strcat(字符*,为const char *)似乎工作,但随后它会导致核心转储。 My implementation of strcat(char*, const char*) seems to work but then it causes a core dump. 的strcat()实施char* strcat(char* dest, const char* src){ char* tmp = dest; while(*tmp) ++tmp ; while( (*tmp++ = *src++ ) != '\0') ; return (dest);}在 code INT的main(),我叫的strcat():Code in int main() where I call strcat():char arr3[] = "Mr. ";char arr4[] = "Smith";printf("Hello %s!", strcat(arr3, arr4));这实际上串联两个字符串并打印出来,但仍然引起了核心转储。It actually concatenated both strings and printed it out but still caused a core dump.输出:您好史密斯中止先生(核心转储) 我做了什么错了?推荐答案您计划做的 缓冲区溢出是在运行时,由的strcat(ARR3,arr4),因为 ARR3 大小刚好等于的长度先生。字符串,它有额外的字符没有多余的存储空间(从 arr4 )。 Your program doing buffer overflow at runs time, by strcat(arr3, arr4) because arr3 size is just equals to length of "Mr." string , it has no extra memory space for extra chars (from arr4). ARR3 的大小应该是的atlest字符串长度先生+史密斯+ 1 (字符串终止额外1 \\ 0 字符的)size of arr3 should be atlest string length of a "Mr. " + "Smith" + 1(extra 1 for string termination \0 char) 我的建议是使用动态分配的内存足够大小的缓冲区,像做如下code:My Suggestion is use dynamic memory allocation for sufficient size buffer, do something like code below: char arr3[] = "Mr. ";char arr4[] = "Smith";length = strlen(arr3) + strlen(arr4) + 1; //cal-length (sufficient long buffer)char* new_arr = malloc(length); // allocate memory strcpy(new_arr, arr3); // copy first string strcat(new_arr, arr4); // then check your function to concat strings核心突降的原因: 在您的code:char arr3[] = "Mr. ";大小 ARR2 是=字符串的长度先生。长+ (因为 \\ 0 1)字符1 。在的strcat()第一个动作温度指针到第一点空while循环而(* TMP)++ TMP; 。 The size of arr2 is = length of string "Mr." length + 1 (1 because of \0) chars. The strcat() first moves temp pointers to point null in first while loop while(*tmp) ++tmp ;. 之后,在第二个while循环,而((* TMP ++ = * SRC ++)='\\ 0'!); 您试图访问和内存分配是未分配(我是你的过程控制),并访问您还没有分配的内存是C.不确定的行为After that in second while loop while( (*tmp++ = *src++ ) != '\0') ; you are trying to accessing and assigning on memory that is not allocated ( my be out of your process control) and access a memory that you have not allocated is undefined behavior in C. 编辑: 在code ARR3 是这样的图,下面,其中温度点 ARR3 数组:In code arr3 is something like below in diagram, where temp points to arr3 array: arr3 temp 5 6 7 8 +-----+ +--+--+--+---+ | 5 +----->|M |r |. |\0 | +-----+ +--+--+--+---+当循环而(* TMP)TMP + 中断温度开始指向内存位置 8 其中空 \\ 0 存储,就像下图。 when loop while(*tmp) ++tmp ; breaks temp starts pointing to memory location 8 where null \0 is stored, like below diagram. arr3 temp 5 6 7 8 +-----+ +--+--+--+---+ | 8 | |M |r |. |\0 | +-----+ +--+--+--+---+ | ^ +--------------------|当你做临时++ 中环而(!(* TMP ++ = * SRC ++)='\\ 0'); ,温度增量为指向的内存位置 9 并起,但访问和内存分配 9 , 10 ..是非法的,因为它没有分配。这将导致操作系统内核发出一个信号核心转储到你的过程,引起异常。 (值得注意的:作为操作系统由过程检测内存侵权问题 - 有效内存的无效访问,得出:SIGSEGV并获得无效的地址给:SIGBUS)。 When you do temp++ in loop while( (*tmp++ = *src++ ) != '\0') ; , temp increment to point to memory location 9 and onwards, but access and assigning on memory 9, 10.. is illegal because its not allocated. This causes OS kernel send a signal core dump to the your process which caused the exception. (interesting to note: as OS detects memory right violation by a process -- An invalid access to valid memory gives: SIGSEGV And access to an invalid address gives: SIGBUS). 程序错误信号:结果  当这些程序错误信号中的一个终止过程中,它也  写这在记录过程中的状态,一个核心转储文件  终止时间。核心转储文件被命名为'核心',并写入  在任何目录是在当时的过程的电流。 (在  GNU系统,可以为核心的转储指定文件名  环境变量COREFILE)的核心转储文件的目的是使  你可以使用调试器来调查是什么原因导致他们检查  错误。 Program Error Signals: When one of these program error signals terminates a process, it also writes a core dump file which records the state of the process at the time of termination. The core dump file is named `core' and is written in whichever directory is current in the process at the time. (On the GNU system, you can specify the file name for core dumps with the environment variable COREFILE.) The purpose of core dump files is so that you can examine them with a debugger to investigate what caused the error.如果您分配额外的内存(如@JerryCoffin和@保罗 - [R建议),那么你可以访问的内存超越 \\ 0 (内存位置 8 的)是没有问题的。 If you allocates extra memory (as @JerryCoffin and @Paul R suggested) then you can access memory beyond \0 (memory location 8) is no problem. 注意:如果您未在声明中给出大小,则数组的大小将等于字符串的大小例如在字符ARR3 [] =先生; 尺寸 5 。但是,如果你给的大小作为明确字符ARR3 [84] =先生; 然后 aar3 的规模将 84 和初始内存包含先生。然后 0 休息的所有位置。 Note: If you not gives size at declaration then size of array will be equals to size of string e.g. in char arr3[] = "Mr. "; size is 5. But if you give size explicitly as char arr3[84] = "Mr. "; then size of aar3 will be 84 and initial memory contains Mr. then 0 in rest of all locations. 在我的解决方案,我完全分配新的内存块是既字符串作为大 ARR3 和 arr4 可存储使用动态内存分配(的malloc()功能)空符号。此外,如果您在使用动态分配的内存 PTR =的malloc()或 PTR =释放calloc(),那么你应该明确地释放内存使用时,所做的工作免费(PTR)。In my solution I completely allocate new memory blocks that is as large as both string arr3 and arr4 can be stored with null symbol using dynamic memory allocation (malloc() function). Additionally If you allocates dynamic memory using ptr = malloc() or ptr = calloc() then you should free memory explicitly when work done using free(ptr). 这篇关于strcat的()的执行工作,但会导致核心转储末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 08:15