strncpy 只复制部分字符串

strncpy only copies part of string

假设我有这样的结构

typedef struct _student {
    int studentID;
    char name[30];
    char class[10];
    char department[10];
} Student;

并且以下函数创建了 Student 类型的新变量:

Student *new_student(int id, char *name, char *class, char *dept) {
    Student *s = (Student *)malloc(sizeof(Student *));

    s->studentID = id;
    strncpy(s->name, name, sizeof(s->name) - 1);
    s->name[sizeof(s->name) - 1] = '[=11=]';
    strncpy(s->class, class, sizeof(s->class) - 1);
    s->class[sizeof(s->class) - 1] = '[=11=]';
    strncpy(s->department, dept, sizeof(s->department) - 1);
    s->department[sizeof(s->department) - 1] = '[=11=]';
    return s;
}

void display_student(Student *s) {
    printf("Student: %d | %s | %s | %s\n", s->studentID, s->name, s->class, s->department);
}

为了测试我的代码,我只是在 main() 中写了一些简单的东西

int main() {

    Student *s1 = new_student(20111201, "Lurther King Anders Something", "ICT-56", "SoICT");
    Student *s2 = new_student(20111202, "Harry Potter", "ICT-56", "SoICT");
    Student *s3 = new_student(20111203, "Hermione Granger", "ICT-56", "SoICT");
    Student *s4 = new_student(20111204, "Ron Weasley", "ICT-56", "SoICT");
    display_student(s1);
    display_student(s2);
    display_student(s3);
    display_student(s4);

    return 0;
}

然而,结果对我来说是出乎意料和奇怪的:

有人可以为我解释为什么奇怪的结果吗!我想我做事的方式是正确的,我安全地使用了 strncpy,但我不理解输出。

这个

 ... malloc(sizeof(Student *));

分配

sizeof(Student *)

字节。通常是 4 或 8,因为 Student * 是指针类型。

你可能想要

     ... malloc(sizeof(Student));

ov 更好:

Student * s = malloc(sizeof(*s));

甚至没有无用的括号:

Student * s = malloc(sizeof *s); /* sizeof is an operator, not a function. */

malloc(sizeof *s) 读作:“分配与 s 指向的一样多的字节。

Student *s = (Student *)malloc(sizeof(Student *));

那一行是错误的。您分配要用于 Student 的内存,但只要求 Student*.

您可以通过将表达式而不是类型传递给 sizeof 来降低此类错误的可能性。
另外,in C you don't cast on assigning from a void* to an other data-pointer-type:

Student *s = malloc(sizeof *s);

作为建议,考虑使用 strlcpy,如果需要,请自行定义。
当然,除非您依赖于将缓冲区的其余部分归零,例如因为您将它们直接写入文件。
strncpy 几乎总是错误的,尽管您似乎巧妙地避免了所有陷阱(性能可能除外)。

好的,首先: malloc(sizeof(Student*)) 你得到的指针大小只有 4 个字节,所以你没有为你的结构获得足够的内存。我想知道它实际上是如何工作的,但无论如何。因此,要获取结构的大小,请使用以下示例:

Student *s = (Student *)malloc(sizeof(Student));

第二次你在堆中分配了新的数据,在你尝试执行之后:

strncpy(s->name, name, sizeof(s->name) - 1);

这里你的 s->name 内存中有一些垃圾,因为你没有为这个内存分配任何数据,你应该使用参数中的数据长度函数

 Student *new_student(int id, char *name, char *classSt, char *dept) 
{
    Student *s = (Student *)malloc(sizeof(Student));

    s->studentID = id;

    strncpy(s->name, name, strlen(name) + 1);
    strncpy(s->classSt, classSt, strlen(classSt) + 1);
    strncpy(s->department, dept, strlen(dept) + 1);

    return s;
}