字符串字符掉线?

时间:2014-02-23 22:47:09

标签: c string calloc strcat

我一直在使用strcat加入多个字符串。一切似乎都是正确的,打印:

/proc/573/fd/ <- with the backslash
13            <- length

在我尝试将带有strcpy的“src”字符串复制到另一个字符串后,尾随字符不会打印在“dest”或“src”字符串中:

/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打印出一些不同的 - 未定义的行为?

1 个答案:

答案 0 :(得分:3)

我已经执行了您的确切代码而没有任何麻烦。尽管如此,我发现了两个可能的问题:

// This function counts the number of digit places in 'pid'
int pid_digit_places(int pid)
{
    int n = pid;
    int places = 0;
    while (n) { // <-- The braces were missing here.
        n /= 10;
        places++;
    }
    return places;
}

char *construct_path(int pid, char *dir)
{
    // get count of places in pid
    int places = pid_digit_places(pid);

    // You need "places" bytes for the digits, plus one for the zero
    char *pid_str = calloc(places + 1, sizeof(char));

但是,一般来说,我不会浪费时间来分配完全我需要的内存;额外的代码补偿了大小和复杂性。

只是猜测可能的最大值,并猜测强制

// avoid pid_digit_places altogether
pid_str = malloc(16);
if (pid > 999999999999L) {
    // fprintf an error and abort.

    // Better yet, see whether this is a limit #define'd in the OS,
    // and place an appropriate compile-time # warning. Chances are
    // that unless your code's trivial, running it on a system with
    // such large PIDs (and therefore likely so different an arch!)
    // would cause some other troube to pop up.
    // With an # error in place, you save also the pid check!
}