static_cast in C++

Thread Starter

chesart1

Joined Jan 23, 2006
269
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
 

Mark44

Joined Nov 26, 2007
628
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.
Rich (BB code):
int main() {
   // Allocate memory for the array
   char* pCharArray = new char[CName::sizeOfBuffer];
   strcpy_s(pCharArray, CName::sizeOfBuffer, "Array of characters");

   // Deallocate memory for the array
   delete [] pCharArray;           
   pCharArray = NULL;

   // Allocate memory for the object
   CName* pName = new CName;
   pName->SetName("Firstname", "Lastname");

   // Deallocate memory for the object
   delete pName;
   pName = NULL;
}
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
 

Mark44

Joined Nov 26, 2007
628
John,
After thinking about it awhile, I don't know that you even need to use a cast. Won't this work?
Rich (BB code):
 storage * newstore = new storage;
This should allocate storage for a struct of this type and return a pointer to it, with the type of the pointer being storage*.
 

Thread Starter

chesart1

Joined Jan 23, 2006
269
John,
After thinking about it awhile, I don't know that you even need to use a cast. Won't this work?
Rich (BB code):
 storage * newstore = new storage;
This should allocate storage for a struct of this type and return a pointer to it, with the type of the pointer being storage*.
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
 

Mark44

Joined Nov 26, 2007
628
Maybe the reason is, as you indicated in a previous message, that storage is not a single type of date.
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.
 

Thread Starter

chesart1

Joined Jan 23, 2006
269
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.
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.
 
Top