![]() |
|
Segmentation Fault for BITMAP |
ThisisNate
Member #7,578
August 2006
|
I'd like to store series of images inside F_Right and F_Left. Here's part of my code. BITMAP** F_Right; F_Right[0]=load_bitmap("right1.bmp",NULL); F_Left[0]=load_bitmap("left1.bmp",NULL);<--SEGMENTATION FAULT OCCURS HERE I've got segmentation fault when the F_Left loads the bitmap images,and 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 Please explain in detail why you think I'm blaming it on a faulty C runtime lib? If you could guide in the right direction, it'd be more constructive and be more appreciated. |
Trent Gamblin
Member #261
April 2000
![]() |
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
![]() |
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. ______________________________________ |
ReyBrujo
Moderator
January 2001
![]() |
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 and try again. -- |
ThisisNate
Member #7,578
August 2006
|
You, guys, are awesome. However, there is one thing I still have a question about. How can I allocate dynamic memory of the BITMAP object for F_left and F_right? If I decided to use double pointer, do I go by (BITMAP**) malloc(sizeof(BITMAP*)*3)? Thanks |
ReyBrujo
Moderator
January 2001
![]() |
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. -- |
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++){ |
amber
Member #6,783
January 2006
![]() |
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
![]() |
No delete, free. -- |
anonymous
Member #8025
November 2006
|
If you are more familiar with C++ then a) if you know that there will be 3 of each: 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"));
|
|