有效率(我是否正确?) [英] efficeincy ( Am I correct ?)

查看:55
本文介绍了有效率(我是否正确?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有


我们都熟悉calloc和malloc,现在我们知道

calloc进行初始化,而这在malloc中没有实现

为不同的数据类型分配字节。


现在我对这两个问题的关注更有效率,


为了避免任何mallacisous行为,我们逐步采用calloc,

无论如何,calloc和malloc都在连续的
memeory位置分配字节。 (因为calloc也是初学者)


现在我认为malloc对calloc的效率更高

背后的原因是Calloc有额外的开销<当我们只想要

来分配字节时(不需要初始化它)

(char) *)abcd =(char *)calloc(2,100);

上面的


(char *)abcd =(char *)malloc相同(200);

memset(abcd,0,1 * 200); //系统调用


现在这意味着calloc的效率不比

malloc高得多。

但是请看下面的情况

假设我们必须为int数据类型分配内存

并用零初始化它,


案例1:


(int *)abcd =(int *)malloc(100);

memset(abcd,0,sizeof(int )* 100); //用户名为memset()


案例2:

(int *)abcd =(int *)calloc(1,100);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

以上等于

(int *)abcd =(int *)malloc(100);

memset(abcd,0,sizeof(int) * 100); //系统名为memset()

因此在案例1中我们有用户称为memset函数,因此

案例1对于案例2是无效的,因此制作案例

2效率更高。

现在假设我们要再次为int数据分配内存

类型并用2初始化它,


案例A:


(int *)abcd =(int *)malloc(100);

memset(abcd,2,sizeof(int)* 100); //用户名为memset()


案例B:


(int *)abcd =(int *)calloc(1,100 );

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

以上等于

(int *)abcd =(int *)malloc(100);

memset(abcd,0, sizeof(int)* 100); //系统名为memset()

memset(abcd,2,sizeof(int)* 100); //用户名为memset()

所以我们看到案例A与CASe B相比有效率

因此在案例A中只有一次memset()是调用,而在

的情况下B调用两次memset()函数。 (即Usser和系统)

因此,在这种情况下,Malloc更有效率。


因此,这使得calloc的效率方面的差异< br $>
和malloc,


我们必须得到默认的intialiosation然后总是使用

calloc和我们必须使用的那些指定的初始化然后

总是使用malloc。


请指导我因为我试图获得

之间的差异calloc和malloc ..这是我猜测它可能是一个不同的

。请让我知道mi是否正确或我错过了什么。


在此先感谢

Ranjeet

Dear All

we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.

Now my concern about these two are which is more efficent to use,

To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)

Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)
(char *) abcd = (char *)calloc (2, 100);

above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called

Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,

Case 1:

(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset()

Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,

Case A:

(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()

Case B:

(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()

memset(abcd, 2, sizeof(int) * 100); // The usser called memset()
So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.

So this make the diffrence in the terms of the Efficency of the calloc
and malloc,

where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.

Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.

Thanks In Advance
Ranjeet

推荐答案

ra *********** @ gmail.com 写道:
亲爱的所有

我们都熟悉calloc和malloc,现在我们知道了calloc做了初始化在分配不同数据类型的字节时,没有在malloc中实现。

现在我对这两个问题的关注更有效率,

避免任何我们逐步采用calloc的mallacisous行为,无论如何,calloc和malloc都会在连续的memeory位置分配字节。 (因为calloc也是初学者)

现在我认为malloc对calloc更有效率
背后的原因是Calloc正在使用Zero来获得额外的开销
initailisaig (默认),当我们只是想要分配字节时(不需要初始化它)

(char *)abcd =(char *)calloc(2,100); <上面的内容与
(char *)abcd =(char *)malloc(200);
memset(abcd,0,1 * 200)相同; //系统调用

现在这意味着calloc与由于头顶的情况相比没有那么高效。

但是请看下面的情况
假设我们必须为int数据类型分配内存并用Zero初始化它,

案例1:

(int *)abcd = (int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //用户名为memset()
这不是正确的,但可能是一个typeo。你可能想要

这样的东西:

(int *)abcd =(int *)malloc(sizeof(int)* 100);

memset(abcd,0,sizeof(int)* 100);


另一件事,演员没有意义,因为malloc返回void *。

案例2:
(int *)abcd =(int *)calloc(1,100);
^^^^^^^^^^^^^^^^^^^^^^^^上面的^^^^^^^^^^^^^^


(int *)abcd =(int *)malloc(100);
memset (abcd,0,sizeof(int)* 100); //系统名为memset()
不,不完全,但这可能是因为typeo'。


(int *)abcd =(int *) calloc(100,sizeof(int));

上面的
等于

(int *)abcd =(int *)malloc(sizeof(int) * 100);

memset(abcd,0,sizeof(int)* 100);


现在你的陈述是真的: - )。

因此,在案例1中,我们将用户称为memset功能,因此案例1对于案例2是无效的,因此使案例更有效。

现在假设我们要再次为int数据分配内存类型并用2初始化它。

案例A:

(int * )abcd =(int *)malloc(100);
memset(abcd,2,sizeof(int)* 100); //用户名为memset()

案例B:

(int *)abcd =(int *)calloc(1,100);
^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面等于
(int *)abcd =(int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //系统名为memset()
memset(abcd,2,sizeof(int)* 100); // usser叫memset()
再说不完全(和上面描述的很相似),但我想我得到了

你的观点。


另外一点是memset不会将int'初始化为2.

Memset会将内存中的每个字节初始化为两个。


因此,我们看到案例A与CASe B相比是有效的。因此,只有在调用memset()时才使用案例A,而在案例B中,调用两次memset()函数。 (即Usser和系统)
因此,在这种情况下,Malloc更有效率。

因此,这使得calloc的效率和malloc,

我们必须得到默认的intialiosation然后总是使用
calloc,我们必须使用一些指定的初始化然后
总是使用malloc。

请指导我因为我试图获得
calloc和malloc之间的差异..这是我猜测它可能也是一种差异。请让我知道mi是否正确或我错过了什么。
Dear All

we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.

Now my concern about these two are which is more efficent to use,

To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)

Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)

(char *) abcd = (char *)calloc (2, 100);

above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called

Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,

Case 1:

(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset() This is not right thos, but that might be a typeo. You probably wanted
something like this:
(int *) abcd = (int *)malloc(sizeof(int) * 100);
memset(abcd, 0, sizeof(int) * 100);

Another thing, the cast has no meaning since malloc returns void*.

Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset() No, not quite, but that is probably because of typeo''s.

(int *) abcd = (int *)calloc(100, sizeof(int));

above is equivelent to
(int *) abcd = (int *)malloc(sizeof(int) * 100);
memset(abcd, 0, sizeof(int) * 100);

Now your statement is true :-).

So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,

Case A:

(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()

Case B:

(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()

memset(abcd, 2, sizeof(int) * 100); // The usser called memset() Again not quite (much the same as described above), but I think I get
your point.

Another thing is that the memset will NOT initialize the int''s to 2.
Memset will initialize each byte in the memory to two.


So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.

So this make the diffrence in the terms of the Efficency of the calloc
and malloc,

where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.

Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.



除了你的代码错了 - 我指出了几个错误

以上 - 你的想法非常正确。当

缓冲区需要初始化为零时你应该使用calloc,而在所有其他情况下你应该使用malloc。


说到memset它不会做你认为它做的事。它将
将内存缓冲区中的所有字节初始化为你给它的值,

但是因为sizeof(int)usaly是> 1你不能用它来初始化

int'到例如2.


-

bjrnove


Except from you code to be wrong - I pointed out a couple of errors
above - your idea is pretty much right. You should use calloc when the
buffer needs to be initialized to zero and malloc in all other cases.

When it comes to memset it doesn''t do what you think it does. It
initializes all bytes in the memory buffer to the value you give it,
but since sizeof(int) usaly are > 1 you can''t use it to initialize
int''s to for example 2.

--
bjrnove


ra ***** ******@gmail.com 写道:
亲爱的所有

我们都熟悉calloc和malloc,现在我们知道了calloc在为malloc
分配不同数据类型的字节时进行初始化。

现在我对这两个问题的关注更有效,

为了避免任何不道德的行为,我们逐步采用calloc,
无论如何,calloc和malloc都会在连续的记忆位置分配字节。 (因为calloc也是初学者)

现在我认为malloc对calloc更有效率
背后的原因是Calloc正在使用Zero来获得额外的开销
initailisaig (默认),当我们只是想要
分配字节时(不需要初始化它)

(char *)abcd =(char *)calloc(2,100); <上面的内容与
(char *)abcd =(char *)malloc(200);
memset(abcd,0,1 * 200)相同; //系统调用

现在这意味着calloc与由于头顶的情况相比没有那么高效。

但是请看下面的情况
假设我们必须为int数据类型分配内存并用Zero初始化它,

案例1:

(int *)abcd = (int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //用户名为memset()

案例2:
(int *)abcd =(int *)calloc(1,100);
^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面是等于
(int * )abcd =(int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //系统名为memset()

因此在案例1中我们有用户称为memset功能,因此案例1对于案例2是无效的,因此使案例
2更高效。

现在假设我们要再次为int数据分配内存类型并用2初始化它,

案例A:

(int *)abcd =(int *)malloc(100);
memset(abcd,2,sizeof(int)* 100); //用户名为memset()

案例B:

(int *)abcd =(int *)calloc(1,100);
^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面等于
(int *)abcd =(int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //系统名为memset()
memset(abcd,2,sizeof(int)* 100); //用户名为memset()

所以我们看到案例A与CASe B相比是有效的
因此只有在案例A中调用memset()时才会调用
情况B两次调用memset()函数。 (即Usser和系统)
因此,在这种情况下,Malloc更有效率。

因此,这使得calloc的效率和malloc,

我们必须得到默认的intialiosation然后总是使用
calloc,我们必须使用一些指定的初始化然后
总是使用malloc。

请指导我因为我试图获得
calloc和malloc之间的差异..这是我猜测它可能也是一种差异。请让我知道mi是否正确或我错过了什么。

在此先感谢
Ranjeet
Dear All

we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.

Now my concern about these two are which is more efficent to use,

To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)

Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)
(char *) abcd = (char *)calloc (2, 100);

above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called

Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,

Case 1:

(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset()

Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,

Case A:

(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()

Case B:

(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()

memset(abcd, 2, sizeof(int) * 100); // The usser called memset()
So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.

So this make the diffrence in the terms of the Efficency of the calloc
and malloc,

where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.

Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.

Thanks In Advance
Ranjeet




这取决于哪个你正在使用的编译器(和/或std库)。


gcc调用一个名为bzero的函数将init数组调为0.


MS VC ++使用calloc的2个不同版本:一个非常复杂

(并调用memset),另一个迭代数组上的指针,如:


while(startptr < lastptr)

* startptr ++ = 0;

另外:


问问自己实际需要多少次零初始化数组。

通常你会自己初始化数组,并且在每个(大多数)元素中使用不同的数据

,所以我建议使用malloc


在过去的7年里我写了大约250,000行C,而且我认为我只用了几次就使用了calloc。


另外:在上面的案例B中,为什么要使用calloc如果你知道你想要

数组初始化为2而不是0?这种说明了我的观点。


另外:除非您正在为嵌入式系统编写代码,或者在其他资源受限的架构上运行

,否则额外的调用(或循环)

将init数组归为零真的很重要吗?


如果你要分配一组结构怎么办?

初始化为0可能没有意义,所以你必须使用malloc,然后

手动初始化每个元素(memset不能工作)。


HTH。



It depends on which compiler (and or std library) you are using.

gcc calls a function called bzero to init array to 0.

MS VC++ uses 2 different versions of calloc: One is VERY complicated
(and does call memset) and the other iterates a pointer over the array like:

while ( startptr < lastptr )
*startptr++ = 0;
ALSO:

Ask yourself how often you actually NEED a zero initialized array.
Usually you will initialize the array yourself, and with different data
in each (most) elements aswell, so I suggest using malloc

I have written probably 250,000 lines of C over the past 7 years, and I
think I''ve only used calloc a handful of times.

Also: in Case B above, why would you use calloc if you knew you wanted
the array initialized to 2 and not 0? This kind of illustrates my point.

Also: unless you are writing code for embedded systems, or to run on
other resource constrained architectures, does the extra call (or loop)
to init array to zero really matter?

And what if you are allocating an array of structs?
Initializing to 0 may not make sense, so you have to use malloc, then
init each element manually (memset won''t work).

HTH.


2005-06-09 10:17:14 -0400, ra *********** @ gmail.com 说:
On 2005-06-09 10:17:14 -0400, ra***********@gmail.com said:
亲爱的所有
我们都熟悉calloc和malloc,现在我们知道了calloc做了初始化,这在malloc
中没有实现,同时为不同的数据类型分配了字节。

现在我关注这两个是更有效的使用,


写代码tha t做你想做的,测量它*然后*做

决定什么需要优化。


为了避免任何恶意行为我们生活采用calloc ,
无论如何,calloc和malloc都在连续的记忆位置分配字节。 (因为calloc也是初学者)

现在我认为malloc对calloc更有效率
背后的原因是Calloc正在使用Zero来获得额外的开销
initailisaig (默认),当我们只是想要
分配字节时(不需要初始化它)


可能是,它可能不是。

(char *)abcd =(char *)calloc(2,100);

上面与
(char *)abcd =(char *)malloc(200)相同; memset(abcd,0,1 * 200); //系统调用

现在这意味着calloc与由于头顶的情况相比没有那么高效。

但是请看下面的情况
假设我们必须为int数据类型分配内存并用Zero初始化它,

案例1:

(int *)abcd = (int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //用户名为memset()

案例2:
(int *)abcd =(int *)calloc(1,100);
^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面是等于
(int * )abcd =(int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //系统名为memset()

因此在案例1中我们有用户称为memset功能,因此案例1对于案例2是无效的,因此使案例< 2/2更有效率。


可能是,可能不是。事实上,无论如何,许多操作系统的所有内存都为零。在这样的平台上,calloc可以实现为:


void * calloc(size_t count,size_t size)

{

返回malloc(计数*大小);

}


....在这种情况下,执行速度几乎没有差别


现在假设我们要再次为int数据分配内存类型并用2初始化它,

案例A:

(int *)abcd =(int *)malloc(100);
memset(abcd,2,sizeof(int)* 100); //用户名为memset()

案例B:

(int *)abcd =(int *)calloc(1,100);
^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

上面等于
(int *)abcd =(int *)malloc(100);
memset(abcd,0,sizeof(int)* 100); //系统名为memset()
memset(abcd,2,sizeof(int)* 100); //用户名为memset()

所以我们看到案例A与CASe B相比是有效的
因此只有在案例A中调用memset()时才会调用
情况B两次调用memset()函数。 (即Usser和系统)
因此,在这种情况下,Malloc更有效率。

因此,这使得calloc的效率和malloc,

我们必须得到默认的intialiosation然后总是使用
calloc,我们必须使用一些指定的初始化然后
总是使用malloc。

请指导我因为我试图获得
calloc和malloc之间的差异..这是我猜测它可能也是一种差异。请让我知道mi是否正确或我错过了什么。
Dear All

we all are familiar with the calloc and malloc, Now we know that
calloc does the initialisation which is not achived in the malloc
while allocatings bytes for diffrent data types.

Now my concern about these two are which is more efficent to use,
Write the code that does what you want, measure it *then* make
decisions about what needs to be optimized.

To avoid any mallacisous behaviour we gernally adopt the calloc,
anyway both the calloc and malloc allocates the bytes in continious
memeory location. (As calloc also initalises)

Now I suppose the malloc is more efficent with the calloc
reason behind this is that Calloc is having the extra overhead of
initailisaig the bytes with Zero (Default), when we just simply want
to allocate the bytes (no need to initialise it)
It might be, it might not be.
(char *) abcd = (char *)calloc (2, 100);

above is same as
(char *) abcd = (char *)malloc(200);
memset(abcd, 0, 1 * 200); // system called

Now this means that calloc is not so much efficient as compared to
malloc due to over head.
But see the below case that
suppose we have to allocte the memory for the int data type
and initialise it with Zero,

Case 1:

(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The user called memset()

Case 2:
(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()
So as in case 1 we have the User called memset fuction, hence
case 1 is inefficent with respect to the case 2, Thus making the case
2 more efficient.
It might be, it might not be. In fact, many OS''s zero all memory
allocations anyway. On such a platform, calloc might be implemented as:

void *calloc(size_t count, size_t size)
{
return malloc(count * size);
}

....in which case, there''d be almost no difference in execution speed
between malloc and calloc.
Now Suppose we have the to allocate the memory for again int data
type and initialise it with 2,

Case A:

(int *) abcd = (int *)malloc(100);
memset(abcd, 2, sizeof(int) * 100); // The user called memset()

Case B:

(int *) abcd = (int *)calloc(1, 100);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

above is equivelent to
(int *) abcd = (int *)malloc(100);
memset(abcd, 0, sizeof(int) * 100); // The system called memset()

memset(abcd, 2, sizeof(int) * 100); // The usser called memset()
So we see that Case A is efficent as compared to the CASe B
Thus as In Case A only once the memset() is called, while in
case B two times memset() fucntion is called. (I.e Usser and system)
Thus Malloc is more efficent in this case.

So this make the diffrence in the terms of the Efficency of the calloc
and malloc,

where we have to get the default intialiosation then always use
calloc and where we have to use the Some specified intialisation then
always use the malloc.

Please Guide me as As I was trying to get the diffrence between the
calloc and malloc .. this is what i guessed it may be a diffrence
also. Please let me know m i correct or not or what i am missing.




如果您对代码进行分析,并发现calloc明显较慢

比malloc,然后是的,当你想要立即初始化数据时你可以改变你的代码使用malloc,并且当你想要它时你可以改变calloc

初始化为所有 - 零。


-

Clark S. Cox,III
cl ******* @ gmail.com



If you profile the code, and find that calloc is significantly slower
than malloc, then yes, you can change your code to use malloc when you
plan to immediately initialize the data, and calloc when you want it
initialized to all-zero.

--
Clark S. Cox, III
cl*******@gmail.com


这篇关于有效率(我是否正确?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆