Suppose there exists a function which returns a message say of the following format:
struct message
{
void* data;
}msgG;
Which would be the best way to extract the data (i.e. Get the message accessible to fun1 in the code): 1- using a global variable 2- Using double pointers(pointer to a pointer)
//Note: msgG is the global variable
void fun2(struct message **ptr)
{
**ptr = msgCreate(); // msgCreate returns a type struct message;
msgG = msgCreate();
}
void fun1()
{
....
.....
struct message *ptr;
ptr = malloc(sizeof(struct message));
fun2(&ptr);
...
}
Now we have the message stored in msgG and ptr ? Which is the better one? Using global variable or accessing the pointer since one is allocated in the heap and the other in the bss(not sure of this)?? Is there any other way to deal with this kind of situation?