Re: what will happen after i use free()???



On Sun, 19 Mar 2006 13:00:22 UTC, "Vladimir S. Oka"
<novine@xxxxxxxxxxxxxxx> wrote:

Herbert Rosenau opined:

On Sun, 19 Mar 2006 11:27:39 UTC, Mark McIntyre
<markmcintyre@xxxxxxxxxxx> wrote:

On Sun, 19 Mar 2006 08:54:20 +0000 (UTC), in comp.lang.c , "Herbert
Rosenau" <os2guy@xxxxxxxxxxxxx> wrote:

(<any cast>) malloc() results in
undefined behavor, p=malloc() is the only secure call allowed in C.

Rubbish. Inserting a cast before malloc is not needed, but doesn't
result in UB.

You should program under an environment that would use for return
valuses of different types different locations and then use casting
on functions returning other than int,

Again, can you please elaborate /why/ you think this matters (physical
storage of return values)?

C Standard C makes absolutely no assumption how an implementation
should handle such things as returning values from functions. So each
implementation does it in a way best resolved by the underlying
hardware. Intel on on that aspect one of the littlest market ever seen
even as M$ means that they have the mayority.

So there are implementations around where void* or char* will occupy
56 bits while int is only 48 bits. They have special registers for
storing pinters in, other to stor floatingpoint values and another to
store int pointers, having other instructions addressing pointers
other ones addressing floating point values, other for floatingpoint
and so on.

Getting an exception on addressing a pointer in int registers creates
an exception.

A void* can not been interpreted as int - whereas C defines that any
undefined function returns int. So casting that value requires some
special mashine instructs and will fail miserably becaus the 56 bits
returned by malloc gets lost completely while an random bitpattern
gets used for converting that to a pointer.

forgetting to #include stdlib you gets undefined behavior

And why it would lead to this?

Because the differences between void* and int are significant and may
occre in something but not what you means you get.

- find then all that will cost you more
time as to write one million lines of conforming code.

Are you now proposing writing non-conforming code?

No, here we speak only about strictly conforming code.

Apart from calling people names again, you also forgot to breathe. Your
posts are increasingly resembling any of the dozens of definitions of
flaming. You risk becoming a troll...

That doesn't disturb me. Only trolls will name others who tries to
teach a troll.

Here you'll see nearly dayly an idiot who means that casting to hide
diagnostics is ok. True, many peoples coming here have learned that by
reading defektive books, broken code from idiots, so I try to help
them to get it right whenever I have free time to do so.

As this thread is running for more than a week now I get allergic to
read again and again therin casting results returning from functions
like malloc is ok.

It looks like there is a need to drum into somebody that casting is
almost the wrongest method to resolve a problem.

Use cast only if you are absolutely sure that yor really needs the
cast and after you have checked and rechecked again and again that you
really knows what you does and not on the way to lie to the compiler.
lie to the compiler and it will get its dreadful revange.

Myself had learned that more than 20 years ago even on K&RII by
falling through cede inspections, failing on getting the results I
expected before I learned how to use casts right. There was no usenet
ready to learn from others.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
.



Relevant Pages

  • Re: about the array
    ... 3- If you cast to the wrong type by accident, ... are malloc and free. ... the compiler should issue a diagnostic - gcc ... unknown set of arguments, and return an int. ...
    (comp.lang.c)
  • Re: Reading a string of unknown size
    ... "Don't cast return value of malloc() in C. ... It is entirely possible for an int to be returned by a ... function using a different method than that used to return a pointer. ...
    (comp.lang.c)
  • Re: Simple Casting Question
    ... only in the form (int to float),,, ... or just casting identical types, so I hope that I can avoid some ... None of the conversions you describe ... In most cases where a cast is used, ...
    (comp.lang.c)
  • Re: How does free() knows?
    ... Doesn't malloc() return a void ... >> implicit casting is better than explict casting? ... > An explicit cast will keep the compiler from complaining (even ... > cast only is reasonable if you either try to compile ...
    (comp.lang.c)
  • Re: How does free() knows?
    ... See the FAQ section 7.7. ... > implicit casting is better than explict casting? ... malloc() returns a void pointer and that gets automatically ... An explicit cast will keep the compiler from complaining (even ...
    (comp.lang.c)

Loading