Solved

Array of TBitmaps (#2)

Posted on 2001-07-13
2
235 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.
Suggested Courses

624 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