1

I wrote a program that automatically allocates memory for a structure and then sets a field within that structure equal to zero.

I thought, instead of setting only that member equal to zero, why not zero-out the entire block? In comes memset().

After using this function my program no longer worked. Once I attempted to assign a value to a field within the structure contained in that memory block the program crashed with a segmentation fault error.

So, this leads me to wonder - when a block of memory is allocated via malloc()/AUTO, is there other information stored in this location that is necessary for accessing and using this space?

typedef struct{ NodeT *nodes[MAX_SIZE]; int count; } stackT; stackT stack; stackT* ptr = &stack; void init(stackT* stackPtr){ //stackPtr->count = 0; memset(stackPtr, 0, sizeof(stackptr)); } 
3
  • Where are all the comments from this post ? Commented Sep 11, 2013 at 20:03
  • @cnicutar: Cleared because they had to do with the missing code. Did you want the floats conversation back? (Although maybe it should go somewhere else now, e.g. chat or a separate question.) Commented Sep 11, 2013 at 20:04
  • @minitech I don't want my comments back, no. I was just surprised with comments suddenly vanishing - not really used to that. Commented Sep 11, 2013 at 20:05

4 Answers 4

5

Please do a memset by passing the size of the structure and not size of the pointer:

memset(stackPtr, 0, sizeof(stackT)); 

Having said that, malloc would return a pointer to the address block (let us say of N bytes). Next, you need to pass the same pointer to memset and the size N.. The pointer returned by malloc does not contain any other information that can be affected by doing a memset.

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

Comments

4
memset(stackPtr, 0, sizeof(stackptr)); 

This is taking the size of the pointer; the first element of nodes will be zeroed, not count. Use sizeof(*stackptr) or sizeof(stackT).

Comments

3

memset() is the right function to use:

#include <string.h> #include <stdbool.h> typedef struct { char name[30]; int age; bool isMale; } MyStruct; MyStruct *myStruct = (MyStruct *)malloc(sizeof(MyStruct)); memset(myStruct, 0, sizeof(MyStruct)); 

The reason your code doesn't work as expected is because sizeof(stackptr) is the size of the pointer, not the struct.

7 Comments

Hmm, is it that memset is attempting to overwrite unallocated space? Otherwise, it seems that memset would then just be setting to zero a smaller portion of the total allocation - however the error is a segmentation fault.
I C, you should not be casting the return value of malloc().
You could replace those last two lines with a single call to calloc().
@sherrellbc Yes, unless the sizeof(struct) < sizeof(struct *), which is unlikely, given memory alignments would be at least 4 bytes on all modern platforms, I cannot see why your code would cause a SIGSEGV. It's possible the error does not exist in the code you have posted.
@PaulGriffiths Agreed; however calloc() is a function I've never ever used, but you are correct.
|
3

malloc() does indeed store information about the size of a block of memory it returns.

However, memset() does not know or care about the size. memset() will blindly write to the specified address for the number of specified bytes, regardless of what it might be overwriting. It is up to the programmer to ensure that only valid memory is written to.

And now that you've decided to post your code, I see you mistakenly use sizeof(stackptr) for the size instead of sizeof(stackT). The version you use is using the size of the pointer rather than the size of the data structure.

1 Comment

Perhaps sizeof(*stackPtr)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.