2
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; 
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Vans S
  • 1,743
  • 3
  • 21
  • 36
  • 1
    If this is C++, you shouldn't use `malloc` nor `memcpy`. If this is C, you shouldn't cast the return value of `malloc`. – dyp Apr 25 '14 at 21:14
  • its ANSI C. With or without cast same thing. – Vans S Apr 25 '14 at 21:15
  • 1
    See http://stackoverflow.com/q/605845/420683 If you're writing C, I recommend you should remove the C++ tag. – dyp Apr 25 '14 at 21:16
  • @VansS: That's not ANSI-C behaviour. – Deduplicator Apr 25 '14 at 21:16
  • 2
    Did you `#include ` ? – Martin R Apr 25 '14 at 21:16
  • Did not include #include tried with it same thing – Vans S Apr 25 '14 at 21:18
  • What's the value of `bytesRead` after the call to `recv`? – Reto Koradi Apr 25 '14 at 21:23
  • The value of bytesRead is 7 "Hello\n" – Vans S Apr 25 '14 at 21:24
  • If you're programming C (and not C++) and don't include `` You should really read the question and accepted answer linked to by @dyp. – Some programmer dude Apr 25 '14 at 21:25
  • You might like to hear of `calloc`... – Deduplicator Apr 25 '14 at 21:25
  • 3
    It seem you need to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), as the code as shown in the question is okay. – Some programmer dude Apr 25 '14 at 21:25
  • @JoachimPileborg The accepted answer however does not explain why this fail / is an error -- that's hidden in a comment. – dyp Apr 25 '14 at 21:26
  • Looking at it with a debugger the bytes getting copied into the char* temp buffer is the address of the recBuffer. followed by 07 00 00. I think the &data needs to be cast somehow.. – Vans S Apr 25 '14 at 21:33
  • @VansS: Don't see any `&data`, and the `&recBuffer` is fine. – Deduplicator Apr 25 '14 at 21:37
  • Oh. Maybe I am failing to realize something? Is it correct for the address of temp to be storing the address of recBuffer? Unless the address of temp points to the temp buffer which got.. ok let me check. Maybe it was working all along and I was not derefferencing correctly. – Vans S Apr 25 '14 at 21:41
  • no its wrong. I dont need the address copied into the char * temp buffer. I need the actual bytes. – Vans S Apr 25 '14 at 21:43
  • Voting up @JoachimPileborg's last comment. Just to make 100% sure, I even tried it. Slightly changed the surrounding code, mainly to copy "Hello\n" into `recBuffer` instead of calling `recv`. Added the necessary includes. Compiled it with clang on a Mac, and ran it. I ended up with "Hello\n" in `temp`, as expected. The code as copied above is not broken. – Reto Koradi Apr 25 '14 at 21:44
  • Reto did you get a pointer to Hello\n in temp or actual "Hello\n" in hex quiv? – Vans S Apr 25 '14 at 21:44
  • @VansS: It copied the string. I printed it out with `printf("%s\n", temp);`, and got Hello, followed by a newline. – Reto Koradi Apr 25 '14 at 21:46
  • @MartinR: `memcpy` is declared in `string.h`, not `stdlib.h`, based on all reference information I found. @VanS, can you try adding `#include `? – Reto Koradi Apr 25 '14 at 21:52
  • Wierd. I get intended behavior when I do memcpy(&temp , &recBuffer, bytesRead ); now with debugger I can see hello is at the address of temp. vs a pointer to hello which it was doing before. But now I want to increment the offset. &temp+10 doesnt seem to work. – Vans S Apr 25 '14 at 21:52
  • @RetoKoradi: According to http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html and to http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc it is ``. That's also what "man malloc" says on my OS X computer. – Martin R Apr 25 '14 at 21:54
  • shows it coming from string.h when i go to ref on it in visual studio 2010. – Vans S Apr 25 '14 at 21:59
  • 1
    @MartinR: Yes, malloc is in `stdlib.h`. memcpy is in `string.h`. – Reto Koradi Apr 25 '14 at 22:02
  • Ok well I am getting semi-intended behavior by doing address of on both src and dst. But now I want to increase dst by the current size of data in the array, so the next copy does not copy to position 0. I tried doing &dst + 5 for example but it still copies to 0. – Vans S Apr 25 '14 at 22:04
  • Don't use the address-of operator on the pointer, it will then copy to the address of the pointer (i.e. overwrite the pointer and not what it points to). When you use the address-of operator in a pointer to `char` you get a pointer to pointer to `char`. If you use that you most certainly will enter into the territory of [*undefined behavior*](http://en.wikipedia.org/wiki/Undefined_behavior). You ***really*** need to provide a better code sample, it seems that what you show is far from what you actually have. – Some programmer dude Apr 25 '14 at 22:10
  • Well, yes - the read data will blow the pointer and stack above it away. – Martin James Apr 25 '14 at 22:13
  • Il make a new example.. im sure if I used a char array vs a char* it would work. I just want char* so i can resize the array if the buffer is too big. – Vans S Apr 25 '14 at 22:17
  • Here is the code that works for me. Sorry, formatting is lost in comment. `#include #include 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; }` – Reto Koradi Apr 25 '14 at 22:42
  • The above should also include ``. Sorry, attempting to copy code into a comment was not a good idea. – Reto Koradi Apr 25 '14 at 22:59
  • Ok. this code works in 1 function. But the Append(Buffer* b, char data, int len) { memcpy(b->m_charBuff, $data, len); } is failing here. Maybe the address of is being passed wrong into func? Called by Append(struct->temp, recBuffer, bytesRead); – Vans S Apr 26 '14 at 15:30

4 Answers4

2

Change:

memcpy(temp , &recBuffer, bytesRead );

To:

memcpy(temp , recBuffer, bytesRead );
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • gives me access violation reading memory address. Not sure if matters but temp is stored in a struct. – Vans S Apr 25 '14 at 21:13
  • 3
    That should not matter, as the address of an array (like `&recBuffer`) is a pointer to its first element anyway (easy to check by printing the address given by `&recBuffer` and `recBuffer`, and might as well print `"recBuffer[0]` at the same time, all three should be the same) – Some programmer dude Apr 25 '14 at 21:16
  • @VansS Did you check that recv() returned something you expect ? I.e. that it didn't fail and returned -1 ? – nos Apr 25 '14 at 21:17
  • Yup. recv is returning hello – Vans S Apr 25 '14 at 21:22
  • @VansS The question is, what does `recv` *return*? – Some programmer dude Apr 25 '14 at 21:23
1

Try

memcpy(temp, &recBuffer[0], bytesRead);
sfjac
  • 7,119
  • 5
  • 45
  • 69
  • compile error "error C2109: subscript requires array or pointer type" – Vans S Apr 25 '14 at 21:21
  • 2
    @VansS: `recBuffer` *is* an array, therefore `&recBuffer[0]` should not produce this error. Did you copy/paste your actual code into the question? – Martin R Apr 25 '14 at 21:27
0

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 );
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

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..

Vans S
  • 1,743
  • 3
  • 21
  • 36