wrote:
> I am using the Redhat version of Linux and GNU C++.
>
> It is not clear to me whether this is a Linux issue or a C++ issue.
I bet it is neither Linux nor C++; most likely a programming error.
Are you an experienced C++ user, or are you a beginner?
> I
> do not have this problem running the same program on Windows but
> tweaking the C++ code appears to fix the problem on Linux.
I spent several years working on the optimizer for C (and, implicitly, C++)
for the UNIX OS. People persisted in filing trouble reports on the optimizer
(which ran between the compiler and assembler steps in that system) because
their programs worked unoptimized (or so they said) and failed when optimized.
It turned out that the problems were in their source programs. They were
faulty. One thing to note about an optimizer is that _for correct programs_,
the optimized program should give the same results as the unoptimized ones.
And our optimizer did that. But it did not guarantee to give the same
results for incorrect programs.
One example is that for optimized programs, we did register allocation even
if the programmer did not explicitly declare a variable to go into a
register. And we overloaded registers. If a variable needed to be in a
register in one part of a function but not in the rest, and another variable
needed to be in a register in another part of a function, they could both be
assigned to the same register. Now consider what happens when the program
uses a variable, to which no value has been assigned, as a pointer. While
_not required_, a memory location is typically zero until a value is
assigned to it. Now in the case of overloaded registers, if a variable has
no value assigned to it, what the program gets is just whatever was in the
register, which is usually non-zero. So in the unoptimized case, the program
gets a null pointer (which should crash the program), where in the optimized
case, the program just uses some memory location somewhere.
This is a simple case of a programming error that runs differently on
different machines; different in this case being simply the unoptimized
machine and the optimized machine -- if you care to look at it that way.
Now in C++, the normal place to allocate memory is in the constructor
functions and the normal place to deallocate memory is in the destructor
functions. And I try to never let pointers to such memory escape from any of
the methods on an object. If you program like that, all your memory
allocations will be in the constructors and destructors, which should make
debugging easier: put tests in them to verify what is going on. And be sure
your constructors initialize everything.
>
> I have a static array that is declared privately in one class and a
> structure that is declared publicly in another class. The program uses
> both classes. I was getting core dumps and traced the problem down to
> the fact that the array shares the same memory as the structure so
> that, when an array element is written to, the structure is
> overwritten. I found that the problem appears to go away when I change
> thet declaration of the static array to public. However, I am not sure
> I understand the issues involved. In fact, I am not really clear of
> the advantages of declaring a variable as private as opposed to public,
> apart from not wanting someone to modify the variable erroneously.
> However, it appears that declaring it as private makes the program go
> ahead and erroneously change it anyway.
I normally have all variables in a class be private. Now I may have
functions in the class that return the value of a variable, but that is not
the same as making the variable public. Similarly, though less likely, I
might have a function that sets the value of a variable. But normally, that
is getting too fine: I might, in a reimplementation of the class, remove the
variable entirely. This should not affect the users of the class.
>
> I should also point out that the class where the static array is
> declared privately, has the class with the publicaly declared structure
> as one of its class objects. I have no idea why the compiled code
> would make them use the same memory.
>
> Any clarification of this/these issues would be greatly appreciated.
>
> Thanks,
> Peter.
>
--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ PGP-Key: 9A2FC99A Registered Machine 241939.
/( )\ Shrewsbury, New Jersey
http://counter.li.org
^^-^^ 07:30:01 up 10 days, 9:57, 3 users, load average: 4.41, 3.78, 3.53