how to set pointer to a memory to NULL using memse

2019-01-26 18:30发布

I have a structure

typedef struct my_s {

   int x;
   ...
} my_T;

my_t * p_my_t;

I want to set the address of p_my_t to NULL and so far this is how I've tried to do this:

memset (&p_my_t, 0, sizeof(my_t*))

This doesn't not look right to me though. What is the correct way of doing this?


Amendment to question - asking a radically more complex question:

Here is what I am 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 it.
  • So I call a function, pass address of p_my_t to B to set its address to NULL in B so no other threads in B can use anymore
  • After call back from B, I then free memory in A

NB: there is no standard way to manage memory allocations via shared memory between processes. You will have to do some rather careful thinking about what is going on.

9条回答
萌系小妹纸
2楼-- · 2019-01-26 18:41

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

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

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

查看更多
爷、活的狠高调
3楼-- · 2019-01-26 18:42

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

查看更多
做自己的国王
4楼-- · 2019-01-26 18:47

Per your reply (elsewhere in this post) stating:

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

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

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

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

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

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

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

查看更多
时光不老,我们不散
5楼-- · 2019-01-26 18:48

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

p_my_t = NULL;

or the equivalent:

p_my_t = 0;
查看更多
手持菜刀,她持情操
6楼-- · 2019-01-26 18:51

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 sure it's worth defining a function to do this, but I think this answers the question you're trying to ask.

查看更多
小情绪 Triste *
7楼-- · 2019-01-26 18:56

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
查看更多
登录 后发表回答