Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_destroy_bitmap() crashes when called

This thread is locked; no one can reply to it. rss feed Print
 1   2 
al_destroy_bitmap() crashes when called
beoran
Member #12,636
March 2011

Hey, William, even I I had to break out cdecl for that one, but now I understand too. Arguably it's not that useful except in cases where you happen to have a constant size array you want to pass to a function.

And that's what I mean with "learn C first". If you learn C from learning C++, you won't learn C well. And if you don't know C well, then there's many fine points of C++ you won't be able to learn well either. Even Stoustrup learned C first. :)

William Labbett
Member #4,486
March 2004
avatar

Here's one that cost me probably days of tedious debugging.

variable = a + b + c + get_some_variable() == 5 ? 10 : 20;

Call me a dunce but I made that mistake many times.

variable = a + b + c + (get_some_variable() == 5 ? 10 : 20);

Is what I mean.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I'm guessing that's only valid in C becase C is retarded sometimes, allowing you to declare functions without any input parameters. C++ would never let you get away with that.

Edit
Ah, I see. That is an integer pointer, not a function pointer. Why does it use a function pointer syntax int (*ptr); though? That's one of the odder things that I didn't know about C.

Trent Gamblin
Member #261
April 2000
avatar

Parenthesis are probably ignored if there's not a second set like int (*ptr)().

Arthur Kalliokoski
Second in Command
February 2005
avatar

I was thinking it was the same as how

int array[10];
printf("%d\n",array[5]);

is equal to

int array[10];
printf("%d\n",5[array]);

For what it's worth, all three of these functions print the value in the offset passed.

#SelectExpand
1#include <stdio.h> 2 3void function1( int (*ptr)[10] ) 4{ 5 printf("%d\n",ptr); 6} 7 8void function2( int * ptr[10] ) 9{ 10 printf("%d\n",ptr); 11} 12 13void function3( int ptr[10] ) 14{ 15 printf("%d\n",ptr); 16} 17 18int array[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; 19 20int main(void) 21{ 22 function1(array[5]); 23 function2(array[5]); 24 function3(array[5]); 25 return 0; 26}

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

beoran
Member #12,636
March 2011

Well, yes, Arthur, but the program should give you warnings when compiled because you're converting an integer to a pointer and then back to an integer when you use printf().

Maybe the following modification would be instructive:

#SelectExpand
1#include <stdio.h> 2 3void function1( int (*ptr)[10] ) 4{ 5 printf("%p %p %d\n", ptr, (*ptr), (*ptr)[5]); 6} 7 8void function2( int * ptr[10] ) 9{ 10 printf("%p %p\n", ptr, ptr[5]); 11} 12 13void function3( int ptr[10] ) 14{ 15 printf("%p %d\n", ptr, ptr[5]); 16} 17 18int array[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; 19int * array2[10] = { NULL }; 20 21 22int main(void) 23{ 24 function1(&array); 25 function2(array2); 26 function3(array); 27 return 0; 28}

 1   2 


Go to: