0

How to create a dynamic bitmap array?

I tried it like this:

Bitmap* bit = new int(5);

But it didn't work out.

After, I tried like this:

Bitmap* bit = (Bitmap*)malloc(6);
bit[5] = new Bitmap(sFileName);

But it didn't work, too.

EDIT

In .h, I have:

std::shared_ptr<CachedBitmap> *CachedBitmap;

In .cpp (WM_PAINT):

Bitmap* bit = new Bitmap(L"file");
Graphics graphics(hdc);
(CachedBitmap[0]) = PNGLoader::createCachedBitmap(bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);

std::shared_ptr<CachedBitmap> PNGLoader::createCachedBitmap(Bitmap* originalBitmap, HDC hdc)
{

    Graphics graphics(hdc);
    return std::make_shared<CachedBitmap>(originalBitmap, &graphics);
}

When I run the program, I have an exception error because CachedBitmap[0] is nullptr.

I tried:

(**CachedBitmap) = std::make_shared<::CachedBitmap>((new ::CachedBitmap(bit, &graphics))[5]);
(*CachedBitmap[0]) = PNGLoader::createCachedBitmap(bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0]->get(), position.x, position.y);

But I have error:

Gdiplus::CachedBitmap::CachedBitmap: unable to access private member declared in "Gdiplus" class::CachedBitmap"

1 Answer 1

1
Bitmap* bit = new int(5);

This doesn't work because you are dynamically allocating a single int whose value is 5, and then trying to assign the address of that int to a Bitmap* pointer. Obviously, an int is not a Bitmap, so the compiler does not allow this assignment.

You would need this instead:

Bitmap* bit = new Bitmap[5];

However, that doesn't work either, because GDI+'s Bitmap class does not have a default constructor. So, you need an array of Bitmap* pointers, not an array of Bitmap objects. Which leads us to the next example...

Bitmap* bit = (Bitmap*)malloc(6);
bit[5] = new Bitmap(sFileName);

This doesn't work because you are allocating only 6 bytes total, but you would need to allocate enough bytes for 6 Bitmap* pointers in order for bit[5] to hold a valid Bitmap* pointer, eg:

Bitmap** bit = (Bitmap**) malloc(sizeof(Bitmap*) * 6);
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;

But, don't use malloc() in C++, use new[] instead:

Bitmap** bit = new Bitmap*[6];
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;

And don't forget to delete whatever you new when you are done using it:

delete bit[0];
delete bit[1];
...
delete bit[5];
delete[] bit;

But, do you really need the array itself to be allocated dynamically?

Bitmap* bit[6];
...
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;
...
delete bit[0];
delete bit[1];
...
delete bit[5];

However, in modern C++ coding, avoid new/delete manually, use standard containers and smart pointers to handle all of the memory management for you, eg:

std::vector<std::unique_ptr<Bitmap>> bit;
...
bit.resize(5);
bit[0] = std::make_unique<Bitmap>(sFileName);
bit[1] = ...;
...

Or:

std::unique_ptr<Bitmap> bit[5];
...
bit[0] = std::make_unique<Bitmap>(sFileName);
bit[1] = ...;
...

UPDATE:

std::shared_ptr<CachedBitmap> *CachedBitmap;

There is no good reason to have a pointer to a smart pointer. Drop the * from the declaration:

std::shared_ptr<CachedBitmap> CachedBitmap;
...
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap.get(), position.x, position.y);

Or, if you really need an array of smart pointers:

std::vector<std::shared_ptr<CachedBitmap>> CachedBitmap;
...
CachedBitmap.resize(5);
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap[0] = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);

Or:

std::shared_ptr<CachedBitmap> CachedBitmap[5];
...
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap[0] = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);
Sign up to request clarification or add additional context in comments.

6 Comments

thx, but for feature in .h i have: Bitmap* gifBitmap; how init. in .cpp? *gifBitmap = new Bitmap [6]; is not working
i edit my ques. with new problem
@Y0MMY I have updated my answer. But it seems you have a fundamental lack of understanding of what pointers actually are and how they work, so I strongly suggest you stop what you are doing and get some decent C++ books that will cover this topic in more detail for you.
ok, thx for books, but how init. with new? for example, I get some value, and then I insert it to initialize the array. int val = GetValue(); std::shared_ptr<CachedBitmap> CachedBitmap[val];
@Y0MMY std::shared_ptr<CachedBitmap> *CachedBitmap = new std::shared_ptr<CachedBitmap>[val]; but that is trivial if you use std::vector instead, like I showed in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.