首页 新闻 会员 周边

difference between an object and pointer variable

0
悬赏园豆:5 [已解决问题] 解决于 2021-10-19 14:10

I have following code:

include <assert.h>

include <errno.h>

include <pthread.h>

include <stdio.h>

include <unistd.h>

include "linkedlist.h"

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?

blogger2020的主页 blogger2020 | 菜鸟二级 | 园豆:257
提问于:2021-07-05 13:17
< >
分享
最佳答案
0

(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.

收获园豆:3
计算机知识杂谈 | 菜鸟二级 |园豆:470 | 2021-10-14 22:17

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.)

计算机知识杂谈 | 园豆:470 (菜鸟二级) | 2021-10-14 22:19

@计算机知识杂谈:
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.

blogger2020 | 园豆:257 (菜鸟二级) | 2021-10-19 14:05
其他回答(2)
0

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() )

收获园豆:2
从今往后^-^ | 园豆:208 (菜鸟二级) | 2021-07-05 23:15

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.

支持(0) 反对(0) blogger2020 | 园豆:257 (菜鸟二级) | 2021-07-06 11:45
0

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.

blogger2020 | 园豆:257 (菜鸟二级) | 2021-10-19 14:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册