gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > initializing buffer data

#67437 - linus - Wed Jan 18, 2006 1:59 am

hello, i have the following bit of code:
Code:
   
while (length > curPos) {
          char *buffer = (char*) malloc(32);
          FAT_fread(buffer, 32, 1, fp);
          PA_OutputSimpleText(0,0,j++, buffer);
          curPos = fp->curPos;
          free(buffer);
          //PA_OutputText(0,0,j++,"%d bytes read", result);
}


and everything works fine but the buffer contains the old data - how can i clear it?

#67444 - sajiimori - Wed Jan 18, 2006 4:25 am

Writing to the buffer just before freeing it doesn't make sense, and writing to it after freeing it is undefined in C.

To understand why, think of malloc as creating a new buffer every time you call it. The address malloc returns should be considered random, as should the contents of a newly-allocated buffer.

If malloc happens to return the same address each time through the loop, that should be considered a coincidence. Relying on such a coincidence will lead to confusion later.

'calloc' will allocate a block and then fill it with zeroes. It's just like calling malloc followed by memset.

All that aside, you should not be using malloc/free here. Allocating 'buffer' on the stack (by declaring it as a local array) is faster and simpler. If you must use dynamic allocation, you could at least allocate the buffer once before entering the loop instead of allocating and freeing it repeatedly.

In general, don't use dynamic allocation unless it makes your program better in a measurable way.

#67506 - linus - Wed Jan 18, 2006 6:47 pm

yeah i see what your saying - and i did expect malloc to return a different memory space everytime, but it didnt.

this was my original code:

Code:

    while (length > curPos) {
          char *buffer[32];
          FAT_fread(buffer, 32, 1, fp);
          PA_OutputSimpleText(0,0,j++, buffer);
          curPos = fp->curPos;
    }


the reason i switched to malloc was to see if it cleared the buffer (which it didnt because i should have been calling calloc). this method doesnt clear every time the buffer is created.

i dont want to have

char *buffer[32] = " ... " //32 whitespaces

but how do i get this effect?

#67513 - sajiimori - Wed Jan 18, 2006 7:44 pm

Careful -- the above declaration creates an array of 32 pointers to chars. Take out the asterisk to create 32 chars.

Do you want spaces or nulls? They are different values.

You can have the compiler generate code to clear the array upon declaration like this:
Code:
char buffer[32] = { 0 };

You can also make a 'for' loop to iterate over the array and set it to the values you want. 'memset' is a standard library function for that.

#67514 - linus - Wed Jan 18, 2006 7:48 pm

sajiimori wrote:
Careful -- the above declaration creates an array of 32 pointers to chars. Take out the asterisk to create 32 chars.


oh bumf your right - sorry should have spotted this :S thanks very much for your help. what can i say - im a java programmer lol.