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;

Re: Is this syntax ok? by Swin

Swin
Thu Nov 20 05:41:26 CST 2003

From pShort variable point of view and being carefull with pChar variable in
Method 2, I think both methods are ok, but first one is more intuitive
¿isn't it?

Swin


"pertheli" <pertheli@hotmail.com> escribió en el mensaje
news:48957908.0311200229.188de20f@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));
>
> //Method 2 seems ok even if nAllocSize is large
> Word* pShort;
> Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
> pShort = (Word*)pChar;



Re: Is this syntax ok? by Igor

Igor
Thu Nov 20 08:55:19 CST 2003

"pertheli" <pertheli@hotmail.com> wrote in message
news:48957908.0311200229.188de20f@posting.google.com...
> 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;

There should be absolutely no difference. The problem must be in the
code you don't show.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Re: Is this syntax ok? by Ron

Ron
Thu Nov 20 11:00:46 CST 2003


"pertheli" <pertheli@hotmail.com> wrote in message news:48957908.0311200229.188de20f@posting.google.com...

>
> //Method 1 crashes in my machine for large nAllocSize
> Word* pShort = (Word*)malloc(nAllocSize*sizeof(Word));

In C you should avoid gratutiously casting malloc (with a proper declaration, this
is not necessary). In C++, the cast is necessary, but one should question why
you are using malloc.

> //Method 2 seems ok even if nAllocSize is large
> Word* pShort;
> Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char)*2);
> pShort = (Word*)pChar;

This should result in the same value as method 1. The call to malloc
gets the same value and presumably the crash occurs before the return
value is used for anything.

What is nAllocSize and what kind of crash are you getting?



Re: Is this syntax ok? by James

James
Thu Nov 20 12:43:37 CST 2003

"pertheli" <pertheli@hotmail.com> wrote in message
news:48957908.0311200229.188de20f@posting.google.com...
> typedef unsigned char Char;
> Char* pChar = (Char*)malloc(nAllocSize*sizeof(Char) *2);
> pShort = (Word*)pChar;

We should also note that the C++ Standard requires that sizeof(char) == 1,
so adding it to that statement is pointless.