[英]What happens exactly when you pass by value pointer that is already allocated?
What happens here in terms of allocation ? 在分配方面会发生什么? is it faulty situation to use pointers like this? 使用这样的指针是否存在错误情况?
void f(int p[])
{
p = (int*)malloc(sizeof(int));
*p = 0;
}
int main()
{
int *q = 0;
q = (int*)malloc(sizeof(int));
*q = 1;
f(q);
return 0;
}
The short answer is that p
and q
are independent variables. 简短的答案是p
和q
是自变量。 So first p
will be assigned the same value as q
and then p
gets a new value due to the malloc
. 因此,首先将为p
分配与q
相同的值,然后由于malloc
p
得到一个新值。 q
is not changed by the function call. q
不能通过函数调用更改。 However, there is a memory leak due to p
(and q
) not being freed. 但是,由于没有释放p
(和q
),因此存在内存泄漏。
You can see this using a few prints. 您可以通过一些印刷品看到它。
#include <stdio.h>
#include <stdlib.h>
void f(int p[])
{
printf("--------------------\n");
printf("p is now %p\n", (void*)p);
printf("p points to the value %d\n", p[0]);
p = (int*)malloc(sizeof(int));
*p = 0;
printf("--------------------\n");
printf("p is now %p\n", (void*)p);
printf("p points to the value %d\n", p[0]);
printf("--------------------\n");
}
int main(){
int *q = 0;
q = (int*)malloc(sizeof(int));
*q = 1;
printf("q is now %p\n", (void*)q);
printf("q points to the value %d\n", q[0]);
f(q);
printf("q is now %p\n", (void*)q);
printf("q points to the value %d\n", q[0]);
return 0;
}
The output (with a few comments to explain): 输出(带有一些解释说明):
q is now 0x1912010 // In main q is initialized
q points to the value 1 // In main the location q points to is initialized
--------------------
p is now 0x1912010 // In function p first has the same value as q
p points to the value 1 // so it also points to the same as q
--------------------
p is now 0x1913040 // In function p gets a new value due to malloc
p points to the value 0 // and the pointed to memory gets a new value
--------------------
q is now 0x1912010 // Back in main q is the same as before the function call
q points to the value 1 // and the pointed to memory is unchanged as well
What I guess your question is about, it's the assignment 我想你的问题是关于作业
p = malloc(...)
in the function f
. 在函数f
。
That's a fully valid assignment, and works like any other assignment. 这是完全有效的分配,并且与其他分配一样工作。
Consider this code: 考虑以下代码:
void f(int p)
{
p = 0;
}
int main(void)
{
int q;
q = 1;
f(q);
}
In the function f
there's a reassignment of the variable p
, just like in your shown code. 在函数f
,就像在您显示的代码中一样,对变量p
了重新分配。 It's really just the same as for your code. 它实际上与您的代码相同。 It doesn't matter if p
is a plain int
variable, or a pointer variable. p
是普通的int
变量还是指针变量都没有关系。 You can still reassign it as much as you want. 您仍然可以根据需要重新分配它。
The thing to note is that arguments in C are passed by value . 需要注意的是C语言中的参数是按值传递的 。 That means the value of the argument is copied into the functions argument variable ( p
in your case and in my example). 这意味着将参数的值复制到函数参数变量中(在您的情况下,在我的示例中为p
)。 Modifying the copy (ie p
) will of course not modify the original. 修改副本 (即p
)当然不会修改原件。 All modifications that the function f
does will be lost once it returns. 函数f
返回的所有修改都将丢失。
So in my example, if you print the value of q
after the call f(q)
, then it would show that q
is equal to 1
. 因此,在我的示例中,如果在调用f(q)
之后打印q
的值,那么它将表明q
等于1
。
whenever you ask for allocation on heap using malloc for example- you have to free it, if you don't you get a memory leak- so for 2 mallocs here you must use 2 frees. 例如,每当您使用malloc请求在堆上分配时-您必须释放它,如果没有,则会发生内存泄漏-因此,对于此处的2个malloc,您必须使用2个frees。
And you need to keep in mind that when you send q to the function you send it BY VALUE so if you check *q in main it will still stay 1. 而且需要记住的是,当您将q发送到函数时,将按值发送它,因此,如果在main中选中* q,它将仍然保持1。
if you want to change the value q is pointing to in the function you can send f(int **). 如果要更改函数中q指向的值,可以发送f(int **)。
in your example a way to go if you want to change a where a pointer is pointing and to avoid memory leak is: 在您的示例中,要更改指针指向的位置并避免内存泄漏的方法是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f(int** p)
{
free(*p); /*first free the initial main allocated memory on heap */
*p = malloc(sizeof(int)); /*now the pointer is pointing to the new allocation */
if(NULL == *p)
{
return;
}
**p = 0;
}
int main(){
int *q = NULL;
q = malloc(sizeof(int)); /*ask for memory allocation */
if(NULL != q)
{
*q = 1;
f(&q);
printf("%d\n", *q); /*now pointer is pointing to the allocationasked for by f */
free(q); /*free the allocated place on heap */
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.