Reputation: 1727
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?
Upvotes: 0
Views: 159
Reputation: 273646
It can be as simple as this:
struct message
{
void* data;
} msgG;
void fun2(struct message the_msg)
{
/* access the_msg.data */
}
void fun1()
{
struct message *ptr;
ptr = malloc(sizeof(struct message));
ptr->data = ... /* initialize it to something */
fun2(*ptr);
}
But this way, fun2
won't be able to manipulate the_msg
, because it's passed a copy of the structure by-value. It will be able to manipulate the stuff pointed to by the data
pointer inside the_msg
, because that's a pointer.
If you want to manipulate the contents of the_msg
itself, such as retarget the data
pointer, fun2
should accept a pointer to message
(a double pointer is unnecessary for this).
And a global variable is almost always a bad solution. Don't use it.
Upvotes: 0
Reputation: 96191
Don't use a global variable. What you're trying to do can be done this way:
void fun2(struct message *ptr)
{
*ptr = msgCreate();
}
void fun1()
{
struct message *m = malloc(sizeof *m);
if (m == NULL) {
/* error handling */
}
fun2(m);
}
If struct message
is big, consider not having a function returning such a struct
. In most of the cases, it is more efficient to return a pointer to some memory than to return a big automatic variable from a function.
Upvotes: 3