PacketBuilder是一个允许写入char*数组的类.附加功能:
template <class T>
void PacketBuilder::Append(const T value)
{
memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T));
m_Index += sizeof(T);
}
Run Code Online (Sandbox Code Playgroud)
编译没有错误.如果我调用Append并使用T作为无符号短(WORD).它很棒.如果我使用T作为unsigned char.我收到链接器错误.
m_Builder.Append<unsigned char>(0x01); // Error: LNK1120
m_Builder.Append<unsigned short>(0x0001); // Works
Run Code Online (Sandbox Code Playgroud)
来自VS2010的错误(我得到了德国vs2010):
错误LNK2019:Verweis aufnichtaufgelöstesexternes符号""public:void __thiscall PacketBuilder :: Append(unsigned char)"(?? $ Append @ E @ PacketBuilder @@ QAEXE @ Z)"在Funktion中""public:void __thiscall客户端: :DoHandshake(无效)"(?DoHandshake @ Client @@ QAEXXZ)".1> C:\ XXX\C++\SilkroadEmu\Debug\LoginServer.exe:致命错误LNK1120:1nichtaufgelösteexterneVerweise.
翻译成英文:
错误LNK2019:未解析的外部符号""public:void __thiscall PacketBuilder :: Append(unsigned char)"(?? $ Append @ E @ PacketBuilder @@ QAEXE @ Z)"in Function""public:void __thiscall Client :: DoHandshake(无效)"(?DoHandshake @ …
struct item_CHECK_LIST_data
{
char list[MAX_CHECK_LIST_OPTIONS + 1][MAX_ITEM_TEXT_LEN];
char checkeditems[MAX_CHECK_LIST_OPTIONS + 1];//which are checked
char number_of_options;
};
Run Code Online (Sandbox Code Playgroud)
我可以用memcopy复制这个吗?
我有一个奇怪的错误,将由memcopy无法解释...
最后编辑在OP结束
我用Valgrind测试了一个项目中使用的函数,它说"memcpy中的源和目标重叠",并且还给出了"无效读取"和"无效写入"错误.我修复了代码,以便不重叠这两个缓冲区但没有结果.这是代码:
static
int download_build_buffer(char **seq_numbered_buffer, int seq_n, int dim_payload, char *buffer) {
if(!seq_numbered_buffer)
return 1;
/* allocates seq_numbered_buffer */
if(*seq_numbered_buffer != NULL) {
free(*seq_numbered_buffer);
*seq_numbered_buffer = NULL;
}
if(!(*seq_numbered_buffer = malloc((SIZE_SEQ_N + SIZE_DIM_S + dim_payload + 1) * sizeof(char))))
return 1;
#if DEBUG_DOWNLOAD
fprintf(stderr, "download_build_buffer %d: seq->%d, dim->%d\n",
getpid(), seq_n, dim_payload);
#endif
/* prints sequence number in its string */
seq_n = htonl(seq_n);
if(!memcpy(*seq_numbered_buffer, &seq_n, SIZE_SEQ_N))
return 1;
dim_payload = htonl(dim_payload);
if(!memcpy(&(*seq_numbered_buffer)[SIZE_SEQ_N], &dim_payload, SIZE_DIM_S))
return 1;
/* creates payload …Run Code Online (Sandbox Code Playgroud) 我试图从内存块中读取连续存储的两个整数(我有一个指向void *block块内容的指针)memcpy.使用以下内容读取第一个就好了:
memcpy(&test, block, sizeof(int));
我尝试阅读第二个使用:
memcpy(&test, block + sizeof(int), sizeof(int));
(当然我在程序的不同执行实例中有这些stataments,所以问题不在于覆盖测试)
但我没有得到正确的结果!我在这做错了什么?
我有一个函数将字符串附加到另一个字符串:
char* strjoin(char* str1,const char* str2) {
long len1 = strlen(str1);
long len2 = strlen(str2);
char* result = (char*)malloc(len1+len2+1);
memcpy(result,str1,len1+1);
memcpy(result+len1,str2,len2+1);
free(str1); <--------- program crashes here with error: invalid pointer
return result;
}
Run Code Online (Sandbox Code Playgroud)
调用上面函数的代码是这样的:
char* str = "John";
str = strjoin(str,"\x20");
str = strjoin(str,"Doe");
Run Code Online (Sandbox Code Playgroud)
在上面的函数strjoin中,我为新字符串分配内存,所以我释放旧的str1.为什么str1无效指针?
完整的代码在
http://docs.google.com/file/d/0B09y_TWqTtwlaHNjdjYybHVIcjA/edit?usp=sharing
char data[]="just for a try";
u_char *packet=(u_char *)malloc(28+sizeof(data));
...
...
u_char *udp_data=(u_char *)malloc(8+sizeof(data));
if(udp_data == NULL )
perror("allocating space for udp_data fails\n");
memcpy(udp_data, &udp, sizeof(udp));
memcpy(udp_data+8, data, sizeof(data));
udp.check = in_cksum_udp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)udp_data, sizeof(udp)+sizeof(data)); // if I comment this line, no segmentation fault!
free(udp_data);
udp_data=NULL;
// I get segmentation fault on the next line:
memcpy(packet + 20, &udp, sizeof(udp));
Run Code Online (Sandbox Code Playgroud)
问题是错误是cksum_udp()函数
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a2b in run (arg=0x0) at raw_udp_client.c:115
115 memcpy(packet + 20, …Run Code Online (Sandbox Code Playgroud) 我想用这样的原型编写一个C#方法:
void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, int len);
Run Code Online (Sandbox Code Playgroud)
这个方法有2个选项:
1.
void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, int len)
{
for (int i = 0; i < len; i++)
{
dst[dstOffset + i] = src[srcOffset + i];
}
}
Run Code Online (Sandbox Code Playgroud)
2.
void memcpy(byte[] dst, int dstOffset, byte[] src, int srcOffset, int len)
{
IntPtr intPtr = getIntPtr(dst, dstOffset);
System.Runtime.InteropServices.Marshal.Copy(src, srcOffset, intPtr, len);
}
IntPtr getIntPtr(byte[] buffer, int offset)
{
IntPtr intPtr;
unsafe
{
fixed (byte* …Run Code Online (Sandbox Code Playgroud) 试图回答另一个问题,我提出了一个解决方案,用于std::memcpy()将通用类型存储在chars 的缓冲区中.
我怀疑存储POD可能存在内存对齐问题(我知道有非POD类型,因为std::string非常非常危险).
简而言之:以下程序存在内存对齐问题?
如果它们是,那么可以写一些类似的东西(将POD值存储在char缓冲区中)是安全的吗?如何?
#include <cstring>
#include <iostream>
int main()
{
char buffer[100];
double d1 { 1.2 };
std::memmove( buffer + 1, & d1, sizeof(double) );
double d2;
std::memmove( & d2, buffer + 1, sizeof(double) );
std::cout << d2 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) char buffer[2000];
char boundary[]= "--this-is-a-boundary\n";
char header1_a[]= "Content-Disposition: form-data; name=\"metadata\"\n";
char header1_b[]= "Content-Type: application/json; charset=UTF-8\n\n";
printf("%s%s%s\n\n\n\n", boundary, header1_a, header1_b);
std::memcpy(buffer, boundary, sizeof boundary);
std::memcpy(buffer + sizeof boundary, header1_a, sizeof header1_a);
std::memcpy(buffer + sizeof boundary + sizeof header1_a, header1_b, sizeof header1_b);
std::memcpy(buffer + sizeof boundary + sizeof header1_a + sizeof header1_b,
strJSONout, sizeof strJSONout);
printf("%s", buffer);
Run Code Online (Sandbox Code Playgroud)
但输出是:
--this-is-a-boundary
Run Code Online (Sandbox Code Playgroud)
字符串的其余部分会发生什么?我希望buffer包含所有这些字符数组......
是因为我复制了以NULL结尾的char数组吗?
我定义了以下结构
typedef struct {
uint16_t type;
uint16_t name_offset;
uint32_t data_offset;
uint32_t size;
}node;
Run Code Online (Sandbox Code Playgroud)
sizeof(node) 按预期返回12个字节.
node *nodes = (node*)malloc(sizeof(node)*nodecount);
Run Code Online (Sandbox Code Playgroud)
在我当前的测试中,nodecount是96,并且为节点分配的内存是预期的1152.(通过_msize测试)
我想从我在偏移量为20的缓冲区memcpy进入这个新节点数组(这是我崩溃的地方).我已经确认0x20(含)-0x4A0(不包括)是这个数组的正确结构.
memcpy(nodes,buffer[0x20],sizeof(node)*nodecount)
Run Code Online (Sandbox Code Playgroud)
缓冲区看起来像这样
00000020: 01 00 00 00 00 00 00 00 00 00 00 60
...
00000490: 00 00 00 88 00 00 06 89 02 15 DE 40
Run Code Online (Sandbox Code Playgroud)