memcpy error : Segmentation fault (core dumped)
memcpy error : Segmentation fault (core dumped)
我正在尝试使用带有以下代码的 memcpy 在 c 中将一个字符串复制到另一个字符串:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
并且它在 运行 程序之后给出分段错误(核心转储)错误
我试图将 firstName
和 lastName
声明为指向字符数组的指针而不是指向 char 的指针,但错误仍然存在。我错过了什么?!请帮助...
仅供参考 .. 我正在研究 Ubuntu 14,我正在使用 gcc (Ubuntu 4.8.2-19ubuntu1)...
在这些陈述中
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
你用字符串文字的地址初始化了指针。
在下一条语句中
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
您尝试修改其中一个字符串文字。
根据 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.
还有这个表情
sizeof(hisName.firstName)
returns 指针本身的大小。与对应字符串文字的大小不一样
有效程序可能如下所示
#include <stdio.h>
#include <string.h>
typedef struct fullName
{
char firstName[8];
char lastName[8];
} NAME;
int main()
{
NAME myName = { "aaaaaaa" };
NAME hisName = { "bbbbbb" };
memcpy( myName.firstName, hisName.firstName, sizeof( hisName.firstName ) );
printf( "myName.firstName = %s\n", myName.firstName );
printf( "hisName.firstName = %s\n", hisName.firstName );
}
这在 MSVC 中没有崩溃,它产生了输出:
myName.firstName = bbbbaaa
hisName.firstName = bbbbbb
请注意,只有 hisName
中的 4 个字符已复制到 myName
。那是因为
sizeof(hisName.firstName)
在你的平台上是4,指针的大小。但是无论如何操作这样的字符串都是未定义的行为。
您正在为 myName.firstName
和 hisName.firstName
分配指向字符串文字的指针。无法修改字符串文字,这是导致错误的原因。
要实现您想要的效果,您可以将 firstName
声明为 char
数组,或为其分配内存(作为 char
指针)。
排列方式:
typedef struct fullName
{
char firstName[256]; // a sufficiently large number
char lastName[256];
} NAME;
int main()
{
NAME myName, hisName;
strcpy(myName.firstName, "aaaaaaa"); // You can't assign a string directly
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
分配方式:
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
size_t buffersize = 256; // a sufficiently large number
myName.firstName = malloc(buffersize);
hisName.firstName = malloc(buffersize); // same for lastName
strcpy(myName.firstName, "aaaaaaa");
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, buffersize); // You can't use sizeof() as this will give the size of a pointer
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
我正在尝试使用带有以下代码的 memcpy 在 c 中将一个字符串复制到另一个字符串:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
并且它在 运行 程序之后给出分段错误(核心转储)错误
我试图将 firstName
和 lastName
声明为指向字符数组的指针而不是指向 char 的指针,但错误仍然存在。我错过了什么?!请帮助...
仅供参考 .. 我正在研究 Ubuntu 14,我正在使用 gcc (Ubuntu 4.8.2-19ubuntu1)...
在这些陈述中
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
你用字符串文字的地址初始化了指针。
在下一条语句中
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
您尝试修改其中一个字符串文字。
根据 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
还有这个表情
sizeof(hisName.firstName)
returns 指针本身的大小。与对应字符串文字的大小不一样
有效程序可能如下所示
#include <stdio.h>
#include <string.h>
typedef struct fullName
{
char firstName[8];
char lastName[8];
} NAME;
int main()
{
NAME myName = { "aaaaaaa" };
NAME hisName = { "bbbbbb" };
memcpy( myName.firstName, hisName.firstName, sizeof( hisName.firstName ) );
printf( "myName.firstName = %s\n", myName.firstName );
printf( "hisName.firstName = %s\n", hisName.firstName );
}
这在 MSVC 中没有崩溃,它产生了输出:
myName.firstName = bbbbaaa
hisName.firstName = bbbbbb
请注意,只有 hisName
中的 4 个字符已复制到 myName
。那是因为
sizeof(hisName.firstName)
在你的平台上是4,指针的大小。但是无论如何操作这样的字符串都是未定义的行为。
您正在为 myName.firstName
和 hisName.firstName
分配指向字符串文字的指针。无法修改字符串文字,这是导致错误的原因。
要实现您想要的效果,您可以将 firstName
声明为 char
数组,或为其分配内存(作为 char
指针)。
排列方式:
typedef struct fullName
{
char firstName[256]; // a sufficiently large number
char lastName[256];
} NAME;
int main()
{
NAME myName, hisName;
strcpy(myName.firstName, "aaaaaaa"); // You can't assign a string directly
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
分配方式:
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
size_t buffersize = 256; // a sufficiently large number
myName.firstName = malloc(buffersize);
hisName.firstName = malloc(buffersize); // same for lastName
strcpy(myName.firstName, "aaaaaaa");
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, buffersize); // You can't use sizeof() as this will give the size of a pointer
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}