static_cast in C++

Discussion in 'Programmer's Corner' started by chesart1, Apr 5, 2008.

  1. chesart1

    Thread Starter Senior Member

    Jan 23, 2006
    269
    1
    Hi,

    I'm trying to use static_cast for dynamic allocation of a structure.

    Here is my code

    struct storage
    {
    char mychar;
    int myint;
    storage* next;
    storage* prev;
    } store;

    int main(void)
    {
    // newstore points to the new structure storage
    storage* newstore = static_cast<storage*>new(sizeof(storage));
    return 0;
    }

    The compiler returns the following error:
    "parse error before new"

    In (Microsoft ) C++, how do I use static_cast in this application?

    Thanks in advance,
    John
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    John,
    I think your problem is with new, not with static_cast. You are calling new with the size of the block of memory rather than a type, which it's expecting. The following code comes from the MSDN ref page for the new operator, at http://msdn2.microsoft.com/en-us/library/kewsb8ba(VS.80).aspx.
    Code ( (Unknown Language)):
    1.  
    2. int main() {
    3.    // Allocate memory for the array
    4.    char* pCharArray = new char[CName::sizeOfBuffer];
    5.    strcpy_s(pCharArray, CName::sizeOfBuffer, "Array of characters");
    6.  
    7.    // Deallocate memory for the array
    8.    delete [] pCharArray;          
    9.    pCharArray = NULL;
    10.  
    11.    // Allocate memory for the object
    12.    CName* pName = new CName;
    13.    pName->SetName("Firstname", "Lastname");
    14.  
    15.    // Deallocate memory for the object
    16.    delete pName;
    17.    pName = NULL;
    18. }
    19.  
    In the preceding example, the first call to new allocates space on the heap for an array of char. The second call to new allocates space on the heap for a CName class instance (I have omitted the definition of this struct, but it's in the example if you follow the link I provided).

    There's another form of the new operator--placement new--that allows you to specify where the object will be allocated, but I think it also requires a type.

    Hope this is helpful.
    Mark
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    John,
    After thinking about it awhile, I don't know that you even need to use a cast. Won't this work?
    Code ( (Unknown Language)):
    1.  
    2.  storage * newstore = new storage;
    3.  
    This should allocate storage for a struct of this type and return a pointer to it, with the type of the pointer being storage*.
     
  4. chesart1

    Thread Starter Senior Member

    Jan 23, 2006
    269
    1
    I tried it and you are correct. It compiles without errors. The books, I have, use cast and sizeof without indicating why. Maybe the reason is, as you indicated in a previous message, that storage is not a single type of date.

    Thanks,
    John
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Did you mean to write "data?"
    When you use new, as opposed to the standard runtime library's malloc and related functions, get a pointer to the type specified in the call. No need to cast the pointer. malloc and its kin, on the other hand, just give you a void pointer that you need to cast to the appropriate type.
     
  6. chesart1

    Thread Starter Senior Member

    Jan 23, 2006
    269
    1
    I chose the confusing name storage for the structure. I was referring to the structure, not the writing of data.

    I was making the new instruction more complicated than need be. Thanks for explaining the difference between malloc and new. I appreciate it.
     
Loading...