Defining a Static Array

B

better_cs_now

Hello All,

I'd like to do this:

class Foo
{
...
static unsigned char s_data[sizeof(Foo)];
...
};

unsigned char Foo::s_data[sizeof(Foo)];

However, I cannot because at the time the first sizeof (the one in the
declaration) is encountered, Foo is an incomplete type.

I did this instead:

class Foo
{
...
static unsigned char s_data[];
...
};

unsigned char Foo::s_data[sizeof(Foo)];

The compiler accepts this. However, is it standard and does it
accomplish my intended purpose of putting enough memory for a Foo in
static storage?

Thanks,
Dave
 
T

Triple-DES

Hello All,

I'd like to do this:

class Foo
{
   ...
   static unsigned char s_data[sizeof(Foo)];
   ...

};

unsigned char Foo::s_data[sizeof(Foo)];

However, I cannot because at the time the first sizeof (the one in the
declaration) is encountered, Foo is an incomplete type.

I did this instead:

class Foo
{
   ...
   static unsigned char s_data[];
   ...

};

unsigned char Foo::s_data[sizeof(Foo)];

The compiler accepts this. However, is it standard and does it
accomplish my intended purpose of putting enough memory for a Foo in
static storage?

Yes and yes, but if you are thinking of constructing a Foo inside the
array, keep in mind that it may not be suitably aligned to hold an
object of type Foo.
 
B

better_cs_now

Hello All,
I'd like to do this:
class Foo
{
   ...
   staticunsigned char s_data[sizeof(Foo)];
   ...

unsigned char Foo::s_data[sizeof(Foo)];
However, I cannot because at the time the first sizeof (the one in the
declaration) is encountered, Foo is an incomplete type.
I did this instead:
class Foo
{
   ...
   staticunsigned char s_data[];
   ...

unsigned char Foo::s_data[sizeof(Foo)];
The compiler accepts this. However, is it standard and does it
accomplish my intended purpose of putting enough memory for a Foo in
staticstorage?

Yes and yes, but if you are thinking of constructing a Foo inside thearray, keep in mind that it may not be suitably aligned to hold an
object of type Foo.- Hide quoted text -

- Show quoted text -

I'm glad to hear that the code shown does what I want (at lesat in
part). But for my edification, I have a question...

Given the declaration:
static unsigned char s_data[];

Why does the compilre *not* interpret this as just an unsigned char *
(which is four bytes on the platforms I deal with), thereby creating a
conflict with the definition, which is for a different numbre of
bytes?

Also, regarding the alignment problem...

I do indeed want to construct a Foo in that space, so the alignment
issue you raised is important to me.

Is this alignment problem due simply to an absence in the standard of
a requirement requiring that static allocations be aligned properly?
Or is it due to some other omission, or prehaps even an explicit
statement of some sort?

More importantly, is there anything I can due to ensure proper
alignment (keeping in mind that, for reasons that are too much to go
into here, I cannot construct the object on the heap)?

Thanks!
 
J

James Kanze

On Feb 5, 12:06 am, Triple-DES <[email protected]> wrote:

[...]
Given the declaration:
static unsigned char s_data[];
Why does the compilre *not* interpret this as just an unsigned
char *

For the same reasons the compiler doesn't interpret "float x" as
a float*. It's not. Why on earth should the compiler interpret
it as an unsigned char*.

[...]
I do indeed want to construct a Foo in that space, so the
alignment issue you raised is important to me.
Is this alignment problem due simply to an absence in the
standard of a requirement requiring that static allocations be
aligned properly? Or is it due to some other omission, or
prehaps even an explicit statement of some sort?

It has nothing to do with static. Different types have
different alignment requirements, and the compiler will only
ensure adequate alignment for the type it sees.
More importantly, is there anything I can due to ensure proper
alignment (keeping in mind that, for reasons that are too much
to go into here, I cannot construct the object on the heap)?

The usual solution is to use some sort of union:

union {
TypeWithMaxAlignemnt dummyForAlignment ;
unsigned char data[ sizeof( T ) ] ;
} ;

What the TypeWithMaxAlignemnt is will depend on the
implementation, but a union with long double, double, long long
and possibly pointers to data and pointers to functions has been
sufficient for all implementations I've encountered.

Note that this can increase the reserved size for very small T.
If that's a problem there are fairly simple template tricks to
generate the union in such a way that it ignores all types which
are larger than T.
 

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
474,164
Messages
2,570,897
Members
47,439
Latest member
shasuze

Latest Threads

Top