[英]Why do we need calloc (or malloc)?
After ignoring C for my entire CS career I have decided to give it a look! 在我整个CS职业生涯中忽略了C后,我决定试一试!
When initialising variables, we can have : 在初始化变量时,我们可以:
int b = 0;
This initialises b, allocates memory for it, and we can later update it with 这初始化b,为它分配内存,我们以后可以用它来更新它
b = 2;
if needs be. 如果需要的话。
So, and forgive me for this ridiculously "noob" question but why do we need calls like : 所以,原谅我这个荒谬的“noob”问题,但为什么我们需要这样的电话:
double *b = (double *) calloc(n, sizeof(double));
when initialising the variable would allocate the space for it already? 初始化变量时会为它分配空间吗?
Why can we not just do 为什么我们不能这样做
double b = 0;
b* = b.addressOf(b) //or some similar construct.
What is the use of this? 有什么用?
I have tried Googling this to no avail so please forgive me - ufortunately * in Google is a wildcard and so relevant results are hard to find. 我试过谷歌这个无济于事所以请原谅我 - 不幸的是*在谷歌是一个通配符,因此很难找到相关的结果。
Variables declared in the current context end their lifetime at the end of the context. 在当前上下文中声明的变量在上下文结束时终止它们的生命周期。
Allocating memory gives you space to store longer-lived variables. 分配内存为您提供了存储寿命较长变量的空间。
For example, 例如,
double *foo() {
double d;
return &d;
}
void bar() {
double *d = foo();
*d = 0.0;
}
will try to access a variable that no longer exists, because its lifetime is the foo
function. 将尝试访问不再存在的变量,因为它的生命周期是
foo
函数。
C and C++ do not keep track of objects. C和C ++不跟踪对象。 A pointer only points to the object, but does not extend object lifetime, so it is entirely possible for a pointer to be invalid even if it is not
NULL
. 指针只指向对象,但不延长对象的生命周期,因此即使指针不是
NULL
指针也完全有效。
However, this is valid: 但是,这是有效的:
double *foo() {
return (double *)malloc(sizeof(double));
}
void bar() {
double *d = foo();
*d = 0.0;
}
This will allocate memory for a double
, and return the pointer to the memory, which remains valid until explicitly returned to the pool using the free
function. 这将为
double
分配内存,并将指针返回到内存,该内存在使用free
函数显式返回池之前保持有效。 Not returning it to the pool will create a memory leak. 不将其返回池将导致内存泄漏。
除非我完全弄错,否则在C中, calloc
或malloc
是实现动态数据结构的唯一可能性。
When it comes about variable allocation you can do it like: 当涉及变量分配时,您可以这样做:
int a = 10
. int a = 10
。 These variables are defined on the stack most possibly together with some of the code using them (this is why it can be dangerous to write in an array declared on the stack without proper checking the boudadries. You might overwrite code). calloc()
or some other memory allocation function. calloc()
或其他一些内存分配函数。 These variables are declared in an area known as the heap, or dynamic memory. free
d using free()
. free()
free
使用。 You always should free the memory to avoid memory leaks. Scope is the region or section of the code where a variable can be accessed. 范围是可以访问变量的代码的区域或部分。 There can be
可以有
Example 例
#include<stdio.h>
void function1()
{
printf("In function1\n");
}
static void function2()
{
printf("In function2\n");
{
int i = 100;
Label1:
printf("The value of i =%d\n",i);
i++;
if(i<105)
goto Label1;
}
}
void function3(int x, int y);
int main(void)
{
function1();
function2();
return 0;
}
In the example, 在这个例子中,
The variable i
in the above example have the block scope. 上例中的变量
i
具有块范围。 If the control goes out of scope (life ends), then the variable is gone. 如果控件超出范围(生命结束),则变量消失。 You can not access the variable.
您无法访问该变量。
So C provides dynamic memory constructs to access the memory in these kind of scenarios. 所以C提供动态内存构造来访问这种场景中的内存。 For example:
例如:
int* function(void)
{
int *ptr = malloc(sizeof(int));
*ptr = 5;
return ptr;
}
int main(void)
{
printf("%d", function());
return 0;
}
the printf
would still print the value even the variable ptr
is out of scope but the memory pointed by ptr
still exists (has life). 即使变量
ptr
超出范围, printf
仍会打印该值,但ptr
指向的内存仍然存在(有生命)。
Also read https://stackoverflow.com/a/18479996/1814023 另请阅读https://stackoverflow.com/a/18479996/1814023
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.