Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Object/variable scope problems (Linux and Win comparison)

Reply
Thread Tools

Object/variable scope problems (Linux and Win comparison)

 
 
itaj sherman
Guest
Posts: n/a
 
      02-28-2011
On Feb 28, 8:47*pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 28/02/2011 18:44, jacob navia wrote:
>


>
> Another adjective for you:
>
> 4) shoddy
>


Even if you had something sensible to say, it's lost in your
overwhelming waves of bad manners.

itaj
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      02-28-2011
Le 28/02/11 18:35, SG a écrit :
>> There is no point in deleting anything since it is the main() function
>> and the OS will cleanup stuff anyway.

>
> *sigh*
> No, it's not restricted to the main function (see above). And it comes
> with mo surprize that anything one writes here is subject to
> nitpicking. I probably should have added a "generally" in there
> somewhere... If it makes you feel any better, I have no problem with
> you allocating memory once in a main function and not explicitly
> releasing it or any other popular singleton implementation strategy
> which does not destruct the object.
>


It is not nitpicking. It is an interesting strategy: always allocate
and never free, that results in interesting discussions (see the answer
of Mr Johnston).

jacob

P.S. I think that outside a conscious memory allocation strategy you
are obviously RIGHT.
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      02-28-2011
Le 28/02/11 19:59, itaj sherman a écrit :
> On Feb 28, 8:47 pm, Leigh Johnston<(E-Mail Removed)> wrote:
>> On 28/02/2011 18:44, jacob navia wrote:
>>

>
>>
>> Another adjective for you:
>>
>> 4) shoddy
>>

>
> Even if you had something sensible to say, it's lost in your
> overwhelming waves of bad manners.
>
> itaj


Good point. Let's try to discuss in a civilized manner, without
just falling into a childish attitude of calling people names...

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-28-2011

"jacob navia" <(E-Mail Removed)> wrote in message
news:ikgr4c$ig1$(E-Mail Removed)...
> Le 28/02/11 18:31, Leigh Johnston a écrit :
>
>> One should get into the habit of always deleting what you new to avoid
>> memory leaks. Programs which allocate everything up front and require no
>> deallocations until program termination are few and far between;

>
> I mentioned two examples: a compiler and other command line utilities.
>
> In general all transient programs (i.e. those designed to do one task
> and exit immediately) can benefit from this strategy.
>
>> your
>> eschewing of deletes mostly smells of premature optimization which would
>> result in code that is a nightmare to maintain as you would have to
>> constantly worry if a particular allocation is a one-off or if leaked
>> would result in unacceptable increased memory usage during program run.
>>

>
> If your allocation concerns an important chunk of memory yes, you
> should delete it if you recycle objects. In many cases however
> (and in the case at hand in the example that started this discussion)
> there is no need to worry about it.
>
>> Perhaps you would prefer a garbage collected language such as Java?
>>

>
> My compiler system (lcc-win) offers in its standard distribution a
> garbage collector. It is used also in C++ programs. But obviously
> you think (as all those "language patriots" that C++is the best
> language on the world, and that Java is a kind of atrocious horror
> that will scare me (and others) if you just mention it.
>
> > Perhaps you would prefer a garbage collected language such as Java?
> >

>
> Perhaps I would prefer that C++ people would acknowledge this
> huge HOLE in their (so complicated) language and plug it some time...
>
> But what, learning from other languages and using the good features they
> have?
>
> NEVER. C++ is the best and there is nothing outside, only BAD languages.
>
>> Here are three adjectives which *still* describe you as a *C++*
>> programmer:
>>
>> 1) cowboy
>> 2) sloppy
>> 3) slapdash
>>

>
> Yes, that is your opinion. Who cares about it? Surely not me.
>
> I know this kind of people that pontificate in newsgroups, distributing
> "good" or "bad" points without proposing any arguments really.
>
> Why would the absence of "delete" when the program is exiting be "bad"?
>
> And this strategy is very easy to maintain: Just plug in a garbage
> collector and you are done.
>
> jacob
>

Well put mate, you certainly put him in his proper place *thumbs up*

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-28-2011
Le 28/02/11 19:47, Leigh Johnston a écrit :
>
> Another adjective for you:
>
> 4) shoddy
>
> /Leigh


Incredible deep argument Mr Johnston. It has really a lot of well
thought principles in it.



But maybe can you explain to a "shoddy" "cowboy" "sloppy" programmer
why a program should waste time and resources cleaning up objects
that are of no importance since it is going to exit anyway?

I make the distinction between objects that NEED recycling (big objects
for instance, that if maintained beyond their useful lifetime would
provoke an out of memory condition), and small objects that could
never provoke anything bad.

This distinctions are not just empty words. A C++ program can spend
a LOT of time cleaning up objects without any useful reason since
it is done at program exit...

That is my point.

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-28-2011
Le 28/02/11 20:22, Leigh Johnston a écrit :
>
> The point is that you are a shoddy C++ programmer if you genuinely
> believe this crap you are posting. Stick to Java and its GC mate; C++ is
> not for you.
>



Well Mister, apparently you can't discuss in a civilized manner without
calling people names.

And yes, if a customer pays me to program in Java I will program in
Java without thinking that I am doing something awful, the same as in
C++. Last september I had a contract to program in Visual Basic and
Access data base. And I did it in Visual Basic and Access.

Today I am programming in objective c and an iPhone. It is a very
small language but is great. And there this strategy doesn't work
very well, you have to release each piece of memory/resources you use.

Applications never exit really in most cases, so a strategy like I am
proposing here would be catastrophic. So I do not use it.

Am I a "shoddy" programmer? (and all other "adjectives" you use)?

For you, yes.

But what *I* think is important is the opinion *my customers*
have of me. Your opinion doesn't really count.

So you can go on calling me with any adjectives you may want to use.



Yours sincerely

jacob


 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      02-28-2011
On Feb 28, 2:36*pm, jacob navia <(E-Mail Removed)> wrote:
> Le 28/02/11 20:22, Leigh Johnston a crit :
>
>
>
> > The point is that you are a shoddy C++ programmer if you genuinely
> > believe this crap you are posting. Stick to Java and its GC mate; C++ is
> > not for you.

>
> Well Mister, apparently you can't discuss in a civilized manner without
> calling people names.
>
> And yes, if a customer pays me to program in Java I will program in
> Java without thinking that I am doing something awful, the same as in
> C++. Last september I had a contract to program in Visual Basic and
> Access data base. And I did it in Visual Basic and Access.
>
> Today I am programming in objective c and an iPhone. It is a very
> small language but is great. And there this strategy doesn't work
> very well, you have to release each piece of memory/resources you use.
>
> Applications never exit really in most cases, so a strategy like I am
> proposing here would be catastrophic. So I do not use it.
>
> Am I a "shoddy" programmer? (and all other "adjectives" you use)?
>
> For you, yes.
>
> But what *I* think is important is the opinion *my customers*
> have of me. Your opinion doesn't really count.
>
> So you can go on calling me with any adjectives you may want to use.
>
>
>
> Yours sincerely
>
> jacob


Sorry I didn't want to raise this confusion.
FYI I have never programmed in Java, for my job I often use pure C,
Perl and shell scripts, sometime python. Object programming is a thing
I use very seldom and therefore I have problems even in the
application's project phase.
Anyway I found the stupid bug: simply I called the constructor of
LoggerClass with an uninitialized variable. This was causing the
misbehaviour.
Sorry if I don't seem serious Leigh, but I do agree with Jacob Navia
(on the last part of his 1st reply)

>There is no point in deleting anything since it is the main() function
>and the OS will cleanup stuff anyway.


I have no problems related to memory limit. As the unix part will
contain just 1 father and 1 or 2 children, a wait/waitpid are enough
to ensure a proper cleanup of the heap when main exits.
If you don't agree, please argue and demonstrate I am not right, but
please in a civil manner.

Mark.
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      03-01-2011
On Feb 28, 10:56*am, jacob navia <(E-Mail Removed)> wrote:
> Le 28/02/11 18:31, Leigh Johnston a crit :
> > Perhaps you would prefer a garbage collected language such as Java?

>
> My compiler system (lcc-win) offers in *its standard distribution a
> garbage collector. It is used also in C++ programs. But obviously
> you think (as all those "language patriots" that C++is the best
> language on the world, and that Java is a kind of atrocious horror
> that will scare me (and others) if you just mention it.
>
> *> Perhaps you would prefer a garbage collected language such as Java?
> *>
>
> Perhaps I would prefer that C++ people would acknowledge this
> huge HOLE in their (so complicated) language and plug it some time...
>
> But what, learning from other languages and using the good features they
> have?
>
> NEVER. C++ is the best and there is nothing outside, only BAD languages.


Reasonable people will not argue that garbage collected systems are
purely inferior to manual memory management systems. Having said that,
it is rather difficult to mix implicit destructor calls and garbage
collection. It is not a simple problem with simple answers.

C++ has largely the same design goals as C, which is to be a portable
assembly language, and to be cost competitive with assembly. Garbage
collection simply does not fit this design paradigm of C++, especially
when it's largely incompatible with implicit destructor calls - the
heart of C++.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-01-2011
SG <(E-Mail Removed)> wrote:
> On 28 Feb., 13:39, Mark wrote:
>> [...]
>> The implementation is very similar both in Win and *NIX.
>> CControlClass incorporates CLoggerClass this way.
>> *LoggerObj is declared as private member of CControlClass.
>> (CLoggerClass *LoggerObj)
>>
>> CControlClass::CControlClass()
>> { readConfFile("somefile.conf"); }
>>
>> int CControlClass::readConfFile(const char * somefile){
>> //do some parsing
>> LoggerObj=new CLoggerClass("some_log_file.log");
>> LoggerObj->writeLog(some_const_int,some_char_array_or_string stream)
>> }
>>
>> int CControlClass::functionOne()
>> {
>> //something
>> LoggerObj->writeLog(...,...)}
>>
>> //main.cpp
>> int main(int argc, char** argv)
>> {
>> *CControlClass *ControlObj=new CControlClass();
>> ControlObj->functionOne();
>> }

>
> btw, this looks like a Java/C# programmer is trying to import his
> programming style into C++. You know that there is no garbage
> collection in C++ and that you should delete the things you created
> with new, right?


In fact, why is he using 'new' at all? There's no need, and 'new' is
only making the program less efficient than instantiating the objects
on the stack.

There's a reason C++ uses RAII. That fact should always be used to
one's advantage.

 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      03-01-2011
On 1 Mrz., 08:46, Juha Nieminen wrote:
> SG <(E-Mail Removed)> wrote:
> > [...]
> > btw, this looks like a Java/C# programmer is trying to import his
> > programming style into C++. You know that there is no garbage
> > collection in C++ and that you should delete the things you created
> > with new, right?

>
> * In fact, why is he using 'new' at all? There's no need, and 'new' is
> only making the program less efficient than instantiating the objects
> on the stack.


Good point! I somehow forgot to mention this.

Cheers!
SG
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet to run on Win 98, Win ME, Win XP, Win Vista & Win 7 ?? Krist Java 6 05-06-2010 11:53 PM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments