Hi,

I have a char* buffer that is 1024 bytes in size, and am trying to write an int to a particular offset in the buffer, in this case 0, but all offsets have the same problem.

I initialize the buffer to some values when I first start up. For example, the first 8 bytes contain two separate ints, followed by 2 chars. When I print this out, it shows up as full.

However, when I try to add another value as shown in the code below, it completely destroys all the other data in the buffer.

char* buffer = new char[1024];

***buffer initialization code***

int fd = 4;
int bufferOffset = 0;

memcpy(buffer + bufferOffset, &fd, 4);

cout << buffer << endl;

Does anyone know why this could be happening? I'm under the impression that the above code should copy the 4 bytes of an int to my buffer, not completely destroy it.

As an update, upon closer inspection, my buffer is only completely cleared when I try to write a POSITIVE int with memcpy. Writing a negative value seems to work fine.

I've got to say, this has me quite stumped. Why only negative values. :)

Try printing the bytes in the buffer rather than printing the buffer as a string (especially when you are not treating buffer as a string).

Hmmm...I guess I'm having some trouble understanding how the char buffer works with memcpy.

I'm allocating the space at initialization using:

char* buffer;
buffer = new char[pageFrameSize * numPageFrames];
memset(buffer, 1, pageFrameSize * numPageFrames);
outputBuffer();
void outputBuffer()
{
	for(int i = 0; i < strlen(buffer); i++)
	{
		printf("%x", buffer[i]);
	}
}

Why is it that when I use 0 instead of 1 in the above memset code, the buffer appears to have been wiped out, but any other number outputs a buffer full of that value?

Later in the code, I'm trying to write a few ints into the buffer as header information.

However, when I do the following:

int fd = *any number*;
memcpy(buffer + pageFrameOffset, &fd, 4);

as noted in my original post, everything past the pageFrameOffset appears to have been wiped from my buffer. The length of the array is much smaller when this happens.

Am I not putting data into the buffer correctly, or am I extracting it incorrectly? Also, why can I apparently not store an int of 0 in the buffer??? My goal here is to be able to store and extract information in this buffer correctly.

Edited 7 Years Ago by cmk2901: n/a

>>for(int i = 0; i < strlen(buffer); i++)

buffer is NOT a null-terminated string, so strlen() can/will not work with it. The buffer you created is just an array of characters, and memset set each byte of the array to the value of 0x01.

>>Why is it that when I use 0 instead of 1 in the above memset code, the buffer appears to have been wiped ou
Because the string functions in string.h all; use null-terminated string, and 0 is the same as NULL. So when you memset the buffer with 0 then you are telling strlen() and other string functions that the buffer is empty. strlen() returns the number of non-0 bytes in the buffer. It will scan the buffer until it encounters the first 0 byte, where-ever that may be. If you memset() the buffer with all 1s then strlen() will continuing looking through memory until it finds a byte whose value is 0.


>>as noted in my original post, everything past the pageFrameOffset appears to have been wiped from my buffer. The length of the array is much smaller when this happens.
That's because you are writing the binary value of the integer to the buffer, not the text value. Take the case of an integer whose value is 1. After memcpy() the buffer will contain (*nix operating system the bytes are in reverse order)
buffer[0] = 1
buffer[1] = 0
buffer[2] = 0
buffer[3] = 0

Here again, strlen() will not work with such a buffer. You can only view the buffer's contents with a binary editor, such as vc++ 2008 Express's debugger. Notepad.exe and other such text editors will not let you see the buffer's contents.

>>why can I apparently not store an int of 0 in the buffer??
You can -- the buffer will not contain what you think it will

How to extract the integer from the buffer

There are two methods:

  1. memcpy(); int x; memcpy(&x, buf, sizeof(int));
  2. direct assignment with typecasting: int x = *(int *)buffer;

Edited 7 Years Ago by Ancient Dragon: n/a

Ah, ok. Thanks very much; I think I understand now.

For anyone else who needs info on this, the best I could do to see my buffer at any given point was this:

int bufferSize = *whatever you sized the buffer as in malloc*
for(int i = 0; i < bufferSize; i++)
{
    printf("%02x", buffer[i]
}

This allowed me to view the buffer in hexidecimal format, which was cryptic, but ultimately quite helpful.

If you are using VC++ 2008 (or earlier) you can easily view the entire contents of that buffer without resorting to putting that kind of debug code in your program. Not sure if Code::Blocks will do it too or not. If you are just using command-line compilers then you aren't working as efficiently as you could.

This article has been dead for over six months. Start a new discussion instead.