|
|
View previous topic :: View next topic |
Author |
Message |
Liamthesnake
Joined: 10 Sep 2010 Posts: 1
|
using memset to erase buffer within a structure |
Posted: Fri Sep 10, 2010 10:10 am |
|
|
Hi all
I'm erasing a buffer using memset. If this was a normal buffer it would be
Code: | memset(buffer,0,sizeof(buffer)); |
and to erase a structure it would be
Code: | memset(&structure_test,0,sizeof(structure_test)); |
but what happens if it's a buffer within a structure? do you use the address symbol or not?
Code: | memset(&structure_test.buffer,0,sizeof(structure_test.buffer)); |
Thanks in advance, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 10, 2010 12:55 pm |
|
|
According to the .LST file, the compiler generates the same address for
either method (with vs. 4.112).
Code: |
.... addr = structure_test.buffer;
0022: MOVLW 02 // Load addr with 0x0283
0024: MOVWF 06
0026: MOVLW 83
0028: MOVWF 05
....
.... addr = &structure_test.buffer;
002A: MOVLW 02 // Load addr with 0x0283
002C: MOVWF 06
002E: MOVLW 83
0030: MOVWF 05
// Symbol table
000 @SCRATCH
001 @SCRATCH
001 _RETURN_
002 @SCRATCH
003 @SCRATCH
004 rs232_errors
005-006 main.addr // 'addr' is at 0x0005
280-28C structure_test // Structure is at 0x280
F83 PSP_DATA
FBB CCP_2_LOW
|
Test program:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct
{
int8 a;
int16 b;
int8 buffer[10];
}structure_test;
#locate structure_test = 0x280
//====================================
void main()
{
int16 addr;
addr = structure_test.buffer;
addr = &structure_test.buffer;
while(1);
}
|
The same results occur for a C program compiled in MSVC++ 6.0:
Quote: |
structure_test.buffer = 004237D4
&structure_test.buffer = 004237D4
|
Test program for MSVC++ 6.0 (C program, not C++):
Code: |
#include <stdio.h>
struct
{
unsigned char a;
unsigned short b;
unsigned char buffer[10];
}structure_test;
//============================
void main(void)
{
printf("structure_test.buffer = %p \n", structure_test.buffer);
printf("&structure_test.buffer = %p \n", &structure_test.buffer);
} |
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Sep 13, 2010 1:47 am |
|
|
It also depends on the buffer, if it is a defined array then the compiler knows the size of the overall structure and should clear all memory, if it is a dynamic buffer (pointer) then you would need to free this as clearing the structure will nullify the pointer, you would then re-allocate a new buffer to your pointer. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Mon Sep 13, 2010 3:32 am |
|
|
But of course, far more efficient, to use pointers, and reset these, leaving the contents uncleared. Saved a huge amount of processing to do it this way.....
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|