Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set a vector inside a struc to NULL

Status
Not open for further replies.

abcd12344445555

Programmer
May 10, 2009
24
0
0
BR
Given the following scenario:
Code:
struct a_s{
 u_int16_t cmd;
 u_int16_t dataSize;
 u_int8_t  data[MAX_DATA_SIZE];
};
typedef struct a_s   a_t;

struct b_s {
  u_int16_t part;
  u_int16_t serial;
  a_t       payload;
};
typedef struct b_s   b_t;


(...)

b_t message;

how can I set message.payload.data to NULL .
I cannot change any struct definition.

The statement bellow wont work :

Code:
&(message.payload.data) = NULL;  // it doesnt work : invalid lvalue in assignment

Thanks
Best regards.
P.
 
If by NULL (sematically, a pointer), you mean 0 (or the semantic nul character '\0'), then perhaps
[tt]memset( message.payload.data, 0, sizeof(message.payload.data) );[/tt]




--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 

That wouldn't cause some memory leak? Since it's automatically allocated a chunk of memory of u_int8t * MAX_DATA_SIZE bytes?

 
There is no memory to leak, because you didn't call malloc.

And since you declared an ARRAY inside your struct, you can't just make it go away when you're done with it. It always exists, and always takes up that fixed amount of space.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top