我一直在使用strcat
加入幾個字符串。一切似乎是正確的,打印:字符串字符脫落?
/proc/573/fd/ <- with the backslash
13 <- length
後,我嘗試了「SRC」字符串strcpy
複製到另一個字符串,尾隨字符不無論是在「目標」或「源」字符串打印:
/proc/573/fd <- same string prints without the backslash?
13 <- length is unchanged?
如果我打電話strlen
長度顯示它不變,但?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// This function counts the number of digit places in 'pid'
int pid_digit_places(int pid)
{
int n = pid;
int places = 0;
while (n)
n /= 10;
places++;
return places;
}
char *construct_path(int pid, char *dir)
{
// get count of places in pid
int places = pid_digit_places(pid);
char *pid_str = calloc(places, sizeof(char));
// create string of pid
sprintf(pid_str, "%d", pid);
char *proc = "/proc/";
size_t plen = strlen(proc);
size_t dlen = strlen(dir) + 1;
char *path = calloc(plen + dlen + places, sizeof(char));
strcat(path, proc);
strcat(path, pid_str);
strcat(path, dir);
return path;
}
void fd_walk(int pid)
{
char *fd = "/fd/";
char *fdpath = construct_path(pid, fd);
// prints "/proc/573/fd/ - as expected
printf("Before: %s\n", fdpath);
// shows a length of 13
printf("Size Before: %d\n", (int)strlen(fdpath));
char *test = calloc(strlen(fdpath) + 1, sizeof(char));
strcpy(test, fdpath);
// prints "/proc/573/fd" no trailing "/"
printf("Copied Str: %s\n", test);
//shows a length of 13 though
printf("Copied Size: %d\n", (int)strlen(test));
// prints "/proc/573/fd" no trailing "/" now
printf("After: %s\n", fdpath);
// still shows length of 13
printf("Size After: %d\n", (int)strlen(fdpath));
}
int main(void)
{
// integer to create path around
int pid = 573;
fd_walk(pid);
return 0;
}
我在編譯與gcc-4.8.2
-Wall
:
gcc -o src src.c -Wall
我彈出這個小例子爲ideone。
分配內存時,我已經確保爲null-terminator
添加額外的空間。
我想過重新檢查我是如何初始化我的指針並沒有看到任何錯誤?如何按照預期的方式打印字符串printf
,然後複製它,printf
打印出不同的東西 - 未定義的行爲?
零終止。你可能沒有分配空間來容納他們...... – Roddy
@Roddy關鍵的事情是「終結者」(複數)我只佔一個終結者。謝謝 – tijko
請注意:'pid_digit_places'永遠不會返回大於1的數字。 –