Sign in to follow this  

Shorthand for initalising an array of structs

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

G'day For some crazy reason, c++ isn't liking the shorthand shorthand syntax for array initalisation when my array is an array of structs right. This works fine:
int myArray[4];
myArray[] = {0,1,2,3}
But this doesnt: CustomVertextFormat vertices[4];
vertices[] =
	{
		{ 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  0.0f,  }, // top left
		{ width, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  0.0f,  }, // top right
		{ 0.0f, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  1.0f,  }, // bottom left
		{ width, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  1.0f,  }, // bottom right

	}
I could go vertices[0].x = 0.0f; vertices[0].y = 0.0f ...etc but I'ld prefer to do it the quick and easy way =P I'm guessing its something stupidly simple, any ideas ?

Share this post


Link to post
Share on other sites
int myArray[] = {0,1,2,3};
works fine. The initializer list has to be added right after the declaration, unless I'm mistaken.
So far, it means that
CustomVertexFormat vertices[4] = { ... };
should work, provided that CustomVertexFormat is an aggregate type.
Regards,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's probably better to avoid the old C-style struct initialization and use a constructor instead. Try that.

Share this post


Link to post
Share on other sites
CustomVertextFormat test = { 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  0.0f };


Note that the above doesn't compile.

Your CustomVertextFormat verticies are not POD data structures.

Define a full constructor your CustomVertextFormat.

Share this post


Link to post
Share on other sites
Quote:

Note that the above doesn't compile.

Your CustomVertextFormat verticies are not POD data structures.

Define a full constructor your CustomVertextFormat.


Why arent they POD (plain old data ?) structures ? They are just structs...

Quote:

int myArray[] = {0,1,2,3};

works fine. The initializer list has to be added right after the declaration, unless I'm mistaken.
So far, it means that

CustomVertexFormat vertices[4] = { ... };

should work, provided that CustomVertexFormat is an aggregate type.
Regards,


Umm, yeah your right.

int myArray[4];
myArray[] = {0,1,2,3}

Doesnt actually work, although last night i thought it was lol. Yay for lack of sleepies :P

BTW what is an aggregate type ?





okay, thanks guys looks like im off to make a class full of public members, with no methods, you know, like a struct :P

Share this post


Link to post
Share on other sites

CustomVertextFormat vertices[4] =
{
{ 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f, 0.0f }, // top left
{ width, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f, 0.0f }, // top right
{ 0.0f, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f, 1.0f }, // bottom left
{ width, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f, 1.0f } // bottom right

};



that doesnt work?

Share this post


Link to post
Share on other sites
Quote:
Why arent they POD (plain old data ?) structures ? They are just structs...

If they contain any non-default constructors, operators, or destructors, or contain any types that are not themselves POD types, they are not POD types.

Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.

Share this post


Link to post
Share on other sites
Did you even read that thread?

[quote]Original post from asp_
Yeah Fruny's suggestion generates excellent code for me with no overhead at runtime. If you're really interested I did run some tests. The code I used is below. You can try different variations of it if you're actually interested.


#include <cstdio>

class Point3
{
public:
inline
Point3()
{
mP[0] = 0.0f;
mP[1] = 0.0f;
mP[2] = 0.0f;
};

inline
Point3(const Point3 &p)
{
mP[0] = p.mP[0];
mP[1] = p.mP[1];
mP[2] = p.mP[2];
};

inline
Point3(float x, float y, float z)
{
mP[0] = x;
mP[1] = y;
mP[2] = z;
};

inline void
operator =(const Point3 &p)
{
mP[0] = p.mP[0];
mP[1] = p.mP[1];
mP[2] = p.mP[2];
};

inline void
print()
const
{
printf("%f:%f:%f", mP[0], mP[1], mP[2]);
};

private:
float mP[3];
};

class Plane3b
{
public:
inline
Plane3b(const Point3 &p0,
const Point3 &p1,
const Point3 &p2)
{
mP[0] = p0;
mP[1] = p1;
mP[2] = p2;
};

void
print()
const
{
printf("P0:");
mP[0].print();
printf(", P1:");
mP[1].print();
printf(", P2:");
mP[2].print();
};

private:
Point3 mP[3];
};

class Plane3a
{
public:
inline
Plane3a(const Point3 &p0,
const Point3 &p1,
const Point3 &p2)
: mP0(p0), mP1(p1), mP2(p2)
{
};

inline Point3 &
operator[](size_t idx)
{
return this->*mArr[idx];
};

const Point3&
operator[](size_t idx) const
{
return this->*mArr[idx];
};

void
print()
const
{
printf("P0:");
mP0.print();
printf(", P1:");
mP1.print();
printf(", P2:");
mP2.print();
};
private:
static Point3 Plane3a::*mArr[3];
Point3 mP0;
Point3 mP1;
Point3 mP2;
};

Point3 Plane3a::*Plane3a::mArr[3] = { &Plane3a::mP0, &Plane3a::mP1, &Plane3a::mP2 };

int main()
{
Plane3a p(Point3(1.0f, 2.0f, 3.0f),
Point3(4.0f, 5.0f, 6.0f),
Point3(7.0f, 8.0f, 9.0f));

p.print();

}




And here's my summary of the code MSVC 7.1 generates in release build.

////////////////////////////////////////////////////////////////////////////

// ARRAY INITIALIZER LIST "HACK", EMPTY MEMBER DEFAULT CONSTRUCTOR (PLANE3A)

////////////////////////////////////////////////////////////////////////////

// ENTRY POINT

00401090 &gt;/$ 83EC 24 SUB ESP,24
00401093 |. 8D4C24 00 LEA ECX,DWORD PTR SS:[ESP]
00401097 |. C74424 00 0000803F MOV DWORD PTR SS:[ESP],3F800000
0040109F |. C74424 04 00000040 MOV DWORD PTR SS:[ESP+4],40000000
004010A7 |. C74424 08 00004040 MOV DWORD PTR SS:[ESP+8],40400000
004010AF |. C74424 0C 00008040 MOV DWORD PTR SS:[ESP+C],40800000
004010B7 |. C74424 10 0000A040 MOV DWORD PTR SS:[ESP+10],40A00000
004010BF |. C74424 14 0000C040 MOV DWORD PTR SS:[ESP+14],40C00000
004010C7 |. C74424 18 0000E040 MOV DWORD PTR SS:[ESP+18],40E00000
004010CF |. C74424 1C 00000041 MOV DWORD PTR SS:[ESP+1C],41000000
004010D7 |. C74424 20 00001041 MOV DWORD PTR SS:[ESP+20],41100000
// CALL PLANE3::PRINT()

004010DF |. E8 1CFFFFFF CALL fruny.00401000
004010E4 |. 33C0 XOR EAX,EAX
004010E6 |. 83C4 24 ADD ESP,24
004010E9 \. C3 RETN

////////////////////////////////////////////////////////////////////////////

// COPY OPERATOR, EMPTY MEMBER DEFAULT CONSTRUCTOR (PLANE3B)

////////////////////////////////////////////////////////////////////////////'

// ENTRY POINT

00401090 &gt;/$ 83EC 24 SUB ESP,24
00401093 |. 8D4C24 00 LEA ECX,DWORD PTR SS:[ESP]
00401097 |. C74424 00 0000803F MOV DWORD PTR SS:[ESP],3F800000
0040109F |. C74424 04 00000040 MOV DWORD PTR SS:[ESP+4],40000000
004010A7 |. C74424 08 00004040 MOV DWORD PTR SS:[ESP+8],40400000
004010AF |. C74424 0C 00008040 MOV DWORD PTR SS:[ESP+C],40800000
004010B7 |. C74424 10 0000A040 MOV DWORD PTR SS:[ESP+10],40A00000
004010BF |. C74424 14 0000C040 MOV DWORD PTR SS:[ESP+14],40C00000
004010C7 |. C74424 18 0000E040 MOV DWORD PTR SS:[ESP+18],40E00000
004010CF |. C74424 1C 00000041 MOV DWORD PTR SS:[ESP+1C],41000000
004010D7 |. C74424 20 00001041 MOV DWORD PTR SS:[ESP+20],41100000
// CALL PLANE3::PRINT()

004010DF |. E8 1CFFFFFF CALL fruny.00401000
004010E4 |. 33C0 XOR EAX,EAX
004010E6 |. 83C4 24 ADD ESP,24
004010E9 \. C3 RETN

////////////////////////////////////////////////////////////////////////////

// COPY OPERATOR, MEMBER DEFAULT CONSTRUCTOR SETS POINT TO 0.0f, 0.0f, 0.0f

// (PLANE3B)

////////////////////////////////////////////////////////////////////////////

// PLANE3::PLANE3()

00401090 /$ 8BC1 MOV EAX,ECX
00401092 |. 56 PUSH ESI
00401093 |. 8D70 08 LEA ESI,DWORD PTR DS:[EAX+8]
00401096 |. 57 PUSH EDI
00401097 |. 8BCE MOV ECX,ESI
00401099 |. BA 03000000 MOV EDX,3
0040109E |. 33FF XOR EDI,EDI
004010A0 |&gt; 8979 F8 MOV DWORD PTR DS:[ECX-8],EDI
004010A3 |. 8979 FC MOV DWORD PTR DS:[ECX-4],EDI
004010A6 |. 8939 MOV DWORD PTR DS:[ECX],EDI
004010A8 |. 83C1 0C ADD ECX,0C
004010AB |. 4A DEC EDX
004010AC |.^75 F2 JNZ SHORT fruny.004010A0
004010AE |. 8B4C24 0C MOV ECX,DWORD PTR SS:[ESP+C]
004010B2 |. 8B11 MOV EDX,DWORD PTR DS:[ECX]
004010B4 |. 8910 MOV DWORD PTR DS:[EAX],EDX
004010B6 |. 8B51 04 MOV EDX,DWORD PTR DS:[ECX+4]
004010B9 |. 8950 04 MOV DWORD PTR DS:[EAX+4],EDX
004010BC |. 8B49 08 MOV ECX,DWORD PTR DS:[ECX+8]
004010BF |. 890E MOV DWORD PTR DS:[ESI],ECX
004010C1 |. 8B4C24 10 MOV ECX,DWORD PTR SS:[ESP+10]
004010C5 |. 8B11 MOV EDX,DWORD PTR DS:[ECX]
004010C7 |. 8950 0C MOV DWORD PTR DS:[EAX+C],EDX
004010CA |. 8B51 04 MOV EDX,DWORD PTR DS:[ECX+4]
004010CD |. 8950 10 MOV DWORD PTR DS:[EAX+10],EDX
004010D0 |. 8B49 08 MOV ECX,DWORD PTR DS:[ECX+8]
004010D3 |. 8948 14 MOV DWORD PTR DS:[EAX+14],ECX
004010D6 |. 8B4C24 14 MOV ECX,DWORD PTR SS:[ESP+14]
004010DA |. 8B11 MOV EDX,DWORD PTR DS:[ECX]
004010DC |. 8950 18 MOV DWORD PTR DS:[EAX+18],EDX
004010DF |. 8B51 04 MOV EDX,DWORD PTR DS:[ECX+4]
004010E2 |. 8950 1C MOV DWORD PTR DS:[EAX+1C],EDX
004010E5 |. 8B49 08 MOV ECX,DWORD PTR DS:[ECX+8]
004010E8 |. 5F POP EDI
004010E9 |. 8948 20 MOV DWORD PTR DS:[EAX+20],ECX
004010EC |. 5E POP ESI
004010ED \. C2 0C00 RETN 0C

// ENTRY POINT

004010F0 &gt;/$ 83EC 48 SUB ESP,48
004010F3 |. 8D4424 00 LEA EAX,DWORD PTR SS:[ESP]
004010F7 |. 50 PUSH EAX
004010F8 |. 8D4C24 10 LEA ECX,DWORD PTR SS:[ESP+10]
004010FC |. 51 PUSH ECX
004010FD |. 8D5424 20 LEA EDX,DWORD PTR SS:[ESP+20]
00401101 |. 52 PUSH EDX
00401102 |. 8D4C24 30 LEA ECX,DWORD PTR SS:[ESP+30]
00401106 |. C74424 0C 0000E040 MOV DWORD PTR SS:[ESP+C],40E00000
0040110E |. C74424 10 00000041 MOV DWORD PTR SS:[ESP+10],41000000
00401116 |. C74424 14 00001041 MOV DWORD PTR SS:[ESP+14],41100000
0040111E |. C74424 18 00008040 MOV DWORD PTR SS:[ESP+18],40800000
00401126 |. C74424 1C 0000A040 MOV DWORD PTR SS:[ESP+1C],40A00000
0040112E |. C74424 20 0000C040 MOV DWORD PTR SS:[ESP+20],40C00000
00401136 |. C74424 24 0000803F MOV DWORD PTR SS:[ESP+24],3F800000
0040113E |. C74424 28 00000040 MOV DWORD PTR SS:[ESP+28],40000000
00401146 |. C74424 2C 00004040 MOV DWORD PTR SS:[ESP+2C],40400000
// PLANE3::PLANE3()

0040114E |. E8 3DFFFFFF CALL fruny.00401090
00401153 |. 8D4C24 24 LEA ECX,DWORD PTR SS:[ESP+24]
// PLANE3::PRINT()

00401157 |. E8 A4FEFFFF CALL fruny.00401000
0040115C |. 33C0 XOR EAX,EAX
0040115E |. 83C4 48 ADD ESP,48
00401161 \. C3 RETN

////////////////////////////////////////////////////////////////////////////

// ARRAY INITIALIZER LIST "HACK", MEMBER DEFAULT CONSTRUCTOR SETS POINT TO

// 0.0f, 0.0f, 0.0f (PLANE3A)

////////////////////////////////////////////////////////////////////////////

00401090 &gt;/$ 83EC 24 SUB ESP,24
00401093 |. 8D4C24 00 LEA ECX,DWORD PTR SS:[ESP]
00401097 |. C74424 00 0000803F MOV DWORD PTR SS:[ESP],3F800000
0040109F |. C74424 04 00000040 MOV DWORD PTR SS:[ESP+4],40000000
004010A7 |. C74424 08 00004040 MOV DWORD PTR SS:[ESP+8],40400000
004010AF |. C74424 0C 00008040 MOV DWORD PTR SS:[ESP+C],40800000
004010B7 |. C74424 10 0000A040 MOV DWORD PTR SS:[ESP+10],40A00000
004010BF |. C74424 14 0000C040 MOV DWORD PTR SS:[ESP+14],40C00000
004010C7 |. C74424 18 0000E040 MOV DWORD PTR SS:[ESP+18],40E00000
004010CF |. C74424 1C 00000041 MOV DWORD PTR SS:[ESP+1C],41000000
004010D7 |. C74424 20 00001041 MOV DWORD PTR SS:[ESP+20],41100000
004010DF |. E8 1CFFFFFF CALL fruny.00401000
004010E4 |. 33C0 XOR EAX,EAX
004010E6 |. 83C4 24 ADD ESP,24
004010E9 \. C3 RETN




Of course this is a pretty stupid test but it shows me all that I need to know. The fact is Fruny's suggestion gives me the ability to address the members as an array and avoid the default constructor.

[/quote]

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.


Are you absolutely sure that C++ forbids a "dangling" comma at the end of such a list? At least g++ from GCC 4.1 doesn't have a problem with it, and it is a pretty common thing in other languages.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.


No, that's not it.

The error was that he tried to initialise the array seperately from the declaration.


int[3] array = {1,2,3};//OK



int[3] array;
array = {1,2,3};//Error

Share this post


Link to post
Share on other sites
"D3DCOLOR_XRGB( 255, 255, 255)" lead me to believe that they wheren't POD structures.

On second thought, D3DCOLOR_XRGB is a macro, not a constructor for an object of type D3DCOLOR_XRGB. :)

So yes, they are POD structures. Ik. :)

Share this post


Link to post
Share on other sites

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this