Solved

Array of TBitmaps (#2)

Posted on 2001-07-13
2
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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