malloc问题 [英] malloc questions

查看:67
本文介绍了malloc问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:


char * p;

p =(char *)malloc(10 * sizeof(char)); // LINE1


在LINE1之后,p指向的10个字符长度内存为空或

一些随机字符?

谢谢。

The code is below:

char *p;
p = (char*)malloc(10 * sizeof(char)); //LINE1

After LINE1, the 10 char-length memory that p points to is empty or
some random characters?

Thanks.

推荐答案

杰克说:
Jack said:
代码是下面:

char * p;
p =(char *)malloc(10 * sizeof(char)); // LINE1


更好:p = malloc(10 * sizeof * p);

在LINE1之后,p指向的10个字符长度的内存是空的或
一些随机字符?
The code is below:

char *p;
p = (char*)malloc(10 * sizeof(char)); //LINE1
Better: p = malloc(10 * sizeof *p);
After LINE1, the 10 char-length memory that p points to is empty or
some random characters?




从p指向的字节开始的十个字节的值是

不确定。这意味着你不能依赖他们有意义的价值观。


我不愿意同意使用随机这个词,以防它给出

你认为你可以从这些字节中获得熵。你当然不能这样做可靠。


安全,便携的假设是你不能/读/从那些

bytes,除非你有第一个/写入/值。


-

Richard Heathfield

" ; Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



The values of the ten bytes, starting with the byte pointed to by p, are
indeterminate. That means you can''t rely on their having meaningful values.

I am reluctant to accede to the use of the word "random", in case it gives
you the idea that you can get entropy from those bytes. You certainly can''t
do that reliably.

The safe, portable assumption to make is that you must not /read/ from those
bytes unless you have first /written/ values to them.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


杰克发布:
Jack posted:
代码如下:

char * p;
p =(char *)malloc(10 * sizeof(char)); // LINE1

在LINE1之后,p指向的10个字符长度的内存是空的还是一些随机字符?
The code is below:

char *p;
p = (char*)malloc(10 * sizeof(char)); //LINE1

After LINE1, the 10 char-length memory that p points to is empty or
some random characters?



随机字符 - - 并且你无法保证终止

null字符,所以不要做以下事情:


char * const p = malloc(10);


printf("%s",p);

" calloc"另一方面将所有位设置为零。


如果你想动态分配一个双精度数组,或者一个

指针数组,你''我必须手动将每个元素设置为零:


#include< ...


int main(void)

{

enum {len = 45};


double * p = malloc(sizeof(double [len]));

const double * const p_over = p + len;


do

{

* p ++ = 0;

} while(p!= p_over);


}

我想你可以创建一个函数来创建一个具有

" default"的对象数组值:

typedef char T;


T * const allocdef(unsigned const amount_elem)

{

T * const p = malloc(sizeof(T [amount_elem]));


const T * const p_over = p + amount_elem;


do

{

* p ++ = 0;

} while(p!= p_over);

}

-Tomás


Random characters -- and you''ve no guarantee that there''s a terminating
null character, so don''t do the following:

char * const p = malloc(10);

printf("%s", p);
"calloc" on the other hand will set all bits to zero.

If you want to dynamically allocate an array of doubles, or perhaps an
array of pointers, you''ll have to manually set each element to zero:

#include <...

int main(void)
{
enum { len = 45 };

double *p = malloc( sizeof( double[len] ) );
const double * const p_over = p + len;

do
{
*p++ = 0;
} while ( p != p_over );

}
I suppose you could make a function for creating an array of objects with
"default" values:
typedef char T;

T * const allocdef( unsigned const amount_elem )
{
T * const p = malloc( sizeof( T[amount_elem] ) );

const T * const p_over = p + amount_elem;

do
{
*p++ = 0;
} while ( p != p_over );
}
-Tomás


> char * p;
>char *p;
p =(char *)malloc(10 *的sizeof(char)的); // LINE1

在LINE1之后,p指向的10个字符长度的内存是空的还是一些随机字符?
p = (char*)malloc(10 * sizeof(char)); //LINE1

After LINE1, the 10 char-length memory that p points to is empty or
some random characters?




内存永远不会空。


无法保证内存中你的malloc()和你的内容

不应该读它直到你把东西放进去。肯定

不能保证包含加密安全的随机数,

也不能保证初始化为所有位0xdeadbeef。

"垃圾"可能是一个更好的描述。如果你想要它初始化

到all-bits-zero,请使用calloc()。


Gordon L. Burditt



memory is never "empty".

There is no guarantee what is in the memory you malloc() and you
shouldn''t read it until you''ve put something in it. It is certainly
not guaranteed to contain cryptographically-secure random numbers,
nor is it guaranteed to be initialized to all-bits 0xdeadbeef.
"garbage" is probably a better description. If you want it initialized
to all-bits-zero, use calloc().

Gordon L. Burditt


这篇关于malloc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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