Jump to content
  • Advertisement
Sign in to follow this  
BiGF00T

c functions and their c++ successors :/

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i learned c a while ago at university... then i coded at home and adopted more and more c++ functions... i still use structs and not classes and i still use char* and not string... malloc/new and what else you could think of. i'm now wondering: what would using the newer functions/commands give me? ok, classes/structs are not comparable... but what about char*/string, malloc/new... whats the big improvement? is there a site somewhere with a list of stuff that changed from c to c++ with a description of what exactly changed? or can you give me a quick explanation of things that can be done in c++ with other commands but do nearly the same thing as their predecessors in c? if its possible try to give an explanation of what changed and why it is good that it changed... i am wondering why they invent some things new... there has to be some improvement but its not always obvious. i'm sure they have something different to printf in c++ but i dont know whats the difference... thx in advance BiGF00T ok, now i finished my thread nearly and i found something about the string/char* thing...
Quote:
Q: What is the difference between char* and string? A: char* is a pointer to a character. string (from the standard library) is a class that contains a representation of a sequence of characters plus a range of manipulation functions. Since char* is commonly used in C as a pointer to a sequence of characters, C++ supports this use of "strings" to some degree. For example, class string has a constructor string(char* c_style_string) with which you can create a C++-style string from a char* representation, and there is a function string::c_str() that generates a C-style string from a C++ string. Use C++ strings whenever possible. The use of C-style strings should be restricted to the interface to UNIX and library routines. I.e., if a library requires a char* parameter, then you can pass c_str(your_Cpp_string). Or if a library returns a char*, you can construct a C++ string and use this one from then on.
as a former delphi/pascal coder i missed the strings and used to hate char* and whatever torture datatypes c provided to me. i think i'll stick with the strings :) glad to have them back. are there some cases in which i cant use strings and have to use the char* thing?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Don't know about specific comparisons, but this site:

http://www.gotw.ca/gotw/

and its associated books highlight some issues with C and C++

Share this post


Link to post
Share on other sites
malloc merely grabs some memory. new also calls an object's constructor, and should thus always be preferred in C++ except for some highly specialised purpose (e.g. grabbing memory for later use with placement new) that you don't need to worry about at this point. Note that it is crucial to delete everything that you allocate with new, so that the memory is freed and the object's destructor is called.

Note that when allocating multiple objects you must use new[]/delete[] (e.g. int* x = new int[10]; /*...*/ delete[] x;). Also note that you should never ever mix calls to new/delete with calls to *alloc/free (such as freeing memory allocated with new).

Share this post


Link to post
Share on other sites
so i just use
new/delete instead of malloc/free,
string instead of char*
class instead of struct
... (could this list be continued?)
class/struct is another such thing... classes can do additional stuff and i already read tutorials about them. they seem to be nice and good. but do they complete replace structs? isnt there a single case when using structs should be preferred?
is it generally a good idea to mix c/c++ code or should i try to either use c or c++? sure, mixing new and free wouldnt be a good idea due to the reasons you explained but is there any downside in using structs in c++ programs and creating some objects with new/delete and allocating something else (not the same object's memory) with malloc and free it after a while?
is it considered good programming habit to mix those languages or should i separate them completely (this wont be possible i guess)?

Share this post


Link to post
Share on other sites
When using a C++ compiler, structs will behave identically to classes [with the exception that their default member state is public rather than private], so you can use all of the C++ goodies with them.

Generally, the only time I've seen C++ coders use char */malloc/free is when they're dealing with things like network packets, where direct byte ordering and manipulation is necissary.

Personally, I still use char *'s for text, and malloc/free for anything that isn't a class/struct [which is fairly little these days]

Share this post


Link to post
Share on other sites
Unfortunately it's not that simple. C++ is a different language, although largely backwards compatible, so while almost any C program is also a valid C++ program, that doesn't mean that it's good style. I can only recommend reading about C++; I understand that Effective C++ may be a good book, though I myself have not read it.

As a few pointers; yes, new/delete (and new[]/delete[]) should almost always be preferred (and are essential when constructing objects with non-trivial constructors—and deallocating them), and C++ strings are superior to C "strings" (note that if you need a const char* for compatibility with, say, a C API function, you can use std::string::c_str()).

As for classes vs. structs—C++ makes no real distinction. A struct in C++ is a class (the only real difference between a class declared as a struct and one declared as a class is that the struct defaults to public access and inheritance). It is certainly true, however, that there are cases when all you want is to bundle some data in what is known as a POD ("plain old data") struct. This interview with Bjarne Stroustrup has a few interesting notes, especially page 3.

Other things you may wish to keep in mind: Prefer vectors over plain arrays, prefer polymorphism and generic programming (templates) over void pointers (which you should never use in C++), prefer const identifiers and inline functions over macros, exceptions over setjmp/longjmp ... the list goes on.

As for mixing C and C++, considering the language overlap, I find it an odd question and a difficult one to answer. Keep in mind that good C style is often bad C++ style.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
AK-47 rating++ for everyone :)
thx for the help

EDIT: who fuxx0red teh intarweb? i cant send the reply

*grrrr*
Active Server Pages error 'ASP 0113'

Script timed out

/community/forums/post_info.asp

The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.

Share this post


Link to post
Share on other sites
the last post was from me and the ak47 thing was supposed to be striked out... well i hate the servers, i hope they will burn in hell! it took nearly 20 minutes to send that message and i'm not sure if i'll be able to rate anyone if the servers are working like this.
EDIT: i was able to rate you... thx again :)

[Edited by - BiGF00T on October 12, 2004 7:20:15 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!