Jump to content
  • Advertisement
Sign in to follow this  
DarkRonin

Trying to initialize an array created with 'new'

This topic is 1054 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

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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 }];
Edited by DarkRonin

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.
Edited by phil_t

Share this post


Link to post
Share on other sites

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.

Edited by randomchews

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!