On Mon, 31 Mar 2003, Jerry Feldman wrote:
Remember that tmp is an automatic variable. When you exit the HelloClass constructor, the storage for tmp is released and may (or let's say will be) used in another function. Automatic variables are fundamental to C and C++. Any variable you define within the scope of a function that does not have the static keyword is an automatic variable, and has block scope. Ok, now I see what's going on. My pointing to temp with memberIntPtr doesn't place a 'handle' on so when temp is gone, that memory is up for grabs, and I end up pointing at someone elses data. The reason I did that is because I couldn't figure out how to directly point to the same value.
I guess, from what you're saying, that isn't something I should be doing in the first place.
Pointers are very simple, they are simply data types that hold a memory address.
char *s = "abc"; s will contain the address of a 4 byte chunk of memory containing 'a' 'b' 'c' '\0'
The part about pointing to 'a' is a helpful refresher. I guess I already knew it was pointing to the beginning of the 'string', but I wasn't thinking in terms of the data it was actually referring to. It makes it more real to think about it actually being 'a'.
string *str = new string("abc"); Similarly, str will contain the address of a string class that you have allocated with the new operator.
Note that "string" is a C++ class, and has overhead, such as constructors, destructors, manipulation functions, and several variables as well as a pointer to the actual data.
I'll keep that in mind. The same is true in Java, but Java programmers don't worry about efficiency. That's someone elses job, and sun Microsystems likes it that way.
Let's get back to pointers. In the above example, str points to a string class. Now, let's change it a bit. str = new string("def");
In this case, we have replaced the existing pointer value with a new one, thus causing a memory leak. That is because we did not delete the space for the old value. Unlike Java, neither C nor C++ will automatically reclaim unused allocated memory. Java has a builtin garbage collection system. So, as a C or C++ programmer, you need to make sure that memory you allocate is reclaimed.
A bit of (X)Emacs and Lisp heritage there in the Java GC. I'm under the impression that QT has strings that clean up after themselves, but that may not be the silver bullet it sounds like.
Within a class, that may be done in a destructor or in your case, HelloClass::setMessagePtr should check this->messagePtr to see if it already contains a pointer.
This is part of the reason I'm playing around with this. It's the part of C/C++ that I always avoided, and it's why Java was some much easier for me to learn.
Also, you do not need to use this-> in a class member function, it is assumed.
Ya, I know, it bugs a lot of peple, but I do that in Java too. It helps me understand my code. It also primes the code completion magic in JBuilder. The same works for KDevelop, but KDevelop's code completion is kilometers behind JBuilder.
I hope that the above information has been useful.
Indeed. I guess the most important thing I got out of it is that pointers don't control the memory they point to. I guess I knew that at one point, I just got used to Java's way of keeping track of references. The same can be done in C++, but we have to do it ourselves. If I want to keep the data in a variable from getting zapped, I have to keep the variable alive(in scope, or static). Is that pretty much a correct statement? BTW, I'm using pine for this message, which I rarely do. If it is a bit strangely formatted, that will explain it. STH