做的有什么区别:
ptr = (char **) malloc (MAXELEMS * sizeof(char *));
Run Code Online (Sandbox Code Playgroud)
要么:
ptr = (char **) calloc (MAXELEMS, sizeof(char*));
Run Code Online (Sandbox Code Playgroud)
什么时候使用calloc而不是malloc是一个好主意,反之亦然?
我们的代码涉及一个POD(Plain Old Datastructure)结构(它是一个基本的c ++结构,其中包含其他结构和POD变量,需要在开始时进行初始化.)
根据我所读到的,似乎:
myStruct = (MyStruct*)calloc(1, sizeof(MyStruct));
Run Code Online (Sandbox Code Playgroud)
应该将所有值初始化为零,如下所示:
myStruct = new MyStruct();
Run Code Online (Sandbox Code Playgroud)
但是,当以第二种方式初始化结构时,Valgrind后来抱怨"当使用这些变量时,条件跳转或移动取决于未初始化的值".我的理解是否存在缺陷,或者Valgrind是否会误报?
一名学生问了这个问题,我不确定.
猜测包括:"计数","清算","分块","完整",......
标准库文档没有说明它代表什么,并且没有类似命名的函数来指示模式.有没有人知道实际的词源,也许有一个权威的参考来支持它?
可能重复:
c malloc和calloc之间的区别
请解释一下这句话的意义,
malloc()和calloc()函数之间的另一个区别是malloc()函数分配的内存包含垃圾值,而calloc()函数分配的内存包含全零.
来源('C'编程,Salim Y. Amdani)
谢谢
我有一个结构,我把所有关于球员的信息.那是我的结构:
struct player{
int startingCapital;
int currentCapital;
int startingPosition;
int currentPosition;
int activePlayer;
int canPlay;
};
Run Code Online (Sandbox Code Playgroud)
这是我的主要内容:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{ int s,i,numOfPlayers;
struct player *players;
printf("Give the number of players: \n");
scanf("%d",&numOfPlayers);
players = (struct player *)calloc(numOfPlayers,sizeof(struct player));
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我要求用户给出玩家数量,然后我尝试分配所需的内存.但我得到这个编译错误,我无法弄清楚:
invalid application of `sizeof' to incomplete type `player'
Run Code Online (Sandbox Code Playgroud) 为什么要calloc
采用两个参数而不是一个malloc
?
具体来说,由于以下表达式之间没有区别(或存在?):
calloc (a, b);
calloc (b, a);
calloc (a * b, 1);
calloc (1, a * b);
Run Code Online (Sandbox Code Playgroud)
为什么不接受要分配的总字节数?这个界面背后的理由是什么?为什么这不适用于malloc?
我总是用Java编程,这可能就是为什么我对此很困惑:
在Java中我声明了一个指针:
int[] array
Run Code Online (Sandbox Code Playgroud)
并初始化它或为其分配一些内存:
int[] array = {0,1,0}
int[] array = new int[3]
Run Code Online (Sandbox Code Playgroud)
现在,在C中,这一切都让人感到困惑.起初我以为它就像宣布它一样容易:
int array[]
Run Code Online (Sandbox Code Playgroud)
并初始化它或为其分配一些内存:
int array[] = {0,1,0}
int array[] = malloc(3*sizeof(int))
int array[] = calloc(3,sizeof(int))
Run Code Online (Sandbox Code Playgroud)
除非我错了,以上所有内容都等同于Java-C,对吗?
然后,今天我遇到了一个代码,其中我发现了以下内容:
pthread_t tid[MAX_OPS];
Run Code Online (Sandbox Code Playgroud)
以及下面的一些行,没有任何初始化......
pthread_create(&tid[0],NULL,mou_usuari,(void *) 0);
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是(至少对我而言)代码有效!至少在Java中,会返回一个很好的"NullPointerException"!
所以,按顺序:
我是否正确使用所有Java-C"翻译"?
为什么该代码有效?
使用malloc(n*sizeof(int))
和之间有什么区别calloc(n,sizeof(int))
吗?
提前致谢
从man realloc:realloc()函数返回一个指向新分配的内存的指针,该内存适用于任何类型的变量,可能与ptr不同,如果请求失败则为NULL.
所以在这段代码中:
ptr = (int *) malloc(sizeof(int));
ptr1 = (int *) realloc(ptr, count * sizeof(int));
if(ptr1 == NULL){ //reallocated pointer ptr1
printf("Exiting!!\n");
free(ptr);
exit(0);
}else{
free(ptr); //to deallocate the previous memory block pointed by ptr so as not to leave orphaned blocks of memory when ptr=ptr1 executes and ptr moves on to another block
ptr = ptr1; //deallocation using free has been done assuming that ptr and ptr1 do not point to the same address
}
Run Code Online (Sandbox Code Playgroud)
仅仅假设重新分配的指针指向不同的记忆块而不是同一个块就足够了.因为如果假设变为false并且realloc返回ptr指向的原始内存块的地址然后free(ptr)执行(由于评论中给出的原因)然后内存块将被删除,程序将疯狂.我应该放入另一个条件来比较ptr和ptr1的相等性并排除执行free(ptr)语句吗?
我饶有兴趣地阅读了malloc和calloc之间的后C差异.我在我的代码中使用了malloc,想知道我使用calloc会有什么不同.
我目前的(伪)代码与malloc:
场景1
int main()
{
allocate large arrays with malloc
INITIALIZE ALL ARRAY ELEMENTS TO ZERO
for loop //say 1000 times
do something and write results to arrays
end for loop
FREE ARRAYS with free command
} //end main
Run Code Online (Sandbox Code Playgroud)
如果我使用calloc而不是malloc,那么我将:
Scenario2
int main()
{
for loop //say 1000 times
ALLOCATION OF ARRAYS WITH CALLOC
do something and write results to arrays
FREE ARRAYS with free command
end for loop
} //end main
Run Code Online (Sandbox Code Playgroud)
我有三个问题:
如果阵列非常大,哪个场景更有效?
如果阵列非常大,哪个场景会更有时间效率? …
calloc ×10
c ×8
malloc ×5
c++ ×2
struct ×2
arrays ×1
libc ×1
memory-leaks ×1
new-operator ×1
optimization ×1
realloc ×1
valgrind ×1