how to set pointer to a memory to NULL using memset?

后端 未结 9 858
执笔经年
执笔经年 2020-12-11 08:35

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

相关标签:
9条回答
  • 2020-12-11 09:14

    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 讨论(0)
  • 2020-12-11 09:15

    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.

    0 讨论(0)
  • 2020-12-11 09:19

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

    0 讨论(0)
  • 2020-12-11 09:24

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

    p_my_t = NULL;
    

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

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

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

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

    0 讨论(0)
  • 2020-12-11 09:31

    Per your update, it seems to me that what you are really trying to do is guard access to a resource, which means you should use a read/write lock that is shared between the processes to guard the ptr to that resource, and 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
    0 讨论(0)
  • 2020-12-11 09:32

    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.

    0 讨论(0)
提交回复
热议问题