Solved

Array of TBitmaps (#2)

Posted on 2001-07-13
2
229 Views
Last Modified: 2006-11-17
I'm still having some trouble with an array of TBitmaps in bcb4. I'm declaring it like this:

Graphics::TBitmap *Pictures[10] = {new Graphics::TBitmap};

But when I try any operations on the array, like

Pictures[i]->Transparent = true;
or
Pictures[i]->LoadFromResourceID((int)HInstance,i + 1500);

I get an EAccessViolation with message 'Access violation at address blahblah Read of address FFFFFFFF. What am I doing wrong? If I change Pictures to be a single TBitmap, it works fine.
0
Comment
Question by:TropicalFish
  • 2
2 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6281670
You coudl have asked this on the previous question.  (its close enough and you offered enough points)

The code

Graphics::TBitmap *Pictures[10] = {new Graphics::TBitmap};

creates an array of 10 pointers to TBitmap objects.  It initalizes the first pointer (Pictures[0]  (not 1) ) to point to a new dynamically allocated object.  The remaining 9 pointers are left uninitialzied.  If you try to dereference them, you are using an invlaid pointer and will probalby crash.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6281688
If you want to initialize each of the 10 entries, you have to do something like

Graphics::TBitmap *Pictures[10] = {new Graphics::TBitmap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap,
                                                         new Graphics::TBitMap};

Or better yet (maybe better)

Graphics::TBitmap *Pictures[10];

for (inti i = 0; i < 10; ++i)
   Pictures[i] = new Graphics::TBitMap;

AND REMEMBER the array is zero based.  You use an index of 0 to access the first item.

Also is there a reason why you are using an array of pointers to objects, rather than an array of the objects directly?  (Sometimes that is necesary, but if not, its a lot of extra work for nothing.)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now