Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Segmentation Fault for BITMAP

This thread is locked; no one can reply to it. rss feed Print
Segmentation Fault for BITMAP
ThisisNate
Member #7,578
August 2006

I'd like to store series of images inside F_Right and F_Left.
Therefore, I decided to use double pointers.

Here's part of my code.

BITMAP** F_Right;
BITMAP** F_Left;

F_Right[0]=load_bitmap("right1.bmp",NULL);
F_Right[1]=load_bitmap("right2.bmp",NULL);
F_Right[2]=load_bitmap("right3.bmp",NULL);

F_Left[0]=load_bitmap("left1.bmp",NULL);<--SEGMENTATION FAULT OCCURS HERE
F_Left[1]=load_bitmap("left2.bmp",NULL);
F_Left[2]=load_bitmap("left3.bmp",NULL);

I've got segmentation fault when the F_Left loads the bitmap images,and
I don't know why it gives a seg.fault while the F_Right[x]=load_bitmap... do not get any.
If I switch the order (F_left loads first and then F_right) then F_right gets seg-fault. :(
It seems like allegro won't let me load bitmap more than one variable.

Any suggestion or advice would be appreciated.

Thanks in advance

anonymous
Member #8025
November 2006

char* string;
strcpy(string, "hello world");

Would you expect something like this to work and blame a faulty C runtime library if it doesn't?

ThisisNate
Member #7,578
August 2006

To anonymous
Honestly, I do not get your point.

Please explain in detail why you think I'm blaming it on a faulty C runtime lib?
I do not blame anyone or anything.
I'm just seeking the answer, and what I posted is based on my observation.
I don't even say "screw this.... and that and so on...." bitching about crap.

If you could guide in the right direction, it'd be more constructive and be more appreciated.

Trent Gamblin
Member #261
April 2000
avatar

BITMAP** F_Right;

F_Right[0]=load_bitmap("right1.bmp",NULL);

You haven't actually allocated any space in F_Left or F_Right. You either have to declare the array size when you declare the variable, or malloc/"new" the memory yourself.

You also need to check the return value of load_bitmap and make sure it's not returning NULL.

someone972
Member #7,719
August 2006
avatar

#SelectExpand
1BITMAP** F_Right; 2BITMAP** F_Left; 3 4F_right = (BITMAP**) malloc(sizeof(BITMAP*)*3); 5F_left = (BITMAP**) malloc(sizeof(BITMAP*)*3); 6 7F_Right[0]=load_bitmap("right1.bmp",NULL); 8F_Right[1]=load_bitmap("right2.bmp",NULL); 9F_Right[2]=load_bitmap("right3.bmp",NULL); 10 11F_Left[0]=load_bitmap("left1.bmp",NULL);<--SEGMENTATION FAULT OCCURS HERE 12F_Left[1]=load_bitmap("left2.bmp",NULL); 13F_Left[2]=load_bitmap("left3.bmp",NULL); 14 15//later 16free(F_right); 17free(F_left);

I think that's right, but it's untested and I'm a bit rusty with dynamic arrays.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

ReyBrujo
Moderator
January 2001
avatar

In BITMAP** F_Right; F_Right is a pointer to a pointer. That means, it contains the address to a pointer. However, you never initialized it, so it is pointing somewhere (who knows where). When you try to access F_Right[0], since the pointer has never been initialized, it can break.

Change that to

BITMAP* F_Right[3] = { NULL, NULL, NULL };
BITMAP* F_Left[3] = { NULL, NULL, NULL };

and try again.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

ThisisNate
Member #7,578
August 2006

You, guys, are awesome.

However, there is one thing I still have a question about.
I'm not quite familiar with allegro and C. (I'm more familiar with C++.)

How can I allocate dynamic memory of the BITMAP object for F_left and F_right?
(Obviously, it's not going to be F_left=new BITMAP(), right?)

If I decided to use double pointer, do I go by (BITMAP**) malloc(sizeof(BITMAP*)*3)?

Thanks

ReyBrujo
Moderator
January 2001
avatar

create_bitmap and load_bitmap malloc necessary memory for the bitmaps. You only need to allocate memory for the pointers. So, you only need to do as you just wrote there.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

ThisisNate
Member #7,578
August 2006

So when I want to free memories for F_right and F_left, I do of the followings, correct?

for (int i=0;i<3;i++){
destroy_bitmap(F_Right[i]);
destroy_bitmap(F_Left[i]);
}//for
delete F_Right;
delete F_Left;

amber
Member #6,783
January 2006
avatar

Because it's C, you wouldn't delete, of course. You'd free() anything you malloc()'d and destroy_bitmap() anything you create_bitmap()'d.

That said, if you'd rather work in C++, just because Allegro is written in C doesn't stop you from using in a C++ program.

ReyBrujo
Moderator
January 2001
avatar

No delete, free.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

anonymous
Member #8025
November 2006

If you are more familiar with C++ then

a) if you know that there will be 3 of each:

const int image_count = 3;
BITMAP* F_Right[image_count] = { 0 };
BITMAP* F_Left[image_count] = { 0 };

like ReyBrujo suggested (there is no point in adding complexity with dynamic allocation if you don't need it).

b) if there could be any amount of images

std::vector<BITMAP*> F_Right;
F_Right.push_back(load_bitmap("left1.bmp"));
F_Right.push_back(load_bitmap("left2.bmp"));
F_Right.push_back(load_bitmap("left3.bmp"));

Go to: