2

在下面的代码中,字符串“12345678901234567890”不能完全复制到联合类型变量中。这让我真的很困惑?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

typedef union
{
    int i;
    long l;
    float f;
    double d;
    void *v;
    char *s;
    char c;
 } UType;

UType NewUType_s(char *s)
{
     UType temp;
     strcpy(temp.s, s);      // If we print temp.s and s here, both of them are "12345678901234567890
     return temp;
}

int main()
{
    UType m;
    m = NewUType_s("12345678901234567890");
    printf("%s\n", m.s);

    return 0;
 }

结果是:1234567890123456和一些特殊字符?

这个问题的解决方案可能是:

  • 解决方案1:使用malloc()for m;

  • 解决方案2:将NewUType_s样式更改为指针函数UType *NewUType_s(char *s);,一切都会正常工作,

但是,有人知道上述程序没有正确结果的原因吗?

4

2 回答 2

5

这段代码的问题在于,写入尚未初始化的指针是未定义的行为:temp.s尚未分配可以将字符串复制到其中的内存块,因此strcpy写入您的程序不拥有的内存。

修复此代码很简单:在复制之前分配内存,如下所示:

UType NewUType_s(char *s)
{
     UType temp;
     temp.s = malloc(strlen(s)+1);
     strcpy(temp.s, s);
     return temp;
}

当然你需要free内存以避免内存泄漏。

于 2013-09-02T18:52:02.240 回答
2

您正在调用strcpy,它需要一个目标数组。的值UType仅包含 a char*,但它(未初始化时)将指向内存中的某个随机位置。您可能需要strdup,它将分配一个新字符串并返回一个指向它的指针:

UType NewUType_s(const char *s)
{
     UType temp;
     temp.s = strdup(s);
     return temp;
}
于 2013-09-02T18:52:41.127 回答