Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array of TBitmaps (#2)

Posted on 2001-07-13
2
Medium Priority
?
238 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

885 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