I have following code:
typedef struct node
{
pthread_mutex_t m;
int data;
struct node *next;
} node_t;
void init_node(node_t *n)
{
pthread_mutex_init(&(n->m),NULL); //NULL为默认的互斥锁
n->data = 6;
n->next = NULL;
};
int main()
{
node_t F; //F is a node_t type of object, but not a pointer variable.
init_node(&F);
node_t *p; //p is a node_t type pointer.
p = malloc(sizeof(node_t));
init_node(p);
return 0;
}
Apart from F is a node_t type of object variable and p is a same type of pointer variable, which F might take more memory space than the pointer variable, from linked list usage(inserting, searching, deleting, etc) point of view, what else the differences are there between above 2 variables?
(My English is bad, so perhaps there are some grammar mistakes, and please forgive me.)
F is a normal variable and p is a pointer, so you must use malloc to allocate some memory for variable p.
If you want to give the variable as a argument of a function:
void f1(node_t p){
//we use “p.data” to get data
}
//we can use: f1(F);f1(*p); to call the functions.
void f2(node_t p){
//we use “p->data” or “(p).data” to get data
}
//we can use: f2(&F);f2(p); to call the functions.
Because of the method of C to pass arguments, if we need to change values of F or p, we must use pointers.
Function f2 can use *F or *p to change data in the variable.
About memory:
sizeof(F) returns 8, because int is 4 and pointer is 4.
But all of the pointers’ size is 4 bytes.(Only for x86)
(in x64, pointers’ size is 8 bytes.)
@计算机知识杂谈:
Thanks for the reply.
First of all, your English is good.
The details of F, &F, and *F above are cleared when the variable declaration is given, and it also does to its memory consumption.
The key difference is that one is a variable with type node_t, and the other is a pointer which points to a anonymous variable with type node_t (which you created by malloc() )
Thanks for the answer, but this difference is known whenever the variable is declared.
As long as the variable is declared, like:
node_t F;
or
node_t *p;
we would/need understand the differences and its usages like your answer suggested.
My question is more about its usage. For example: when we have a F declared, any function calls that involve F would be an object passing, and the other would be considered as a reference or pointer passing. Like below:
push(node_t F) {
my code is here;
the function parameter is an object of node_t. which may take more memory than its counterpart.
}
However, when we have below:
push(node_t *p) {
my code is here.
p is a reference or pointer of node_t type, which may take less memory than the object.
}
push(node_t **p) {
my code is here.
when changing the header pointer in the linked list, this type of the parameter would be useful.
}
So, my question remains.
But, thanks for the answer anyway.
Linked list usage(inserting, searching, deleting, etc) point of view, or application scale view point, the way what I can think about is that, passing a pointer type of variable might be faster than object variable due to memory usage.