0

I am running gcc 4.8.4 and compiling with options:

CXXFLAGS+= -Wall -std=c++11 -pedantic -lpthread 

I want to zero a structure using memset:

typedef struct { TSfMsgHeader errorHeader; TErrorHeader errorType; TErrorDetails errorDetails; }TErrorInd; uint8 g_errorIndBlock[16]; TErrorInd* p_msg = (TErrorInd *)&g_errorIndBlock[0]; memset((int*)p_msg, 0, sizeof(TErrorInd)); 

This results in warning:

In function ‘void* memset(void*, int, size_t)’, inlined from ‘void sendMsgPduError(TMsgPduError*, uint32)’ at ../MessageHandling.cpp:174:46:

/usr/include/x86_64-linux-gnu/bits/string3.h:84:70: warning: call to void* __builtin___memset_chk(void*, int, long unsigned int, long unsigned int) will always overflow destination buffer [enabled by default]

return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));

I realise that this is a sensible warning but I don't know how to modify the code to fix it.

I read that std::fill_n is preferred to memset. Is that correct?

If so, how would I replace memset by fill_n?

2
  • 1
    Besides, why do you need to cast into int*? Commented Aug 18, 2016 at 10:30
  • g_errorIndBlock[16]; why 16? how do you know? Commented Aug 18, 2016 at 10:51

2 Answers 2

2

Check the value of sizeof(TErrorInd), for some reason gcc thinks that it is greater than sizeof(uint8) * 16. Maybe you did not counted alignment bytes, computing the struct size.

Sign up to request clarification or add additional context in comments.

Comments

2

-std=c++11 is a rather unusual flag for what's really C89 code. But I digress.

The proper fix is just

TErrorInd p_msg { 0 }; 

No char[] (improper alignment), no memset (not needed as it's already zeroed)

1 Comment

In the original post there's no TErrorInd data. p_msg is TErrorInd*.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.