Reputation: 1813
char recBuffer[8024];
char* temp = (char*)malloc(65536);
ZeroMemory(recBuffer, 8024);
ZeroMemory(temp, 65536);
bytesRead = recv(_socket, recBuffer, sizeof(recBuffer), 0);
memcpy(temp , &recBuffer, bytesRead );
memcpy doesn't work here. It copies a random char into temp. And if I play around with pointers I can get it to copy the first char of the data received. How do I do this properly?
I want the data recieved recBuffer to be copied into the temp buffer.
Edit: Working code from comment:
#include <string.h>
#include <stdio.h>
int main()
{
char recBuffer[8024];
char* temp = (char*)malloc(65536);
strcpy(recBuffer, "Hello\n");
int bytesRead = 7;
memcpy(temp , &recBuffer, bytesRead );
printf("%s\n", temp);
return 0;
}
EDIT 2 Why this fails?:
#include <stdio.h>
void Append(char* b, char data, int len)
{
memcpy(b , &data, len );
}
int main() {
int bytesRead = 7;
char recBuffer[8024];
char* temp = (char*)malloc(65536);
strcpy(recBuffer, "Hello\n");
Append(temp, recBuffer, bytesRead);
printf("%s\n", temp);
return 0;
}
Upvotes: 2
Views: 5530
Reputation: 1813
In example 2 char data needs to be changed to char const * data and passed to memcopy as data not &data. Bad question I know.. did not realize..
Upvotes: 0
Reputation: 206577
I have a feeling you are seeing the problem because you don't have
#include <stdlib.h>
I get the same result whether I use:
memcpy(temp , &recBuffer, bytesRead );
or
memcpy(temp , recBuffer, bytesRead );
Upvotes: 0
Reputation: 6003
Change:
memcpy(temp , &recBuffer, bytesRead );
To:
memcpy(temp , recBuffer, bytesRead );
Upvotes: 2