Trying to initialize an array created with 'new'

Started by
16 comments, last by DividedByZero 8 years, 5 months ago
Hi Guys,

I am trying to initialize an array that has had its memory allocated with 'new'.

I am having trouble working out why it wont compile.



Float3* vertex_data = new Float3[3];
 
vertex_data[] =
{
     -0.5f, 2.0f, 0.0f,
     0.5f, 2.0f, 0.0f,
     0.0f, 0.0f, 0.0f,
};
 
delete[] vertex_data;

Compilation fails at the vertex_data[] initialization stage.


Syntax error: "]"



'Float3' is just a struct with three floats in it.

Any advice would be awesome.

Thanks in advance smile.png
Advertisement

You can only initialize an object at the moment it's new'ed.

This should work, but be less efficient tongue.png


Float3* vertex_data = new Float3[3];
Float3 temp_data[] =
{
    { -0.5f, 2.0f, 0.0f },
    { 0.5f, 2.0f, 0.0f },
    { 0.0f, 0.0f, 0.0f },
};
std::copy(temp_data, temp_data+3, vertex_data);
Thanks for the reply smile.png

How do you mean "initialize an object at the moment it's new'ed."?

Something like this? (the following doesn't work though)


Float3* vertex_data = new Float3[{-0.5f, 2.0f, 0.0f},{ 0.5f, 2.0f, 0.0f },{ 0.0f, 0.0f, 0.0f }];
There's a difference between initialization and assignment. Initialization is done once, at the moment that an object comes into existance. Assignment can happen many times.
int a = 1;//initialization
a = 2;//assignment
a = 3;//assignment
int b = a;//initialization
b = a;//assignment
When you call new, you're creating and initializing your array. Then later on you're trying to reinitialize it, which isn't possible. In the example I gave you, I initialize a temporary array, and then assign those temp values into your original array.

If your compiler supports C++11, you might also be able to use the new initializer list syntax:
Float3* vertex_data = new Float3[3] {
    { -0.5f, 2.0f, 0.0f },
    { 0.5f, 2.0f, 0.0f },
    { 0.0f, 0.0f, 0.0f },
};
Thanks for your advice, once again smile.png


Float3* vertex_data = new Float3[3]
{
	{ -0.5f, 2.0f, 0.0f },
	{ 0.5f, 2.0f, 0.0f },
	{ 0.0f, 0.0f, 0.0f },
};
This seems to compile perfectly smile.png
LOL - seems to compile ok. But the data is filled with garbage once I go back and access it.
Can you post the code for your Float3 class?
This is the data structure


struct Float3
{
	float x, y, z;
};

This is the main code


		/*Float3* vertex_data = new Float3[3]
		{
			{ -0.5f, 2.0f, 0.0f },
			{ 0.5f, 2.0f, 0.0f },
			{ 0.0f, 0.0f, 0.0f },
		};*/

		Float3* vertex_data = new Float3[3];

		vertex_data[0] = { -0.5f, 2.0f, 0.0f };
		vertex_data[1] = { 0.5f, 2.0f, 0.0f };
		vertex_data[2] = { 0.0f, 0.0f, 0.0f };
The commented out code results in garbage data, but the latter is ok.

You can't initialize dynamic arrays that way, apparently (in MSVC 2013 anyway, it appears to work for PODs, but not structs - I'm not sure if that's just undefined behavior, or expected).

Is there a reason you can't just use a static array? e.g.:

Float3 vertex_data[3]
{
{ -0.5f, 2.0f, 0.0f },
{ -0.5f, 2.0f, 0.0f },
{ -0.5f, 2.0f, 0.0f },
};
That will work as you expect.

Hi guys,

I am new to the forum. I joined long ago though I have never been active.

I have a question though. Why are you trying to initialize the struct with new Float3[3]{...} ? , why not new Float3[]{...} , cause it will deduce the type anyway.

This topic is closed to new replies.

Advertisement