Is this syntax ok?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pertheli

    Is this syntax ok?

    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(n AllocSize*sizeo f(Word));

    //Method 2 seems ok even if nAllocSize is large
    Word* pShort;
    Char* pChar = (Char*)malloc(n AllocSize*sizeo f(Char)*2);
    pShort = (Word*)pChar;
  • Rolf Magnus

    #2
    Re: Is this syntax ok?

    pertheli wrote:
    [color=blue]
    > Hello all
    >
    > What is the difference between Method 1 and Method 2 below?[/color]

    If sizeof(short) == 2 * sizeof(unsigned char), there is none.
    [color=blue]
    > Is Method 2 safe to use?[/color]

    Well, I don't know what you mean by 'large', but allocating large blocks
    of memory might fail depending on the system. It should however not
    crash in any case, but rather return a null pointer on failure.
    [color=blue]
    > typedef short Word;
    > typedef unsigned char Char;
    >
    > int nAllocSize = large number;
    >
    > //Method 1 crashes in my machine for large nAllocSize
    > Word* pShort = (Word*)malloc(n AllocSize*sizeo f(Word));
    >
    > //Method 2 seems ok even if nAllocSize is large
    > Word* pShort;
    > Char* pChar = (Char*)malloc(n AllocSize*sizeo f(Char)*2);
    > pShort = (Word*)pChar;[/color]

    Comment

    • Peter Koch Larsen

      #3
      Re: Is this syntax ok?


      "pertheli" <pertheli@hotma il.com> skrev i en meddelelse
      news:48957908.0 311200227.631cd cbc@posting.goo gle.com...[color=blue]
      > Hello all
      >
      > What is the difference between Method 1 and Method 2 below? Is Method 2[/color]
      safe to use?[color=blue]
      >
      >
      > typedef short Word;
      > typedef unsigned char Char;
      >
      > int nAllocSize = large number;
      >
      > //Method 1 crashes in my machine for large nAllocSize
      > Word* pShort = (Word*)malloc(n AllocSize*sizeo f(Word));
      >
      > //Method 2 seems ok even if nAllocSize is large
      > Word* pShort;
      > Char* pChar = (Char*)malloc(n AllocSize*sizeo f(Char)*2);
      > pShort = (Word*)pChar;[/color]

      Do not use malloc. Use new instead:
      Word* pShort = new Word[nAllocSize];
      // pShort will point to the allocated area or an exception will be
      thrown
      If you dont want exceptions here, use:

      Word* pShort = new(std::nothro w) Word[nAllocSize];
      // If allocation fails, pShort will be 0

      Without knowing your program i still believe that using the standard library
      would be even better:

      std::vector<Wor d> word_vector(nAl locSize);

      You will have to live with the possible exception here, but all dynamic
      memory management has been taken care of by the system.

      Apart from this, it seems that you try to make an abstraction of "short" by
      introducing your typedef. Why then do you name the variable pShort? If
      anything it should be pWord, although i detest variable names that aim at
      describing the underlying type. After all, C++ is a strongly typed language.

      Kind regards
      Peter


      Comment

      Working...