Populate an array with Vector2

Hello everyone. I have a problem, but it seems to me that it is just a question of semantic. I missed something in my expression. I am trying to populate directly an array with some Vector2. In the code below, I use a bad method so as to get all my Vector2 in a single array. It works as expected, but I guess that there is a better way. Thank you for your help ++

PS : I am developing a program using the OLC PixelGameEngine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// here there are my Vector2 as float
olc::vf2d vS1 = { 100.0f, 100.0f };
olc::vf2d vS2 = { 200.0f, 150.0f };
olc::vf2d vS3 = { 100.0f, 200.0f }; 

std::unique_ptr<olc::vf2d[]> allObj = nullptr;
(...)
OnUserCreate()
{
    allObj = std::make_unique<olc::vf2d[]>(3); // three entries

    allObj[0] = vS1; // populate the array using index :/
    allObj[1] = vS2;
    allObj[2] = vS3;
}
Last edited on
1
2
3
std::unique_ptr<olc::vf2d[]> allObj { new olc::vf2d[3]{vS1, vS2, vS3} };
                                                    ^
                             Leaving out the size here is only permitted since C++20.
Last edited on
Hello Peter87. I forgot new in my previous attempts. Thank you for your help ++
Well, normally you don't need to use new when working with unique_ptr but I don't think this kind of direct list initialization is possible with make_unique, that's why I had to use new. Normally I would prefer make_unique if possible.
Last edited on
Well. It works as expected, but I don't understand something here :

1
2
std::unique_ptr<olc::vf2d[]> allObj{ new olc::vf2d[]{{ 200, 200 }, { 200, 100 }, { 100, 100 }} };
DrawString(10, 10, std::to_string(sizeof(allObj)), olc::WHITE, 1);


For an unknown reason, the output give me 8, but I put only three entries. So all other entries have for values 0,0. What it means ?
Last edited on
sizeof() gives the number of bytes used by the type. This is not the number of bytes used to store data when dynamic memory is used as here.

AFAIK, there's no standard way to obtain the number of bytes allocated with new (or similar).
sizeof(allObj) gives you the size of the unique_ptr object. Since it probably just contains a pointer inside it's essentially giving you the size of a pointer. This is not very useful.

There is no way to get the size of an array if all you've got is a pointer to the first element. I wasn't thinking about that when I wrote my previous post. I guess that makes it a bad idea to leave out the size.

Something like the following would be less error prone:
1
2
3
const int N = 3;
std::unique_ptr<olc::vf2d[]> allObj{ new olc::vf2d[N]{{ 200, 200 }, { 200, 100 }, { 100, 100 }} };
DrawString(10, 10, std::to_string(N), olc::WHITE, 1);

Unfortunately this means you need to keep N and the number of elements that you initialize the array with in sync but at least it will give you a compilation error if N is less than the number of elements that you use to initialize the array. If N is larger it gives you additional value-initialized elements which is probably not what you want but quite harmless.

I usually don't see unique_ptr used for arrays that often. It's usually easier to simply use std::vector. Is that something you've considered?
1
2
std::vector<olc::vf2d> allObj{ { 200, 200 }, { 200, 100 }, { 100, 100 } };
DrawString(10, 10, std::to_string(allObj.size()), olc::WHITE, 1);

Last edited on
Ok. I understand now. Thank you for your explanations. I though that sizeof(anArray) was llike anArray.Length in C#. I would like to use it so as to manage all Vector2 in a single loop. I wrote a AABB collison system using sizeof() as a way to get all elements in my array. Now I understand why it doesn't work - and why I have as output 8 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// simple checker AABB for sprites
bool AABB(gck::vi2d a)
{
    for (int o = 0; o < sizeof(allObj); o++)
    {	// a tip which I used to avoid "bad" entries
        if (allObj[o].x == 0 && allObj[o].y == 0)
	    break;

    float resultAA = a.x - allObj[o].x;
    float resultBB = a.y - allObj[o].y;

    if (fabs(resultAA) < size * ratio && fabs(resultBB) < size * ratio)
        return false;
    }
    return true;
}


Changed for std::vector<olc::vf2d> allObj{ ... };
Thank you for your help.
Last edited on
Topic archived. No new replies allowed.