Parsing Pointers in C++

Started by
18 comments, last by FXO 22 years, 8 months ago
The only issue with malloc and calloc (and all low-level memory allocation functions) is that they do not call the default constructors for the objects being created. That''s not a problem with built-in types, of course. But with anything else, that might cause some serious problems, which is why new and delete were devised.
Advertisement
btw. Brett, you wanted more links,..
So I thought I''ll send you some of my favourites:

Tomohide Kano has some great technical OpenGL demos at:
http://cgi3.tky.3web.ne.jp/~tkano/

More technical demos:
Objectecture! - http://www.r3.nu/

Great visability testing lib, with demos.
Check out the city demo - 16000 houses & 4000 cars, runs at about 8-16 fps on my machine (p2-400):

http://www.hybrid.fi/umbra/


Hope you all find something you enjoy
1) malloc & calloc and free and such don''t necessarily play well with new and delete. Use one or the other - not both.

2) std::string is in #include (no .h)
MSDN should document their implementation of STL pretty well. Yes, its a string handler

3) You''re welcome to post links yourself, its automatic (I just have to tick them off). I''ll come back to these in a bit if you don''t get a chance. No time right now sorry

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL
~~~Cheers!Brett PorterPortaLib3D : A portable 3D game/demo libary for OpenGLCommunity Service Announcement: Read How to ask questions the smart way before posting!
You can use malloc (and related functions) and free in the same code that uses new and delete. If you make sure that delete is only used for blocks allocated by new and that free is only used for blocks allocated by malloc, everything is fine. Otherwise, you''ll run into some serious trouble.

So, I do not recommend doing this since it''s all to easy to "forget."
merlin9x9 wrote:
The only issue with malloc and calloc (and all low-level memory allocation functions) is that they do not call the default constructors for the objects being created.

Thanks, I didn''t know that, perhaps its time to read up on my C++


Merlin,

You are completely correct. However I never tell people what they can do, I tell them what they should do

Another interesting thing, relating to constructors and destructors: don''t delete a casted pointer.

Bad:
void *pObject = new Object(); // works!delete pObject; // free''s Object, but doesn''t call destructor 

instead:
delete (Object) pObject; 


This is ok:
  class B { virtual ~B() { ... }}class A : public B { virtual ~A() { ... }}B *pA = new A(); // successdelete pA; // will call ~B because it is virtual.  


So always use virtual destructors (required if you have a virtual function).


~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL
~~~Cheers!Brett PorterPortaLib3D : A portable 3D game/demo libary for OpenGLCommunity Service Announcement: Read How to ask questions the smart way before posting!
I upgraded to MSVC 6.0, and recompiled my code.
Now the program hangs with a page fault when I call
"strupr()" with static text - ie. strupr("text").

I always thought that when calling a function with a static value, the function would dynamicly create a local copy of the value, is this false?

btw. how do u get those fancy code-like-boxes in =?

/Thanks

Edited by - FXO on August 2, 2001 10:36:35 PM
RTFM

strupr will convert the string you give and return a pointer to it. (that''s my impression).
So instead:
char str[256];
strcpy( str, "text" );
str = strupr( str );

But I can''t stress this enough: use std::string if you can. Learn and love STL it''ll save you time. it took me a long time to convince myself to use it, but no turning back. I''ve started converting PortaLib code too.

It''s not the quality of Java''s class libraries (the templates make it a bit more confusing), but it''s much easier than doing a lot of this stuf yourself.



The funny blocks: ubb tags. they are wrapped in [ ] and the good ones are : url, source, code. There are instructions somewhere on this site...

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL
~~~Cheers!Brett PorterPortaLib3D : A portable 3D game/demo libary for OpenGLCommunity Service Announcement: Read How to ask questions the smart way before posting!
quote:
strupr will convert the string you give and return a pointer to it. (that's my impression).


Thanks, that must be it.
I'm a little surprised that I't compiled and ran on VC 5.0 though.

I can't think of any other way it would work, except if the func would return a char[1024] or similar, but then the length of the string would be limited.



I appreciate the tip about STL, and I'd like to try it out,
but I can't find the file "no.h".

I've searched my "Vc98" dir for files containing "std::string",
perhaps my compiler doesn't support it.


Edited by - FXO on August 4, 2001 11:43:55 AM
no.h? Not sure what that is. Never heard of it.

std::string is in . I'm pretty sure VC5 supports it, but I may be wrong.

Borland 5 and GCC both do, both of which are free. (They're implementation is a little different to Microsoft's though... but easy to get aronud in a portable way).

VC98... isn't that VC6? That definitely supports it.

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL


Edited by - brettporter on August 5, 2001 4:54:16 AM
~~~Cheers!Brett PorterPortaLib3D : A portable 3D game/demo libary for OpenGLCommunity Service Announcement: Read How to ask questions the smart way before posting!

This topic is closed to new replies.

Advertisement