unexpected result

B

Barry Schwarz

Hi geeks,
Please tell me which book gives the complete
detail of dynamic memory management in C and C++.

Memory management is a detail about the implementation of the run time
library, not about the C language. To learn how memory is managed by
your system, ask in a newsgroup that discusses your system.


Remove del for email
 
R

Richard G. Riley

jaysome said:



In 1989, I was learning C. The guy sitting next to me was learning C too. He
omitted to provide sufficient storage for a string. (He was just one byte
short.) When the program ran, he saw pretty much what he expected to see,
just general student-program output, you know the stuff - and then, at the
bottom there, it said something like:

"Do you really want to format C: (Y/N)?"

He was *very* fortunate. The result of this particular instance of undefined
behaviour appears to have been a jump into "the system" - and if it had
jumped just a bit further, he could well have had his hard drive formatted
without being asked. No, I'm not making this up.

Yes you are :) Scare mongerer you ... Sounds more like you modified
his program to make him pap his kecks ...

It was clearly a PC then? DOS?
The following year, I'd managed to find a job as a C programmer. A colleague
of mine had clearly been on the Intermediate C course and the Advanced C
course, but had unaccountably failed to turn up for the Basic C course. He
made *exactly the same error* (i.e. an array that was one byte too short
for the string being stored there), and this time it did actually trash the
machine, to the extent that it wouldn't even boot until we started feeding
diagnostic diskettes into it.

If he had done an advanced C course and an Intermediate C course then
he presumably knew about buffer issues in C. It is impossible that he
didnt or his intermediate and his advanced programs would have failed too.

Good C programmers frequently get the count wrong on array traversal :
and it doesnt cause your hard disk to get erased. All programmers are
human.

Well, in 99.99999999999999% of the cases. The old "n-1" will haunt C
programmers for years no matter good : its experience which means we
avoid it most of the time.

(Oh and did I mention a good debugger would catch it too ..)
Your "Dad" character is quite right. Leave C well alone unless you're
prepared to spend the time and effort it takes to get it right.

Alternatively, jump in there with a good book and have a lot fun
learning the language. If you worried that your hard disk would melt
for every out of control memory access you have in C then trust me, C
would be history by now.
 
M

Michael Mair

Richard said:
Yes you are :) Scare mongerer you ... Sounds more like you modified
his program to make him pap his kecks ...

That seems to be an unfair accusation.
It was clearly a PC then? DOS?


If he had done an advanced C course and an Intermediate C course then
he presumably knew about buffer issues in C. It is impossible that he
didnt or his intermediate and his advanced programs would have failed too.

Considering the people with which I had the dubious pleasure
to be at the receiving end of a job interview... Many had
excellent references and shiny documents proving their excellence
but were stumped after a couple of intermediate level questions.
When going back to basic level questions, I still found enough
gaps in their knowledge. From my point of view, passing an
advanced or better level course only means that the person should
know about such things but not that he or she actually does.
Good C programmers frequently get the count wrong on array traversal :
and it doesnt cause your hard disk to get erased. All programmers are
human.

Well, in 99.99999999999999% of the cases. The old "n-1" will haunt C
programmers for years no matter good : its experience which means we
avoid it most of the time.

(Oh and did I mention a good debugger would catch it too ..)

And electric fence or valgrind could catch even the ones you
did not expect.
Alternatively, jump in there with a good book and have a lot fun
learning the language. If you worried that your hard disk would melt
for every out of control memory access you have in C then trust me, C
would be history by now.

That is true; however, I once upon a time managed to kill
80 percent of a harddisk's contents by an off-by-one error
when writing a Copper list. And it was not much fun to
bring it back floppy disk by floppy disk (as far as possible).

I did not follow it through but essentially missing memory
protection made it possible for the computer to run off wildly
through memory unfortunately encountering only legal opcodes
until most of the harddisk sectors contained only rubbish
while I thought that I should have given the task a higher
priority and why is the harddisk LED glowing...


Cheers
Michael
 
R

Richard G. Riley

Richard G. Riley schrieb:

That seems to be an unfair accusation.

Note for the humour impaired : I included an obligatory ":)" smiley ...
Considering the people with which I had the dubious pleasure
to be at the receiving end of a job interview... Many had
excellent references and shiny documents proving their excellence
but were stumped after a couple of intermediate level questions.
When going back to basic level questions, I still found enough
gaps in their knowledge. From my point of view, passing an
advanced or better level course only means that the person should
know about such things but not that he or she actually does.

Are you seriously suggesting that someone who passes an advanced
course is not, at least, aware of bounds checking? I have no doubts
about the quality or thinking processes of a lot of C programmers. We
used to set a very simple test : write a version of strcpy. No shit :
90% couldnt do it. We wont mention writing a function to reverse a string.
That is true; however, I once upon a time managed to kill
80 percent of a harddisk's contents by an off-by-one error
when writing a Copper list. And it was not much fun to
bring it back floppy disk by floppy disk (as far as possible).

There are always risks. I was dicking around with a device driver
recently and locked my machine solid : I lost a whole batch of
bluetooth sync data that hadnt flushed through to the HD. Its never
100%. But to advise someone against learning C because of the
potential of buffer overruns is throwing the baby out with the bath water.
I did not follow it through but essentially missing memory
protection made it possible for the computer to run off wildly
through memory unfortunately encountering only legal opcodes
until most of the harddisk sectors contained only rubbish
while I thought that I should have given the task a higher
priority and why is the harddisk LED glowing...

Systems are a lot more robust now. I wiped my entire code of a
microdrive when I was writing a 68000 routine to save hiscore data : I
neglected to swap the microdrive and remove my backup assembler
source. Didnt stop me writing "save routines" though :)
 
M

Michael Mair

Richard said:
Are you seriously suggesting that someone who passes an advanced
course is not, at least, aware of bounds checking? I have no doubts
about the quality or thinking processes of a lot of C programmers. We
used to set a very simple test : write a version of strcpy. No shit :
90% couldnt do it. We wont mention writing a function to reverse a string.

No, I am just suggesting that it is, unfortunately, possible to
pass an advanced course without being aware of bounds checking
or having really learnt much. This is, of course, not only the
participants' fault and -- in some cases -- not in the least the
participants' fault.

When giving a C course at the university, I made sure that I talked
with every participant about the homework when reviewing the result
of the pairs. This way I knew what my students had understood.
This was much work for me and my students complained from time to
time about the workload and what they had to keep in mind. In the
end, I was very happy to hear that they considered my course the one
where they learned most during that semester...


-Michael
 
K

Keith Thompson

Richard G. Riley said:
jaysome said: [...]
In 1989, I was learning C. The guy sitting next to me was learning
C too. He omitted to provide sufficient storage for a string. (He
was just one byte short.) When the program ran, he saw pretty much
what he expected to see, just general student-program output, you
know the stuff - and then, at the bottom there, it said something
like:

"Do you really want to format C: (Y/N)?"

He was *very* fortunate. The result of this particular instance of
undefined behaviour appears to have been a jump into "the system" -
and if it had jumped just a bit further, he could well have had his
hard drive formatted without being asked. No, I'm not making this
up.

Yes you are :) Scare mongerer you ... Sounds more like you modified
his program to make him pap his kecks ...

You just called Richard Heathfield a liar. I suggest you either back
up your accusation or apologize.
 
R

Richard G. Riley

You just called Richard Heathfield a liar. I suggest you either back
up your accusation or apologize.

Oh please. And I also called his pint a poof.

Or I could ask him to back up his claim.

This might have escaped your notice but I included a smiley and also
suggested he doctored the code to scare his friend : it whats known in
the real world as a bit of humour.

Killfile me Keith : it'll save you a lot of effort. Save your energies
for posting that link to "how to reply using google" twenty times a
day.

FWIW, I dont killfile you because I learn things from you : you do
know your standards. I just wish I could find a way to killfile the
numerous posts about google.
 
K

Keith Thompson

Richard G. Riley said:
You just called Richard Heathfield a liar. I suggest you either back
up your accusation or apologize.
[...]
This might have escaped your notice but I included a smiley and also
suggested he doctored the code to scare his friend : it whats known in
the real world as a bit of humour.

Yeah, I saw the smiley. I read it as, "Richard, you're liar (just
kidding)." I'm not impressed.
 
R

Richard G. Riley

Richard G. Riley said:
You just called Richard Heathfield a liar. I suggest you either back
up your accusation or apologize.
[...]
This might have escaped your notice but I included a smiley and also
suggested he doctored the code to scare his friend : it whats known in
the real world as a bit of humour.

Yeah, I saw the smiley. I read it as, "Richard, you're liar (just
kidding)." I'm not impressed.

What about commenting on the advice "not to learn c"? Any thoughts on
that? Or did they not impress you either? Or are your jumping to
protect Richards honor? I suspect Richard is big and ugly enough to do
that himself.

As I once said before when you were lecturing about use of english
lanugage etc, I simply dont care. I meant the above as a joke. You dont like
it? Tough. Killfile me. As it is, I give you the benefit of the
doubt. Not that you care, and nor should you.

Also, you would notice another post on the subject which mentions my
own couple of fauxpas when programming which lead to bad dataloss. So
noone is doubting that things happen : but they are rare and if buffer
overrun was to nuke every machine it happens on then we would be in a
sorry state today. But maybe you didnt? Because according to your
constant posts about context you have a newsreader which removes all
references to previous posts.

It was a joke. Take it or leave it. Now possibly comment on the rest
of the article : your skills and knowledge would be more benficially
used in that respect.
 
M

Mark McIntyre

Memory management is a detail about the implementation of the run time
library, not about the C language. To learn how memory is managed by
your system, ask in a newsgroup that discusses your system.

Its possible that the OP refers to the use of malloc etc

To the OP: pretty much any C book will tell you how to use malloc etc.
There's almosts certainly no book that will give you "complete
detail". The CLC FAQ has some book suggestions.
Mark McIntyre
 
M

Mark McIntyre

Yes you are :) Scare mongerer you ...

FWIW I've seen something similar. On a Vax, falling off the end of
main can cause the system to provide very scary error messages, such
as advising you its unloading a tape, or rebooting the cluster.
If he had done an advanced C course and an Intermediate C course then
he presumably knew about buffer issues in C. It is impossible that he
didnt or his intermediate and his advanced programs would have failed too.

I once hired someone who claimed to be a C programmer, and who had a
qualification in it. His first programme failed dismally after he used
strcpy with unallocated pointers. Upon getting this explained to him,
he went on to screw up even more magnificently with other 'obvious'
stuff.
Good C programmers frequently get the count wrong on array traversal :

No, good programmers *sometimes* get the count wrong. Programmers who
*think* they're good, get it wrong more frequently of course.
and it doesnt cause your hard disk to get erased.

Not always, no. Your OS and build env will try to protect you. But
you won't always be using a friendly OS, or tools that trap errors, or
code review proceses.
(Oh and did I mention a good debugger would catch it too ..)

No, a debugger won't catch this. A good one will however trap the
fatal error that arises by noticing when your OS signals it.

What will actually catch this is a proper tool to check array bounds
and the like.
Mark McIntyre
 
M

Mark McIntyre

Are you seriously suggesting that someone who passes an advanced
course is not, at least, aware of bounds checking?

Yes. Consider the massive incidence of teachers who think main returns
void, or that malloc must be cast.
I have no doubts about the quality or thinking processes of a lot of C programmers.

Nor I. I do however have significant doubts about much of the
teaching.

Mark McIntyre
 
M

Mark McIntyre

Killfile me Keith : it'll save you a lot of effort. Save your energies
for posting that link to "how to reply using google" twenty times a
day.

Alternatively, you could learn some manners and computing skills.
FWIW, I dont killfile you because I learn things from you : you do
know your standards. I just wish I could find a way to killfile the
numerous posts about google.

As part 2 above.
Mark McIntyre
 
R

Richard Heathfield

Richard G. Riley said:
Oh please. And I also called his pint a poof.

Or I could ask him to back up his claim.

I refer you to an article I wrote in comp.lang.c on Monday 26 April 1999, in
a thread entitled "Dos Command", in which I recounted both the incidents in
question. Whilst this does not prove that the claim is true (and I don't
quite see how I'm expected to prove that), it certainly indicates that I
didn't make it up on the spur of the moment to support my argument in this
thread - unless you'd rather believe I am capable of hacking Google's
archives to insert a brand new article and yet make it appear as if it were
posted almost seven years ago.

Keith is right. You have a lot to learn about Usenet.
This might have escaped your notice but I included a smiley and also
suggested he doctored the code to scare his friend :

So you called me a liar, then you smiled about it, and then you accused me
of malicious damage.
it whats known in the real world as a bit of humour.

I don't quite see what anyone in the real world would find amusing about
being called a liar and a vandal.
 
R

Richard G. Riley

Richard G. Riley said:


I refer you to an article I wrote in comp.lang.c on Monday 26 April 1999, in
a thread entitled "Dos Command", in which I recounted both the incidents in
question. Whilst this does not prove that the claim is true (and I don't
quite see how I'm expected to prove that), it certainly indicates
that I

You are not. And I never asked you to.
didn't make it up on the spur of the moment to support my argument in this
thread - unless you'd rather believe I am capable of hacking Google's
archives to insert a brand new article and yet make it appear as if it were
posted almost seven years ago.

What are you talking about? I think you need to come down off your
high horse to be quite honest.
Keith is right. You have a lot to learn about Usenet.

And you have a lot to learn too. Especially about making asumptions on
google pseudonyms.
So you called me a liar, then you smiled about it, and then you accused me
of malicious damage.

Whatever. I give up.
I don't quite see what anyone in the real world would find amusing about
being called a liar and a vandal.

I bet you dont find racist or sexist jokes funny eh? Do you get all
red in the face and demand them to take it back?

Have you anything to say on the crux of the matter? That you were
talking rubbish when telling this guy not to program C? Or that
someone who passed an advanced course didnt know about buffer
overruns? Would you not agree that ALL C programmers get buffer runs
occasionally? That most C programs in the world have such bugs : none
of which regularly format hard drives or fry processors?

Are you aware that other languages allow similar damage?

Remember the Mattoy Dragon? You could fry it with a poke?
The camputers lynx : you could fry the video ram with multi-buffer bus
contention.

You have refused to take the comment in the spirit it was intended :
and if "learning about usenet" is learning how to pamper to self
important guys like yourself then good luck.
 
M

Michael Mair

Richard said:
You are not. And I never asked you to.

What are you trying to say?
"It does not?"
"You don't exist?"
....
What are you talking about? I think you need to come down off your
high horse to be quite honest.

It seems to me that you are construing things a little bit
hastily.

And you have a lot to learn too. Especially about making asumptions on
google pseudonyms.

Please quote what you are referring to.

Whatever. I give up.

I suggest you reread what you write before posting it off;
somehow, several others seem to misread it. For me,
interspersing an insult with a smiley does not exactly make
it better -- there are better means to convey a humorous
remark on usenet.
I bet you dont find racist or sexist jokes funny eh? Do you get all
red in the face and demand them to take it back?

For me, it depends on the context whether I find them
tolerable, intolerable or funny. The latter usually only
if they are directed at the one telling the joke. And I
have indeed a liking for some kinds of non-pc jokes.
Somehow I doubt that it is the others who need a humour fluffer
or whatever to understand when and how you are funny.

You have refused to take the comment in the spirit it was intended :
and if "learning about usenet" is learning how to pamper to self
important guys like yourself then good luck.

If you provide more hints to the "spirit it was intended",
then you may get better responses.
Funnily, the estimated degree of self-importance for you
by far outweighs the one I'd assign to Richard Heathfield
or Keith Thompson.
In fact, you sound like something crawled up your lower
intestines and died.

-Michael
 
R

Richard G. Riley

What are you trying to say?
"It does not?"
"You don't exist?"
...

He asked how he was supposed to "prove" it. I told him he didnt have
to and I never asked him to. And dont want him to. It is irrelvant to
the argument in hand. Really.
It seems to me that you are construing things a little bit
hastily.

Not really. This is getting plain silly : again. Richard has got all
on his horse because I cracked a joke. If it caused offence then I
apologise to those offended : to the rest ... :-;
Please quote what you are referring to.

He earlier mentioned his terms of service in this NG. As if it earns
you stripes.
I suggest you reread what you write before posting it off;
somehow, several others seem to misread it. For me,
interspersing an insult with a smiley does not exactly make
it better -- there are better means to convey a humorous
remark on usenet.

For you. For Keith. For Richard. For some others. Sorry. I'm
devastated. No really.

If you take a step back and pay attention to the real content of the
post you would see that it was attempting to be constructive and
pointing out that C does not wreck systems with every overrun.

And to suggest it does it ridiculous. Note : in no way am I condoning
overruns. There : I covered my ass.
For me, it depends on the context whether I find them
tolerable, intolerable or funny. The latter usually only
if they are directed at the one telling the joke. And I
have indeed a liking for some kinds of non-pc jokes.
Somehow I doubt that it is the others who need a humour fluffer
or whatever to understand when and how you are funny.

See : humour is easy. I laughed at that.
If you provide more hints to the "spirit it was intended",
then you may get better responses.
Funnily, the estimated degree of self-importance for you
by far outweighs the one I'd assign to Richard Heathfield
or Keith Thompson.

Thats a matter of opinion : and you are entitled to yours. I realise
you come from the same school thought as Keith and Richard so fair
play to you. I would humbly point out that I tend to try and help when
I can in a plain and constructive manner : and I certainly dont post
repetitive net nanny posts every 3 seconds or tell people that
"globals" dont exist in C. Are some of you really real world
programmers? My killfile has 8 people in. Some of them even quote the
other in their signatures : it is a clique - nothing unique to this
group. The "etc." is a syntax error comment still makes me laugh.
In fact, you sound like something crawled up your lower
intestines and died.

I dont think you have paid any attention to what I'm saying on the "On
Subject" part of this thread. But its interesting how when the going
gets tough to see who really starts throwing the insults about. But,
of course, if you didnt mean it : add a smiley - sometimes it works.

C is a language : it can be made approachable by posters here. It can
also be made to seem like a dangerous monster. Some do a very good job
of the later.
 
A

AnonMail2005

test said:
hi
look at this code

include <stdio.h>
int main(void)
{
int i,j=2;
i=j++ * ++j * j++;
printf("%d %d",i,j);
return 0;
}

acc. to me the values of i & j are 27,5 respectively & rightly so as i


ran this on turbo c++ compiler but if i ran this on lcc-win32 compiler


i got 32 & 5 for i & j respectively.
why this is so
 
R

Richard Bos

jaysome said:
I'd believe you if you could give me an example of a compiler that does
nothing at all, or formats my hard drive, or melts my monitor, or makes
demons fly out of my nose, when presented with the following code:

int main(void)
{
int i = 0;
i = i++;
return 0;
}

You do not understand. The simplistic examples which every high school
boy could understand are not the problem. The really complex expressions
involving pointers, arrays, and several side effects in several places,
_those_ are the real problem. The compiler must be able to optimise a
correctly written expression, even a complex one. If the price is that
we are not allowed to write an assignment, even a simple one, which is
dubious in the first place, then writing sane code is a small price to
pay for efficiency. Small price? It's no price at all - it's a bonus. I
really do not want to have to debug code which contains the above and
have to guess whether the bozo who wrote it meant i++; i=i; or i=i; i++;
in the first place.
Some people have bewailed this, and claimed that there should be a rule
which allows "obviously" well-meant undefined behaviour (usually the
kinds they themselves write), while disallowing "obviously" perverse UB
(always the kinds they can't wrap their head around) as strictily as it
is now. There's only one answer to that: fine, _write it then_. And make
sure it is as clear as the current Standard. Oddly enough, one rarely
hears anything of their new rules...

Richard
 
R

Richard Bos

Richard G. Riley said:
Oh please. And I also called his pint a poof.

Or I could ask him to back up his claim.

This might have escaped your notice but I included a smiley and also
suggested he doctored the code to scare his friend : it whats known in
the real world as a bit of humour.

Actually, I gather that in Mr. Heathfield's part of the real world it's
what's known as an insufficient defense in a slander case. You're lucky
that he's not the kind to take legal umbrage easily.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top