C
Curt Welch
I could use some help. After 35 some odd years as a C programmer, I've
decided it's finally time to learn C++. I've got a year+ experience with
Smalltalk and about a year of experience with Java as well as experience
with Objective C, so all the OO concepts or the mechanics of C I know very
well. But what I'm having trouble figuring out is the details of what the
C++ language does for you, and what it doesn't do for you, with all the
complexities of memory management.
I'm talking a class on C++ at my local community college, which is working
well at it's intended purpose of motivating me to actually put the time in
to learn the langauge, but it's not working well at actually teaching me
anything because I'm way ahead of the class (I think I might know C and C++
better than the teacher). Though it's a second semester class listed as
"Advanced C++", it's really just "Beginning Programming in C++ - class II"
as he's still going basic concepts like pointers and what a class is.
I'm the type of guy that is not happy unless I understand all the nuts and
bolts of the language. If I don't know the langauge well enough to write a
compiler for, the I don't feel like I know it well enough to use it.
I
certainly don't know the C++ features well enough to do that yet.
I'm trying to understand what the C++ compiler will do for you, and not do
for you, in terms of calling constructors and destruction so you can be
sure when you write code that our not creating memory leaks, or dangling
pointers, or all the other problems that can show up. First, I thought you
had do it all yourself, like with straight C, but that would use new and
delete. But then I learned new and delete was only if you wanted to
manipulate pointers but that you could use local object variables and it
would do it for you. Then I thought object variables were actually
implemented as pointers (like Java) where C++ would do the new and delete
for you behind the scene. But then I figured out it was just straight C
struct semantics and the local objects were just being allocated on the
stack, and all C++ was was doing for you was a hidden calls to constructors
and destructors. That seemed cool. So I was thinking the trend was not to
use object pointers and new or delete unless you needed dynamic structures.
It seemed that if you just used object variables, C++ would make sure your
destructors and constructs were always correctly called.
But then I found out that when using call by value and assignment, C++
didn't take care of things for you after all. So if you have objects that
need special care, you have to supply the copy constructor and overide the
assigment operator to make sure things are done right.
So, I'm getting there. But I'm still somewhat lost becuase the books I'm
reading don't explain what the C++ compiler is doing for you behind the
scene. They show by simple examples how to write the assignment operator,
but they don't explain what the default assignment operator does for
example.
To really understand what's going on here, I need to know what's happening
behind the scene, and that's what I've not found documented anywhere yet.
So the first real question to the group, is where do find this out? Is
there a good FAQ, or web site, or books, you can recommend that will
concisely and simply give me _all_ the facts I need to know about these
sorts of things - especially one that's intended for an advanced programmer
to quickly pick up what the compiler is really doing for you (thinks like
"making a function virutal means it's added to the structure as a function
pointer and all calls the the virtual function are indirected though the
function lookup table in the object").
I did find this web page last night which was very good (but really only a
start and not all the answers I need), I'd like to see more like this on
the rest of the language...
The Anatomy of the Assignment Operator
http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
A lot of these sorts of things I figure out on my own by writing lots of
short test programs and doing a lot of reverse engineering work - but
that's very timing consuming and I was hoping someone could recommend some
good material to read.
And example of a simple specific I don't yet understand how how something
like string works (and when it won't work).
If I have a simple class:
Class Person
{
string name;
int age;
};
And using it as local automatic variables:
Person a, b;
And write something like:
a = b;
What happens? Have I just created a memory leak with the string or will
that work? I don't know how the string class is implemented (yet), so I
can't guess what it's doing. And I don't know what the C compiler does by
default for assignment like the above, so I don't even know if it's
possible for the string class to do the right thing and not create a memory
leak.
If I wrote a class like the Person example above, do I need to overload the
assignment operator to make it work correctly? And how do I write the
assignment operator to make it work?
Does the default assignment operator just do the same thing as struct
assignment in C? That is, just do a raw byte copy of the struct from b to
a in the above without any calls to constructors or destructors? And, if
it does that, is the string class implemented in some way that prevents
that type of assignment from creating a memory leak (I'd guess it would
have to implement its own memory management and do it's own garbage
collection if that was the case - which doesn't sound reasonable).
Or does it generate a default version of the assignment operator for every
class which will do a member by member assignment - which would then call
the string object assignment operator function to "do the right thing" for
the string? In which case the assignment of the Person class objects would
not create a memory leak or any other problem and it work work?
And in general, if I create a class like the Person one above and use any
object in it, will it work as long as assignment of all the members is
correctly supported by all the member objects?
And if you use use object pointers instead of objects in your class and
allocated the objects in your constructor with new, is default assignment
guaranteed not to work if the pointers have owner responsibility for the
memory pointed to becuase pointer assignment will always by default just
assign pointer A to pointer B without any help from the C++ compiler doing
calls to destructors and copy constructors or object assignment functions
for you?
And, how do I find out how string is implemented? When you do an
assignment, does it make copies of the strings, or used shared strings with
reference counts? Are they mutable? Or is it a reference count with
shared objects (like java objects), or does it us a copy on write strategy
to make assignment fast but make then act as if the strings had been copied
no assignment?
The above is typical of the type of thing I'm trying to work through
currently.
Some of this may be in the books I already have, but it's going to take
some time to get though them. The book for the class is the 2" thick
Deitel "C++ How to Program" book which is ok, but very verbose for someone
like myself which is an experienced C programmer just trying to pick up
intimate details of the C++ extensions to C. I can read an entire chapter
of that book and learn only 3 things that could have been communicated in
one paragraph. I also have an old C++ book (been sitting in my self for
decades) which assumes the reader already knows C, which is more to my
speed, but it still fails to talk about what's really happening behind the
scene so I can figure out the answers for myself to questions like the
above, and it is so old for example it seems to be pre-template support in
C++ so I need something similar but more up to date.
Any answers to the above questions and pointers to recommended books or
other material you think would best fit what I need to learn would be
greatly appreciated....
decided it's finally time to learn C++. I've got a year+ experience with
Smalltalk and about a year of experience with Java as well as experience
with Objective C, so all the OO concepts or the mechanics of C I know very
well. But what I'm having trouble figuring out is the details of what the
C++ language does for you, and what it doesn't do for you, with all the
complexities of memory management.
I'm talking a class on C++ at my local community college, which is working
well at it's intended purpose of motivating me to actually put the time in
to learn the langauge, but it's not working well at actually teaching me
anything because I'm way ahead of the class (I think I might know C and C++
better than the teacher). Though it's a second semester class listed as
"Advanced C++", it's really just "Beginning Programming in C++ - class II"
as he's still going basic concepts like pointers and what a class is.
I'm the type of guy that is not happy unless I understand all the nuts and
bolts of the language. If I don't know the langauge well enough to write a
compiler for, the I don't feel like I know it well enough to use it.
certainly don't know the C++ features well enough to do that yet.
I'm trying to understand what the C++ compiler will do for you, and not do
for you, in terms of calling constructors and destruction so you can be
sure when you write code that our not creating memory leaks, or dangling
pointers, or all the other problems that can show up. First, I thought you
had do it all yourself, like with straight C, but that would use new and
delete. But then I learned new and delete was only if you wanted to
manipulate pointers but that you could use local object variables and it
would do it for you. Then I thought object variables were actually
implemented as pointers (like Java) where C++ would do the new and delete
for you behind the scene. But then I figured out it was just straight C
struct semantics and the local objects were just being allocated on the
stack, and all C++ was was doing for you was a hidden calls to constructors
and destructors. That seemed cool. So I was thinking the trend was not to
use object pointers and new or delete unless you needed dynamic structures.
It seemed that if you just used object variables, C++ would make sure your
destructors and constructs were always correctly called.
But then I found out that when using call by value and assignment, C++
didn't take care of things for you after all. So if you have objects that
need special care, you have to supply the copy constructor and overide the
assigment operator to make sure things are done right.
So, I'm getting there. But I'm still somewhat lost becuase the books I'm
reading don't explain what the C++ compiler is doing for you behind the
scene. They show by simple examples how to write the assignment operator,
but they don't explain what the default assignment operator does for
example.
To really understand what's going on here, I need to know what's happening
behind the scene, and that's what I've not found documented anywhere yet.
So the first real question to the group, is where do find this out? Is
there a good FAQ, or web site, or books, you can recommend that will
concisely and simply give me _all_ the facts I need to know about these
sorts of things - especially one that's intended for an advanced programmer
to quickly pick up what the compiler is really doing for you (thinks like
"making a function virutal means it's added to the structure as a function
pointer and all calls the the virtual function are indirected though the
function lookup table in the object").
I did find this web page last night which was very good (but really only a
start and not all the answers I need), I'd like to see more like this on
the rest of the language...
The Anatomy of the Assignment Operator
http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
A lot of these sorts of things I figure out on my own by writing lots of
short test programs and doing a lot of reverse engineering work - but
that's very timing consuming and I was hoping someone could recommend some
good material to read.
And example of a simple specific I don't yet understand how how something
like string works (and when it won't work).
If I have a simple class:
Class Person
{
string name;
int age;
};
And using it as local automatic variables:
Person a, b;
And write something like:
a = b;
What happens? Have I just created a memory leak with the string or will
that work? I don't know how the string class is implemented (yet), so I
can't guess what it's doing. And I don't know what the C compiler does by
default for assignment like the above, so I don't even know if it's
possible for the string class to do the right thing and not create a memory
leak.
If I wrote a class like the Person example above, do I need to overload the
assignment operator to make it work correctly? And how do I write the
assignment operator to make it work?
Does the default assignment operator just do the same thing as struct
assignment in C? That is, just do a raw byte copy of the struct from b to
a in the above without any calls to constructors or destructors? And, if
it does that, is the string class implemented in some way that prevents
that type of assignment from creating a memory leak (I'd guess it would
have to implement its own memory management and do it's own garbage
collection if that was the case - which doesn't sound reasonable).
Or does it generate a default version of the assignment operator for every
class which will do a member by member assignment - which would then call
the string object assignment operator function to "do the right thing" for
the string? In which case the assignment of the Person class objects would
not create a memory leak or any other problem and it work work?
And in general, if I create a class like the Person one above and use any
object in it, will it work as long as assignment of all the members is
correctly supported by all the member objects?
And if you use use object pointers instead of objects in your class and
allocated the objects in your constructor with new, is default assignment
guaranteed not to work if the pointers have owner responsibility for the
memory pointed to becuase pointer assignment will always by default just
assign pointer A to pointer B without any help from the C++ compiler doing
calls to destructors and copy constructors or object assignment functions
for you?
And, how do I find out how string is implemented? When you do an
assignment, does it make copies of the strings, or used shared strings with
reference counts? Are they mutable? Or is it a reference count with
shared objects (like java objects), or does it us a copy on write strategy
to make assignment fast but make then act as if the strings had been copied
no assignment?
The above is typical of the type of thing I'm trying to work through
currently.
Some of this may be in the books I already have, but it's going to take
some time to get though them. The book for the class is the 2" thick
Deitel "C++ How to Program" book which is ok, but very verbose for someone
like myself which is an experienced C programmer just trying to pick up
intimate details of the C++ extensions to C. I can read an entire chapter
of that book and learn only 3 things that could have been communicated in
one paragraph. I also have an old C++ book (been sitting in my self for
decades) which assumes the reader already knows C, which is more to my
speed, but it still fails to talk about what's really happening behind the
scene so I can figure out the answers for myself to questions like the
above, and it is so old for example it seems to be pre-template support in
C++ so I need something similar but more up to date.
Any answers to the above questions and pointers to recommended books or
other material you think would best fit what I need to learn would be
greatly appreciated....