0

I get a segment fault while it comes to the memset,i've looked anywhere can't know why?Can anyone tell me what's wrong?

typedef struct Ircsend1_struct{
  char type;
  char name[32];
}ircsend1_struct;

ircsend1_struct *ircpack;

char *pBuffer;
ircpack = (ircsend1_struct *)pBuffer;
memset(ircpack,0x00,sizeof(ircsend1_struct));

2 Answers 2

3

The assignment

ircpack = (ircsend1_struct *)pBuffer;  

is wrong. pBuffer is uninitialized. ircpack is pointing at unknown location. Therefore, the statement

memset(ircpack,0x00,sizeof(ircsend1_struct));  

will invoke undefined behavior.

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

Comments

2

Because your pointer is not initialized, maybe you mean

ircsend1_struct irpack;
char *pBuffer = &irpack;

memset(pBuffer, 0, sizeof(ircsend1_struct));

1 Comment

You are right.I've done what you write and problem solved.(ircsend1_struct) *ircpack is not initialized and causes segmentfault while memset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.