On Sun, 6 Apr 2003 10:09:41 -0500 "Steven T. Hatton" <hattons@globalsymmetry.com> wrote:
That is a variable. What about anonymous objects? I believe such a concept is meaningful in C++, e.g., the elements of a linked list. If I start removing elements form such a list without explicitly destroying them, I could end up with object occupying memory, yet having no reference to them withing my program. Is this not true? This is what is called a memory leak.
There are a few things I'm not sure of regarding classes. With time, and further reading, I suspect these will become clear, but it probably doesn't hurt to mention them.
As I understand things, the class definion itself is in a favored position to go on the stack rather than the heap. This could mean different things depending on the details. I assume the static members would only appear once in the run-time image. If we say a method gets put on the stack, what we really mean is it's local variables and return pointer are placed on the stack. Methods defined within the body of the class definions are default /inline/ meaning they 'should' go on the stack. A class definition does not take up any memory, just as a structure definition does not. It is the instance of a class that allocates memory. If you allocate a class as an automatic variable, that instance will be allocated in the stack. If you use the new operator, it will be allocated on the heap. remember, that all objects allocated on the stack are implicitly destroyed when the function returns to the caller.
My understanding is that member variables cannot be initialized within the class definiton. Functions can, however, be completely defined within the class definition. And here again, is a point of some confusion. Experimentation indicates to me that classes *are* initialized by their default constructor when declared in the body of the class declaration. So
class MyClass { private: MyMember mm; int myInt; }
If MyMember has an initializer in its constructor, its variables can be initialized by the implicit invocation of the constructor resulting from the declaration. myInt, however will not be initialized, and cannot be initialized at the point shown above.
Part of what I'm trying to get a handle on is the way a class instance and it's members will be represented in memory. It seems possible, if not likely, some parts may go on the stack while other perts go in the heap. In particular, it seems member variables might go on the heap. The original question that started me down this road has to do with the actual size of a pointer to an object of a given type, and also the amount by which such a pointer will be incremented with, for example ptr++.
Are these notions about pointer size and pointer incrementation easy to explain? non-static and non-constant class members are not initialized in the class, but are normally initialized by the constructor for that class, but they can be initialized with a member initialization list. Using above class: inline MyClass::MyClass():myInt(0) {}
WRT: Pointers: int i[10]; int *p; To use the pointer, p to step through the array: for(p = i;p < &i[10]; p++) { *p = <some expression>; } Note that &i[10] is a constant expression and will be evaluated at compile time. The expression, p++, is a postincrement of p, and on a system with a 32 bit int, the address contained in p will be incremented by 4. However, when you use a modern compiler, subscripts are more efficient than pointers because the compiler can more easily figure out what you are doing and generate some more efficient code. The bottom line, is that when you are doing pointer arithmetic, the size of the object that pointer points to is taken into account: So, as above, p + 1 effectively adds 4 to p. -- -- Gerald Feldman <gfeldman@attbi.com> Boston Computer Solutions and Consulting ICQ#156300 PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9