First, I write a simple program.
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 int main()
6 {
7 char *buf = (char*)malloc(sizeof(char)*2000);
8 char tmp[256];
9 strcpy (tmp, "test");
10 printf ("tmp: %s\n", tmp);
11 strncat (buf, tmp, strlen(tmp));
12 printf ("buf: %s\n", buf);
13 }
The expected result is:
tmp: test
buf: test
But after I combine the code in my big project. (which use lots heap segments)
153 char *inbuf = (char*)malloc(sizeof(char)*2000);
154 char *outbuf = (char*)malloc(sizeof(char)*2000);
155 char *oldinbuf = (char*)malloc(sizeof(char)*2000);
156 char *errbuf = (char*)malloc(sizeof(char)*2000);
157 memset (inbuf, '\0', strlen(inbuf));
158 memset (oldinbuf, '\0', strlen(oldinbuf));
159 memset (errbuf, '\0', strlen(oldinbuf));
Then in the line:11, I get the error message Segmentation fault (core dumped)
Is there any possibilities that strncat
result in segment fault?