malloc语法 [英] malloc syntax
问题描述
Hello all
下面的方法1和方法2有什么区别?方法2是否可以安全使用?
typedef短字;
typedef unsigned char Char;
int nAllocSize = large number;
//方法1在我的机器中崩溃以获得大nAllocSize
Word * pShort =(Word *)malloc(nAllocSize * sizeof(Word));
//方法2似乎没问题,即使nAllocSize很大
Word * pShort;
Char * pChar =(Char *) malloc(nAllocSize * sizeof(Char)* 2);
pShort =(Word *)pChar;
In < 48 ************************** @ posting.google.com> pe******@hotmail.com (pertheli)写道:
下面的方法1和方法2有什么区别?方法2是否可以安全使用?
typedef短字;
typedef unsigned char Char;
int nAllocSize =大数;
//方法1在我的机器中崩溃以获得大的nAllocSize
Word * pShort =(Word *)malloc(nAllocSize * sizeof(Word));
//即使nAllocSize,方法2似乎也没问题很大
Word * pShort;
Char * pChar =(Char *)malloc(nAllocSize * sizeof(Char)* 2);
pShort =(Word *)pChar;
如果sizeof(Word)== 2,这两种方法完全相同。
然而,正确的做法是:
size_t size = nAllocSize * sizeof * pShort;
if(size< nAllocSize || size< sizeof * pShort){
/ *我们想要分配比size_t更多的字节可以代表* /
}
pShort = malloc(size);
Dan
-
Dan Pop
DESY Zeuthen,RZ集团
电子邮件: Da ***** @ ifh.de
" pertheli" < PE ****** @ hotmail.com> schrieb im Newsbeitrag
news:48 ************************** @ posting.google.c om ...Hello all
下面的方法1和方法2有什么区别?方法2
是否可以安全使用?
typedef短字;
typedef unsigned char Char;
为什么?
short和char有什么问题?
每个C程序员都知道这些类型,所以typedef只是在代码中添加噪声,并迫使维护者查看typedef ...
int nAllocSize =大数;
/ /方法1在我的机器中崩溃以获得大的nAllocSize
Word * pShort =(Word *)malloc(nAllocSize * sizeof(Word));
#include< stdlib.h>
并将其设为
short * short_array = malloc(< large number> * sizeof * short_array);
你是_sure_你的实现中的sizeof(短)== 2 ??
方法1崩溃和方法2成功建议否则。
//即使nAllocSize很大,方法2似乎也没问题
Word * pShort;
Char * pChar =(Char *)malloc(nAllocSize * sizeof(Char)* 2);
char * other_array = malloc((< large number> * sizeof * other_array * 2);
如果sizeof(短)> ; 2你使用malloc()ed空间你可能会
触发WW3
这两种方法是_not_ repeat _not_等效。
Robert
pe ****** @ hotmail.com (pertheli)在留言新闻中写道:< 48 ************************** @ posting.google。 com> ...大家好。
下面的方法1和方法2有什么区别?方法2是否可以安全使用?
typedef短字;
typedef unsigned char Char;
int nAllocSize =大数字;
//方法1在我的机器中崩溃以获得大的nAllocSize
Word * pShort =(Word *)malloc(nAllocSize * sizeof(Word));
失去演职员表。你不需要它(在C中,无论如何),如果你没有malloc的原型()
可以抑制诊断b范围内(即你忘了#include stdlib.h - 你*记得
到#include stdlib.h,对吧?)。
Word * pShort = malloc(nAllocSize * sizeof * pShort);
同样适用,看起来更清洁。
至于为什么一个人在另一个人没有崩溃时崩溃,我不知道。提供
sizeof(短)== 2 * sizeof(char),他们应该分配相同的
空间量。
//方法即使nAllocSize很大,2似乎也没问题
Word * pShort;
Char * pChar =(Char *)malloc(nAllocSize * sizeof(Char)* 2);
pShort =(Word *) pChar;
Hello all
What is the difference between Method 1 and Method 2 below? Is Method 2 safe to use?
typedef short Word;
typedef unsigned char Char;
int nAllocSize = large number;
//Method 1 crashes in my machine for large nAllocSize
Word* pShort = (Word*)malloc(nAllocSize*sizeof(Word));
//Method 2 seems ok even if nAllocSize is large
Word* pShort;
Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
pShort = (Word*)pChar;解决方案In <48**************************@posting.google.com > pe******@hotmail.com (pertheli) writes:
What is the difference between Method 1 and Method 2 below? Is Method 2 safe to use?
typedef short Word;
typedef unsigned char Char;
int nAllocSize = large number;
//Method 1 crashes in my machine for large nAllocSize
Word* pShort = (Word*)malloc(nAllocSize*sizeof(Word));
//Method 2 seems ok even if nAllocSize is large
Word* pShort;
Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
pShort = (Word*)pChar;
If sizeof(Word) == 2, the two methods are perfectly equivalent.
The right way of doing it, however, is:
size_t size = nAllocSize * sizeof *pShort;
if (size < nAllocSize || size < sizeof *pShort) {
/* we want to allocate more bytes than size_t can represent */
}
pShort = malloc(size);
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
"pertheli" <pe******@hotmail.com> schrieb im Newsbeitrag
news:48**************************@posting.google.c om...Hello all
What is the difference between Method 1 and Method 2 below? Is Method 2 safe to use?
typedef short Word;
typedef unsigned char Char;
Why??
What''s wrong with short and char?
Every C programmer knows these types and so the typedefs just add noise to
the code and force a maintainer to look the typedefs up...
int nAllocSize = large number;
//Method 1 crashes in my machine for large nAllocSize
Word* pShort = (Word*)malloc(nAllocSize*sizeof(Word));
#include <stdlib.h>
and make that
short *short_array = malloc(<large number> * sizeof *short_array);
Are you _sure_ that sizeof(short) == 2 in your implementation??
Method 1 crashing and Method 2 succeeding suggests otherwise.
//Method 2 seems ok even if nAllocSize is large
Word* pShort;
Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
char *other_array = malloc((<large number> * sizeof *other_array * 2);
And if sizeof(short) > 2 and you use the malloc()ed space you will probably
trigger WW3
The two methods are _not_ repeat _not_ equivalent.
Robert
pe******@hotmail.com (pertheli) wrote in message news:<48**************************@posting.google. com>...Hello all
What is the difference between Method 1 and Method 2 below? Is Method 2 safe to use?
typedef short Word;
typedef unsigned char Char;
int nAllocSize = large number;
//Method 1 crashes in my machine for large nAllocSize
Word* pShort = (Word*)malloc(nAllocSize*sizeof(Word));
Lose the cast. You don''t need it (in C, anyway), and having it there
could supress a diagnostic if you don''t have a prototype for malloc()
in scope (i.e., you forgot to #include stdlib.h -- you *did* remember
to #include stdlib.h, right?).
Word *pShort = malloc (nAllocSize * sizeof *pShort);
works just as well, and is a little cleaner to look at.
As to why one crashes when the other doesn''t, I don''t know. Provided
sizeof (short) == 2 * sizeof (char), they should allocate the same
amount of space.
//Method 2 seems ok even if nAllocSize is large
Word* pShort;
Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
pShort = (Word*)pChar;
这篇关于malloc语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!