CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

using memset to erase buffer within a structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Liamthesnake



Joined: 10 Sep 2010
Posts: 1

View user's profile Send private message

using memset to erase buffer within a structure
PostPosted: Fri Sep 10, 2010 10:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 10, 2010 12:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 13, 2010 1:47 am     Reply with quote

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: 19333

View user's profile Send private message

PostPosted: Mon Sep 13, 2010 3:32 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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