Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » if malloc and remalloc are on a boat...

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
if malloc and remalloc are on a boat...
Derezo
Member #1,666
April 2001
avatar

Oh boy.. should I just post links to previous discussions about oop? ;)

Using a function because it's "more advanced" isn't saying much about it. It doesnt' do the job any differently, as far as I know. You still get the same amount of memory and speed, right? :P

I don't understand the idea behind 'objects' anymore. I did, when I took an oop turing class.. but, then I came to allegro.cc.. and it stopped making sense..

Isn't it basically that you have (for example) a structure.
Eg. PLAYER player;
And it can do things by itself easily.
Eg.
player.run();
player.talk("Hello. My name is Bill.");
player.smile();

cuz, you can do that in C...
I know that in C++ when you make a class
class PLAYER player; // or whatever
it runs a constructor which can automatically do stuff (eg. Set Bill's name) .. and you can have the functions up inside the class..
but.. uhh.. ok, I'm in over my head. :P

"He who controls the stuffing controls the Universe"

23yrold3yrold
Member #1,134
March 2001
avatar

That's the funniest post today ;)

Well, member functions are really not a whole lot different from functions you call with a pointer to a C struct instead. in fact, the difference is a single parameter. The fun comes from inheritence and overloaded operators/functions and templates and friends and such. Nothing you can't technically do in C, but C++ makes some things easier, like writing one template function rather than a function for every type you plan on passing it. You can do OOP in C, but C++ makes it easier IMHO.

Member functions get to operate in their own littke world where the class's member variables are treated like they're global. That's really the only difference.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Bob
Free Market Evangelist
September 2000
avatar

Sorry for joining this late in the conversation - got a lot to say :)

Quote:

And I've never really had a use for realloc() ....

You should. realloc() is oftern significantly faster than doing "new", then copying all the values from one array to the other, then doing "delete" on the old array.
Firstly, the OS can use it's uber-faster memcpy() function. Secondly, no constructors are called. Finally, the OS is free to reuse the same memory space if there is room, so no real allocation occurs.

Quote:

0 will always be safe. The same can't be said for NULL

I guess you should have looked that one beforehand....

Quote:

An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant.

That's on page 61, section 6.3.2.2 of the ANSI/ISO/IEC 9899 standard.

23yrold3yrold: Here's a question for you. If you pass an STL container to a function, does it get passed by value or by reference? If you retun an STL container, will you only get a reference to an object, or weill the entire object be dumped on the stack?

Quote:

c++ is more advanced and abstract than c. if you program in c, then i assume you dont like objects which are the most useful idea to ever come along in programming.

You should have worn a flame suit. Now feel my wrath!!

C++ is NOT more "advanced" (whatever that means). C++ is simply different. You can use objects in C (I use objects in C). Allegro uses objects, and it's entirely coded in C.
Having more or less syntaxical sugar does not change the fact that you are using objects.

Finally, OOP is not he be-all-and-end-all of programming. It's a fad, just like procedural and functional programming. There will be better ways of programming, or re-using the older coding methods.

Take, for example, context-sensitive (or word-based) programming. Whether you program using objects or not, you will still end up with butt-ugly unmaintailable junk. Don't beleive me? Try writting an HTML parser, that works with broken HTML.

Quote:

STL looked a lot more complicated.. with it's classes and whatnot

Classes? STL? Where? I don't think that you even see those on the user-level.

Quote:

Unlike malloc(), new never returns NULL!

Not quite. You can always force "new" to return NULL on error instead of throwing an exception, by using:

Foo *foo = new(nothrow) Foo;

--
- Bob
[ -- All my signature links are 404 -- ]

Marty Dill
Member #277
April 2000
avatar

Bob:

"That's on page 61, section 6.3.2.2 of the ANSI/ISO/IEC 9899 standard."

That is the C99 standard, no? I thought it was fairly obvious I was talking about C++.

Bob
Free Market Evangelist
September 2000
avatar

Ahem.

Quote:

The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>, <cstring>,
<ctime>, or <cwchar>, is an implementation defined
C++ null pointer constant in this International
Standard (18.1).

And:

Quote:

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.

This is from ISO/IEC 14882 ("C++ 98").

In other words, NULL may not be 0, but it always must evaluate to 0.

--
- Bob
[ -- All my signature links are 404 -- ]

23yrold3yrold
Member #1,134
March 2001
avatar

So I'm right to use NULL (why NULL? Why not null?) and not 0, correct?

Quote:

23yrold3yrold: Here's a question for you. If you pass an STL container to a function, does it get passed by value or by reference? If you retun an STL container, will you only get a reference to an object, or will the entire object be dumped on the stack?

By value, and the entire object will be dumped on the stack AFAIK. Did you not know or are you testing me? ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Bob
Free Market Evangelist
September 2000
avatar

For all intents and purposes, NULL and 0 are equivalent. In C, you may need to cast the 0 though, which is why NULL is provided for you.

No, I didn't know that, but I suspected it. I use dynamically allocated STL containers anyway :)

--
- Bob
[ -- All my signature links are 404 -- ]

Marty Dill
Member #277
April 2000
avatar

I take it you didn't read the GameDev thread I linked to :)

Yes, as stated quite explicitly in the aforementioned thread, the standard guarantees that the NULL macro will always evaluate to 0. But 0 is the null pointer constant. The null pointer constant can't be changed. NULL is just a macro. Remember, this is what I was originally replying to:

"isnt it a bad thing to make pointers equal to 0 instead of NULL in the case that in the future NULL is changed from 0 to something else?"

I was jokingly (notice the smiley?) pointing out that this logic is backwards.

Now as for why I think NULL is actually unsafe ... well, I am inherently wary of all macros. Also, if you use NULL, you better make sure that you include one of the aforementioned headers, and I have seen code where that was not the case (thus making the code non-standard).

Anyway, I have to go finish my Engr 240 technical report now ...

kazzmir
Member #1,786
December 2001
avatar

Bob: you say that you dont think c++ and objects are better and more advanced than what C can do.

Well its true that pretty much anything you could do in C++ you could do in C, but sometimes its alot easier. I think of C alot like pascal which i learned to program in. There are a ton of global variables and sometimes very specific functions that are only used once, and it would be nice if those functions were associated with the object that it operates on. You can pass everything by reference and thereby get rid of global things, but thats a huge pain in the ass and globals are easier. OOP is why java was invented, becuase people learned that classes are easier to maintain. Well, that may not be the only reason it was invented, or a reason at all so dont get all mad about it, but it does force you to use classes. C is sometimes better than C++, but assembler is just as much better than C also. The reason why nobody likes to program in assembler is because its so amazingly complicated and hard to understand. Instead of making a ton of mistakes, why not let the compiler write the code for you, maybe not exactly optimized, but alot easier to write and maintain. I dont know about SDL, but i know that QT is written in c++ using classes.

Derezo
Member #1,666
April 2001
avatar

Quote:

There are a ton of global variables

That's called bad coding.. not C ;)

Quote:

very specific functions that are only used once

other than int main(), can't think of one..

I thought a vector was an STL class?
Perhaps I'm confused then..... but, when I was in my attempts to complete chris' article, a vector had so many different uses and functions (within it's class, right?).. it was insane. The only reason why that is "confusing" to me, is because I want to know what the heck they all do.. :P

"He who controls the stuffing controls the Universe"

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I thought a vector was an STL class?

It's a class, but you said "STL looked a lot more complicated.. with it's classes and whatnot". What probably confused you was the template aspect; being able to use many different classes in a vector. Plus some of the internal stuff like iterators. I agree it can be confusing, but once you learn it it's great ;) True, there's a lot of functions (did I mention the string class has about 147?) but many of the containers share functionality, so it boils down to common sense quickly (all containers have size(), isempty(), etc. and they all do the same things).

It boils down to knowing the basics; if you don't understand templates and overloaded operators, you will get conpuzzled (some iterators use '++' to go backwards. Wild, huh?). And there's a lot of functions but I think I split them up into logical segments. Hell, if I can learn scripting you can learn C++ ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

Chris, you shouldn't be intimidated by Sven (especially since I despise him).

Both

Fred* p = new Fred();

as well as

Fred* p = new Fred;

are equivalent (in this case).
The thing one SHOULD worry about is the difference between:

Fred p();

and

Fred p;

Ouch!

Here's one for the Book O' Flame Quotes:
Using C instead of C++ is like using K&R C instead of ANSI C.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

The thing one SHOULD worry about is the difference between:
Fred p();
and
Fred p;
Ouch!

That I knew.

Quote:

Both
Fred* p = new Fred();
as well as
Fred* p = new Fred;
are equivalent (in this case).

That I didn't. I think I'll stick with the non-bracketed version in all cases; saves me from error :)

EDIT: I had something written here; I think I got moderated or something for no reason (as usual ::) ). Who's the dumbass .....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Bruce Perry
Member #270
April 2000

Quote:

For all intents and purposes, NULL and 0 are equivalent. In C, you may need to cast the 0 though, which is why NULL is provided for you.

Not true. In C, 0 can always be implicitly cast to a null pointer of any type. Even on systems where the bit pattern of a null pointer is not all zeros, the C compiler will convert the integer constant 0 into the correct null pointer.

What you have to be careful of is optional arguments. If you need to pass a null pointer as an optional argument, you MUST cast it appropriately:

void myfunc(int x, ...);

myfunc(x, (int *)0);

This problem applies equally to functions declared implicitly, but in very nearly all cases you shouldn't have to do this. I can only think of one case where you'd have to use implicit declarations, illustrated by the following quote from DJGPP's stdlib.h file:

/* These vary in expected prototype, so we just don't prototype them.
void            xfree(void *_ptr);
void *          xmalloc(size_t _size);
void *          xrealloc(void *ptr, size_t _size);
*/

It should be noted that these functions are not standard.

Under ANSI C, NULL is defined as one of the following:

#define NULL 0
#define NULL ((void *)0)

The latter will help protect you if you accidentally use NULL where an integer constant was wanted, e.g. as a NUL terminator (yes, one L!) on the end of a string. However it will not get around the optional arguments problem; you must cast.

This is covered in detail in the C FAQ. I can't comment on C++.

---

Quote:
-----------------------------------------

FOO *bar = malloc(sizeof(*FOO));
bar = realloc(bar,sizeof(*FOO) * 2);

-----------------------------------------

Wrong! First it should be sizeof(FOO) or sizeof(*bar), but that aside...

If realloc() cannot find enough memory for the new array, it will return NULL, without freeing the memory you've got already. Assign the same pointer like this and you've got a memory leak! The following would be correct code:

FOO *bar = malloc(sizeof(FOO));
FOO *baz = realloc(bar,sizeof(FOO) * 2);
if (!baz) {
    free(bar);
    return NULL;
}
bar = baz;

Rash despises Sven? Now that I didn't know. I thought crazy_k and I were the only victims. ::)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Apokalypse
Member #2,543
July 2002
avatar

Quote:

C++ is NOT more "advanced" (whatever that means). C++ is simply different. You can use objects in C (I use objects in C). Allegro uses objects, and it's entirely coded in C.
Having more or less syntaxical sugar does not change the fact that you are using objects.

Bob:
What exactly do you mean by "syntaxical (sic) sugar"?

It was my understanding that "syntactic sugar" referred to superficial changes to a language to make them more readable by programmers. For example redundant keywords (such as "unless" in Perl) which try to make code fragments superficially appear like natural language.

Virtual functions (what I assume you were referring to) are not syntactic sugar because they alter the structure of the language in a significant way.

I agree that saying one language is universally (i.e. mathematically) better than another is meaningless. But given that this is a game programming forum we can assume the application of a language being discussed is to programming some kind of game.

A major part of most games more complex than pong are their simulations of worlds (however small). It is a simple fact that C++ does simulation better and easier than C. In fact that's what Simula, the forerunner of C++, was designed for.

So, with that qualification, we can reasonably say "C++ is more advanced in the field of programming complex games".

spellcaster
Member #1,493
September 2001
avatar

Nah. Why do you think it's more advanced?
As bob said, it simply gives you syntactical sugar. There's no real improvement from c to c++.

The language itself hasn't changed much, so there can't be much of an improvemement. In fact, many of the gnu extensions to C are IMO more helpful than the c++ fluff.

And, regarding OO: If you think OO, and make a nice OOD the actual coding can be made in either c or c++.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Bob
Free Market Evangelist
September 2000
avatar

Quote:

Virtual functions (what I assume you were referring to) are not syntactic sugar because they alter the structure of the language in a significant way.

Wait. Are you saying that you cannot have virtual functions in C?

  • Bob equips his flame suit.

Someone's in for a spanking.

--
- Bob
[ -- All my signature links are 404 -- ]

Rash
Member #2,374
May 2002
avatar

Come on, guys, all these "linked list" threads here should be enough to convert even the most extremist C zealot.

P.S. I don't despise you two, Ben.

spellcaster
Member #1,493
September 2001
avatar

Why? A linked list is a dead simply data structure... if you can't code a linked list, chances are good that you will have problems sooner or later anyway (regardless of the lang used).

And all the time I see the STL, I know why I don't like C++. :)
I mean it's like: "Oh, we forgot to create a common base class... let's create a new language gimmick, which gives you something like define, just with a bit more compiler support."

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Korval
Member #1,538
September 2001
avatar

I hate to point something out here, but the logical argument of "realloc == vector" is false.

Let's say I load a 2MB file from disk. I take the data, and compress/alter it into a 500K block. Note that the data is not structured into an array format. It is structured data, but no C/C++ object can be wrapped around its entirty. Therefore, it cannot be a vector<char>.

Had I allocated this block of memory with "new char[size]", I would have to create a 500K block, copy the converted data into it, and delete the old one. I have now moved this memory to new addresses, and I therefore must go through my pointers into various pieces of this data and adjust them to compensate.

On the other hand, if I used "malloc", I could "realloc" that memory down, with full knowledge that it will not change the pointer itself.

The only trouble I have with things like this is that "new" and "malloc" are not interchangable. I have to somehow remember that some memory was allocated with "malloc" and some was allocated with "new". It's easy when it's just objects or arrays of objects. But, if it is just a buffer, it becomes more difficult. Therefore, I have implemented the convention that all "buffers" are allocated with "malloc", and everything else with "new".

As for the C/C++ debate yet again, I find it strange that seasoned programmers like Bob simply refuse to use C++ for no logical reason. Calling it "syntactic sugar" is simply infantile. OOP in C is error-prone, and there is no language facility you can use to prevent that. Instead, it all relies on faith. Faith that all the v-tables are initialized properly. Faith that everybody calls the initializer (over all elements of the array) when they create an object (or that they create all objects of that type through one function). Faith that the user will always destroy an object via a certain function or call a certain destructor beforehand.

I don't need faith when I use C++. I know the v-tables are all fine. Otherwise, I'd have gotten a compile error (unless the compiler is broken, in which case I'm SOL anyway). As long as I use the C++ new/delete to allocate/deallocate object, constructors and destructors will be called automatically. They'll even be called before the program begins for global objects.

Oh, I can trick C++. I can start casting things from one object to another and break it. But that's cheating and isn't expected to work anyway. The C version of OOP has no language support; it relies totally on programmers to follow a particular convention. In this maner, C++ is idiot-proof.

Not only that, if I use C++ dynamic casts to do all of that casting, the RTTI would have returned NULL instead of a valid pointer, thus causing a quick and obvious error. Your method has no such failsafes.

Languages are just tools for programmers. The only reason a programmer should select one over another is because it better fits the design for the program in question. Generally, from a well-thought out design, it is easy to see what features you need out of a language to efficiently implement the design.

For example, if I wanted to make a cross-platform networked version of Tetris, C/C++ with Allegro and a networking library isn't the most optimal solution. I'd use Java. That is the most reasonable language for that project. Here's why:

1) Java's cross-platform. Sure, so are C/C++, but each platform needs to recompile the sources. Java doesn't have these limitations.
2) Speed isn't much of an issue. It's Tetris, after all. It's not that taxing, so the speed advantages of C/C++ aren't relevant.
3) Networking is a breeze in Java. C/C++ doesn't provide easy cross-platform networking support. Certainly not the way Java does.

Therefore, if you're serious about implementing a cross-platform, networked version of Tetris, then Java is the language of choice. You, of course, may implement it in something else, but this is a far less reasonable thing to do. By not using Java, you are intensionally using a weaker tool to solve this problem.

The same goes for any design like that. By forcing OOP into a language (like C) that doesn't have intrinsic support for it (which it doesn't. You have to build it off of existing functionality), you are choosing to use a tool that is less efficient (at OOP) than a language that does intrinsically support OOP.

It's like using an electric screwdriver to beat a nail into wood. Yeah, you can do it. But you'd be better off with a simple hammer than using an electric screwdriver. You'll never see any real carpenter using a screwdriver to drive a nail, because he knows to use the right tool for the right job.

It's about time programmers learned this simple lesson.

Bob
Free Market Evangelist
September 2000
avatar

<rant>

Quote:

As for the C/C++ debate yet again, I find it strange that seasoned programmers like Bob simply refuse to use C++ for no logical reason. Calling it "syntactic sugar" is simply infantile. OOP in C is error-prone, and there is no language facility you can use to prevent that. Instead, it all relies on faith. Faith that all the v-tables are initialized properly. Faith that everybody calls the initializer (over all elements of the array) when they create an object (or that they create all objects of that type through one function). Faith that the user will always destroy an object via a certain function or call a certain destructor beforehand.

Who ever said I didn't or refused to use C++? I mainly use C, but that doesn't stop me from using C++ when it's needed.

I never said that C++ is useless, and that all programming should be done in C. What I keep fighting against is the idiotic notion that C++ and OOP are one and the same, and that C is incapable of doing any form of OO.

IMO, OOP in C is no more error prone in C than in C++ (let's ignore casting here). Initializing the vtable is not really difficult, and since it's done in your code, I don't see where the "faith" element comes in. Of course, if compilers (*cough* MSVC cough) could that correctly all the time, then you may have a point. Even then, I would notice that I did a jump in address 0x00000000 and quickly correct the missing entry.

I don't really see the point in saying I should have "faith" in users calling the constructors/destructors correctly.
Just as users are expected to call delete object in C++, they can call object->delete(object), or more simply destroy_class(object).
If they call the wrong function, they get a nasty casting error. If they don't call the function at all, then they have a memory leak, same as in C++.

About initializding elements in arrays, I'm sure that 90% of the time, your objects are meaningless (don't work properly) without a non-default constructor. Thus, you'd need to call a constructor or at least a setter method on each object in an array anyway.

Quote:

In this maner, C++ is idiot-proof.

Nothing is ever idiot-proof. There will always be bigger idiots.

I understand what you mean though: the compiler does the work. As I've stated before, it's simply syntaxical sugar. There's no reason why you can't do it yourself. There's no reason why it can't be done in C.

Quote:

Not only that, if I use C++ dynamic casts to do all of that casting, the RTTI would have returned NULL instead of a valid pointer, thus causing a quick and obvious error. Your method has no such failsafes.

As there is RTTI in C++, you can also implement it in C. Just like you wouldn't do straight casts in C++ and expect RTTI to kick in, you wouldn't do that in C either. Rather, you'd wrap a function that would take a struct, identify its type, look it up in a table to find if it's a child of the type that you want to cast down to, then return a pointer to the parent.
Sure it's ugly and takes a lot of wrok to code, but the fact remains that you can do it in C.

Remember, the first C++ compilers compiled the C++ code down to C code, and then handed it over to C compilers.

Quote:

By forcing OOP into a language (like C) that doesn't have intrinsic support for it (which it doesn't. You have to build it off of existing functionality), you are choosing to use a tool that is less efficient (at OOP) than a language that does intrinsically support OOP.

I mostly agree.
Notice that this doesn't contradict anything that I've said above, or in any other thread on the subject.

</rant>

--
- Bob
[ -- All my signature links are 404 -- ]

kazzmir
Member #1,786
December 2001
avatar

a tear comes to my eye every time i look at the thread count and see a large number for a thread i started,.. but anyway, this was originally about malloc and new and whatnot and not about c++ vs C. i think everyone is a little tired of arguing, so either post in a new thread or give it a rest. no one is going to hate you for using C or C++.

orz
Member #565
August 2000

What's that? There's nothing you can do in C++ that you can't do in C? Hehe, try this...:

void my_function(); //whatever this does

class __call_before_main {
public:__call_before_main ( void (*func)()) {func();}
};
#define CALL_BEFORE_MAIN(a) static __call_before_main _call ## a ( a ) ;
CALL_BEFORE_MAIN(my_function);

Okay, seriously, I don't see why anyone would choose to use pure C over some kind of C++ unless they were either
1. A library-writter,
2. An embedded developer, or
3. In some other unusual circumstance involving binary compatibility and/or performance.

C++ being almost a super-set of C, it's easy to go part-way and write C++ that's basically C. And once you're part-way to C++, it's easy to start using some random C++ feature that can make your life a little easier... and then another feature... and then another...

Seriously, I switched to C++ 4 years ago for the unified name-spaces. A few months later, I was using it for the virtual tables. I just recently decided to start using function and operator overloading, though I remain convinced that many coders go over-board with that feature. Now I'm starting to get tempted by the idea of templated hashtables...

Korval
Member #1,538
September 2001
avatar

Quote:

What I keep fighting against is the idiotic notion that C++ and OOP are one and the same, and that C is incapable of doing any form of OO.

I never said that C is incapable of OOP. I said that C doesn't support OOP.

Specifically, I mean that the C language does not provide intrinsic support for OOP. It doesn't not provide intrinsic support for objects that self-initialize/deinitialize. It doesn't provide intrinsic support for inheiritance or polymorphism either.

Once again, that's not to say that these things can't be done in C. It merely says that, in order to do them, you must create these facilities. Take Allegro, for example.

If you create a BITMAP * using what ANSI C says is a good way to create one (ie, (BITMAP *)malloc(sizeof(BITMAP));), then, under Allegro, you mave made a mistake. Under Allegro, there are specific functions for creating BITMAPs. And only those functions may be used to create valid BITMAP*'s that can be passed to other Allegro functions.

According to ANSI C, there is nothing wrong with malloc'ing that BITMAP object. That is a perfectly legitimate creation of a BITMAP, and functions that expect a BITMAP* are guarenteed (when they are allocated that way) that the pointer points to an area of memory that is both valid (though not symantically valid) and is large enough to hold a BITMAP.

It just so happens that, symantically, according to Allegro, this is a bad idea. Allegro expects certain things (like the v-table pointer) to be filled in. It may even expect the actual bitmap bits to follow (in memory) the BITMAP data structure. ANSI C does not guarentee any of these things. These can only be guarenteed by user code. Therefore, OOP is supported, not by ANSI C, but only by the program-defined conventions of the use of objects.

By contrast, C++ supports OOP in the language. Let's take the above example, but assuming BITMAP were a proper C++ class object. Under ANSI C++, the correct way to allocate this object is with "new BITMAP;". The blank constructor, assuming a robust implementation, would function like "create_bitmap" with some meaningful, though utterly arbituary, values. Therefore, what you get is a meaningful BITMAP object.

This object is ready to be used by any BITMAP function (most of which are members of the BITMAP, following standard OOP via C++). In fact, the only way to create a BITMAP object that is invalid is to violate ANSI C++. You can "malloc" one, but ANSI C++ specifically warns that this will not create a properly initialized object.

By using the facilities of the C++ language, you can guarentee (as long as the user follows ANSI C++) that the user cannot break certain things. The user cannot create invalid objects. If you're using public/private correctly, he can't directly damage the data. In short, you can make it really difficult for someone to break your underlying code as long as they stick to ANSI C++.

Quote:

Not quite. You can always force "new" to return NULL on error instead of throwing an exception, by using:

I really hate C++ exceptions. They really should be optional. You mean I have to go through my entire codebase and replace "new" with "new(nothrow)"? I guess that's what #defines are for ;)

Bob
Free Market Evangelist
September 2000
avatar

Quote:

I said that C doesn't support OOP.

Just like 33.6 modems also "support" 56 K (via a small firmware upgrade - hehe) ;D

Sorry, I got confused by what you were trying to get to.

Quote:

If you create a BITMAP * using what ANSI C says is a good way to create one (ie, (BITMAP *)malloc(sizeof(BITMAP))

Actually, this will just give you a chunk of memory that's sizeof(BITMAP) chars wide. There's no ANSI C way of creating objects. You'll have to go through the special Allegro functions to do that.

I'm not sure that Allegro does all the apropreate chaecking everywhere (it does it at some places), but it is feasable that using calloc (or malloc followed by a memset to zero) would still build a valid BITMAP object for Allegro.

Of course, if a user is bent on trying to break your code, there's little you can do to stop him, in either C or C++.

In conclusion, I generally agree that C doesn't support OOP intrinsically. C++ is much better at it, but even then isn't quite up to the task. Objective C, Java (and probably some other languages) are much better at the task.

--
- Bob
[ -- All my signature links are 404 -- ]

 1   2   3 


Go to: