Re: Problem with VS and memcpy...

Tech-Archive recommends: Fix windows errors by optimizing your registry



Yes I amde some mistakes in the code, I couldn't paste because it was
too long.

Yes, the correct lines are

unsigned char* buffer = new unsigned char[1000];

and

memcpy(buf, temp, sizeof temp);

I didn't use the redundant casts at first, but then I added them, but
nothing changed.

The problem isI just can't see why if I use the debugger before the
memcpy I can see something like

buffer = 0x01234456 "yqhsfjkvdfdsfhfe"

Where "yqhsfjkvdfdsfhfe" ius the content of the buffer, but after
thememcpy it becomes

buffer = 0x01234456 ""

Is it normal?


Tom Widmer [VC++ MVP] ha scritto:

Paolo wrote:
Hi all!

I have a problem with memcpy, or with VS, or both, I'm not sure....

First of all here's my code:

Well, it looks like a high-error-rate copy of it to me, not the original
code.


unsigned char[] buffer = new unsigned char[1000];

That's not legal code. I assume you actually have:
unsigned char* buffer = new unsigned char[1000];


....
Modify(buffer);

....


bool Modify(unsigned char[] buf)
{
unsigned char temp[8];

//modify temp

memcpy((void *)buffer, (void *)header, sizeof(header));

Did you mean:
memcpy(buf, temp, sizeof temp);
Note that the casts are redundant. Redundant casts hide bugs that could
be detected at compile time.

}

The problem is that if I quickwatch buffer before the memcpy I can see
its content. It Shows the address and the first bytes of the content.
If I do the same after the memcpy I only can see the address, it
doesn't show the content. It also seems like the memcpy doesn't work
well, because if I quickwatch buffer[0] I can't see the first char in
header...

Am I doing something wrong?

Apart from the typos in the code (why didn't you use copy-and-paste?),
and the redundant casts, there's nothing wrong with what you've posted.
You might want to rule out the debugger (or your use of it) as the
problem by outputting the value of header and buffer at appropriate times.

Tom

.



Relevant Pages

  • Re: Problem with VS and memcpy...
    ... unsigned char temp; ... memcpy(buf, temp, sizeof temp); ... Redundant casts hide bugs that could be detected at compile time. ... The problem is that if I quickwatch buffer before the memcpy I can see ...
    (microsoft.public.vc.language)
  • Re: Unplanned Obsolescence
    ... > Other factors to consider are whether the software is capturing to a ... > "local" temp drive first, or to memory, and what kind of buffering there ... The Onyx has a choice of several latency (buffer size) settings. ...
    (rec.audio.pro)
  • Re: malloc()/realloc() - have I got this right?
    ... Be aware, if you do so, that the above routine has what some people ... int cursize, ch, ix; ... char *buffer, *temp; ...
    (comp.lang.c)
  • Re: input word
    ... int cursize, ch, ix; ... char *buffer, *temp; ...
    (comp.lang.c)
  • Re: Storing input into a character array
    ... you're not protecting your buffer against ... fields you want to parse). ... you're using &temp rather than temp - ... use fgets rather than fscanf. ...
    (comp.lang.c)