union elements

J

JasBascom

union Allrecords* rec;

how would i get the size of Allrecords;
and how would I dynamically create memory for rec? I tried using the operator
new
with no success.
 
D

David Harmon

union Allrecords* rec;

how would i get the size of Allrecords;

With the sizeof operator, I suppose. Why would you want to know it?
It seems fairly unlikely that you should care about that. What are you
trying to accomplish?

Probably best to avoid pointers and new for most purposes.
I tried using the operator new with no success.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work correctly?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
J

Jonathan Turkanis

JasBascom said:
union Allrecords* rec;

how would i get the size of Allrecords;

If you want to know the size of a single Allrecords object, use
sizeof(Allrecords). If you have an object of type Allrecords*, and you
want to know whether it points to an array, and if so, the size of the
array, you're out of luck. You have to keep track of this info
yourself.

What do unions have to do with anything here?
and how would I dynamically create memory for rec? I tried using the operator
new
with no success.

Operator new is the way to go for dynamic allocation. What happened
when you tried?

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top