C++ problem -

Johan V. johan at localhost.localdomain
Thu Aug 26 06:09:12 PDT 2004


Björn Lindberg wrote:

> michael <michael8110 at terra.com.br> writes:
> 
> <big snip>
> 
>> but upon introducing the function getmax() ,nothing works anymore.This
>> function is part of a more complex reorder function .What really is
>> odd is  that the programs are identical except as to how they fetch
>> their  input.(Incidentally, if i run the complete programs,the read
>> from file  produces a huge list of the same big numberover and over
>> and way down,the  correct output !).I'm aware that few things can be
>> as boring as reading  someone else's code (listening to their
>> dreams,perhaps ) but i'll be very  thankful for any ideas.--
> 
> The only reason reading your code is boring to read is because it is
> horribly formatted. 

First of all I have to agree with Bjorn, my eyes are still hurting.

Still I have spotted a few errors:

Your lists consistently have an uninitialized node at the end:

from main: node * head = new node;
and after reading a node in 'fillin': temp->itsNext = new node;

In 'getmax' a node is created on the heap: node * big = new node;
only to loose the pointer to this part of the memory at the first possible
moment: big = temp;

I'm suprised anything works as expected.





> It is quite frankly impossible to read in this 
> state. If you repost your code with better indentation and formatting,
> I'll be happy to help. But I don't want to take the trouble going
> through it all reindenting and formatting it myself just to see what
> is going on.
> 
> Meanwhile, here are a couple of notes:
> 
> 1.
> 
> You write:
> 
>>    michael ~ # g++ input_from_user.cpp
>> input_from_user.cpp:61:2: warning: no newline at end of file
> 
> That is a warning from your compiler. Listen to it! Especially when
> learning, but also later, you should always use the compilation flags
> "-Wall -ansi -pedantic". Strive to silence the compiler, and take all
> warnings seriously.
> 
> 2.
> 
> Unless you are implementing your own linked list as part of the
> learning process, use std::list instead, and use as much of the
> standard library algorithms as possible. This will make your code
> shorter, simpler, clearer & most importantly less buggy. For example I
> see in your code many `new's but no `delete's, indicating to me that
> as it is you have memory leaks in your program.
> 
> 3.
> 
> Even if you are implementing your own list as part of learning, I
> would recommend you to look at std::list and mimic its semantics. Use
> references and return by copy to minimize the number of pointers in
> the code. Try to contain pointers within classes. As it is, your code
> looks very C-ish.
> 
> Oh, and use std::string instead of character arrays.
> 
> 
> Björn




More information about the lfs-chat mailing list