Solved

Array of TBitmaps (#2)

Posted on 2001-07-13
2
230 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

21 Experts available now in Live!

Get 1:1 Help Now