[ACCEPTED]-how to set pointer to a memory to NULL using memset?-memset

Accepted answer
Score: 21

Don't use memset to initialize a null pointer 3 as this will set the memory to all bits 2 zero which is not guaranteed to be the representation 1 of a null pointer, just do this:

p_my_t = NULL;

or the equivalent:

p_my_t = 0;
Score: 4

What exactly are you trying to do? p_my_t is 10 already a pointer, but you haven't allocated 9 memory for it. If you want to set the pointer 8 to NULL, simply do

p_my_t = NULL;

Trying to dereference 7 this pointer will result in a segmentation 6 fault (or access violation on Windows).

Once 5 the pointer actually pointers to something 4 (e.g. via malloc() or by assigning to it the address 3 of a struct my_T), then you can properly memset() it:

memset(p_my_t, 0, sizeof(struct my_T));

This will 2 zero out the entire structure, setting all 1 fields to zero.

Score: 2

The recommended code for setting a pointer 11 to null is assigning 0 (zero). Bjarne Stroustrup 10 does it :) Anyway it is just as expressive as 9 NULL and does not depend on a macro definition.

Note 8 that NULL is not a keyword, it is not reserved 7 and while it would be confusing to redefine, nothing 6 says that you should not (more than style). A 5 coworker often jokes about defining NULL 4 to something different than 0 in some header 3 just to see how other people's code behave.

In 2 the upcoming standard there will be a more 1 expressive nullptr keyword to identify a null pointer.

Score: 2

I think maybe you want

extern void set_t_pointer_to_null(my_T *pp);

and call

set_t_pointer_to_null(&p_my_t);

where

void set_t_pointer_to_null(my_T *pp) { *pp = NULL; }

I'm not 3 sure it's worth defining a function to do 2 this, but I think this answers the question 1 you're trying to ask.

Score: 0

Thanks, here is what I an trying to do

  • two processes, A and B
  • malloc p_my_t in A, B has N threads and can access it
  • start deleting in A but I can not simply free it since threads in B may still using.
  • so I call a function, pass address of p_my_t to B to set its address to NULL in B so no others threads in B can use anymore
  • After call back from B, I then free memory in A

0

Score: 0

Per your reply (elsewhere in this post) stating:

Thanks, here 27 is what I an trying to do

  • two processes, A and B
  • malloc p_my_t in A, B has N threads and can access it
  • start deleting in A but I can not simply free it since threads in B may still using.
  • so I call a function, pass address of p_my_t to B to set its address to NULL in B so no others threads in B can use anymore
  • After call back from B, I then free memory in A

What you need is 26 some form of synchronization between all 25 your threads and processes. I'm not sure 24 of how you are sharing this object between 23 processes, but I suspect you are using shared 22 memory.

Normally I would recommend using 21 a shared pointer class (such as Boost's 20 shared_ptr class), but I'm not sure of how well that 19 would work in this scenario. You may want 18 to consider tweaking your class so that 17 it tracks its own references and can be 16 used with the Boost intrusive_ptr class.

That way, process 15 A can simply forget about the object, and 14 when process B is finished, the instance 13 of my_T will know that there are no more references 12 left and clean itself up.

The synchronization 11 would come into play here when my_T is adding 10 or removing references internally (so you 9 don't run into nasty race conditions where 8 it thinks it should clean itself up but 7 is really still in use).

One other approach 6 that has a bit more of a "kluge" feel 5 to it is to give each instance of my_T an "is-valid" flag 4 so that all processes/threads using it will 3 know whether or not to continue doing so.

For 2 more details on Boost's various pointer 1 classes, check out their documentation.

Score: 0

From reading your multi-thread comments 3 I should say there is no safe sequence of 2 code to accomplish your task. You will have 1 to step back and reexamine your algorithm.

Score: 0

If I get it right, memset won't solve your 8 problem. If A and B are separate processes, then 7 p_my_t in process A will be different form p_my_t in 6 process B. You just can't pass a pointer 5 between different processes. I sugest you 4 use some kind of IPC mechanism in order 3 to sinchronyze your two processes (message 2 queues, for example), and just using p_my_t = NULL instead 1 of memset.

Score: 0

Per your update, it seems to me that what 5 you are really trying to do is guard access 4 to a resource, which means you should use 3 a read/write lock that is shared between 2 the processes to guard the ptr to that resource, and 1 test the ptr before using.

  • Allocate the structure in shared memory.
  • Allocate the ptr to the structure in shared memory.
  • Guard access to the ptr to the structure with a Read/Write lock.
  • Process A should acquire a WRITE lock to the ptr when initializing or invalidating the ptr and structure.
  • Process B should acquire a READ lock to the ptr, and test that the ptr is valid before using the structure

More Related questions