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.
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 doTrying 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 astruct my_T
), then you can properlymemset()
it:This will zero out the entire structure, setting all fields to zero.
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 formp_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 usingp_my_t = NULL
instead of memset.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.