Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc() and implicit cast

Reply
Thread Tools

malloc() and implicit cast

 
 
Peter Nilsson
Guest
Posts: n/a
 
      06-04-2010
On Jun 4, 3:36*am, sandeep <(E-Mail Removed)> wrote:
[Subject: malloc() and implicit cast]

Other responses aside, there is no such thing as an implicit
cast. Casts are syntactical constructs, specifically, explicit
conversions. It is the _conversion_ that is implicit when
no cast is applied to the return result of malloc.

--
Peter
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-03, sandeep <(E-Mail Removed)> wrote:
> malloc(size_t n) returns a void pointer and here in my program, I am
> assigning malloc returned pointers to 2 different types and I am not
> getting any warnings about <implicit cast>.


> It has something to do with C90 ?


Yes, it's a standard feature of C. Why should you get any warnings? The
entire *point* of having malloc return (void *) is that you get no warnings
when converting it to other pointer types.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-03, sandeep <(E-Mail Removed)> wrote:
> It's interesting, some of the ones you label as OK are not OK on my
> compiler but none of them generate any warning.


Huh?

> #include<stdio.h>
> #include<stdlib.h>
> main()
> {
> char* c;
> void* v;
> int* i=malloc(4);
> *i=257; v=i; c=v;
> printf("%hhd\n",*c);
> c=malloc(1);
> *c=7; v=c; i=v;
> printf("%d\n",*i);
> }


> It produces
> 1
> 7


> So the "int to char via void" fails but "char to int via void" succeeds.


This isn't even close to correct. No one EVER said, or suggested, or even
hinted, that a program like this would work.

All Keith said is that you can convert *POINTERS*. You are then running
off without ANY evidence whatsoever to the conclusion that somehow this will
magically convert the things pointed-to. It doesn't. If you access something
through a pointer of a different type, it is usually undefined behavior.

As a special exception, when you use unsigned char as the type to iterate
over something's bytes, you're guaranteed to see valid values which are in
some way a representation, but there's not really any general guarantee that
they'll make any sense.

I'm really pretty much sold on the theory that you're just trolling, though,
because you had to work pretty hard to come up with an example this bad.
No one at the level of expertise you're playing at would have known to
use 257, and "hhd" is a beautiful example of something which is funny to
people who remember joke proposals for "short short" as a counterpoint to
"long long", but is otherwise completely implausible and inappropriate.

Nice work, though. Very well constructed, looks almost like newbie code,
and the switcheroo in the layers of abstraction was done without any mention
or reference, making it easy to think you were just hopelessly confused.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-04-2010
On 3 June, 22:47, (E-Mail Removed) (Kenny McCormack) wrote:

> A major part of becoming a man is realizing who (and whose requests) can
> and should be ignored.


that's you, spinny, isn't it?
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-04-2010
On 3 June, 22:44, Eric Sosman <(E-Mail Removed)> wrote:

<snip>

> * * *Sandeep, you have already been asked not to quote signatures.
> In fact, the post to which you made this reply contained just such
> a request: "Please don't quote signatures," it said. *And yet, you
> keep on quoting signatures. *Is your reading comprehension a bit
> below par, or are you just trying to annoy people?


the "signature" is the text at the end following the marker "-- "


> --
> Eric Sosman
> (E-Mail Removed)


that's a signature or sig
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-04, Nick Keighley <(E-Mail Removed)> wrote:
> On 3 June, 22:47, (E-Mail Removed) (Kenny McCormack) wrote:
>> A major part of becoming a man is realizing who (and whose requests) can
>> and should be ignored.


> that's you, spinny, isn't it?


So far as I can tell, they're two different people obsessed with personal
status and how other people perceive them to the exclusion of any
personal development in terms of actual accomplishments or skills.

But I can see how you'd wonder. Kenny spells better, for the most part.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-04-2010
On 4 June, 09:03, Seebs <(E-Mail Removed)> wrote:
> On 2010-06-04, Nick Keighley <(E-Mail Removed)> wrote:
> > On 3 June, 22:47, (E-Mail Removed) (Kenny McCormack) wrote:


> >> A major part of becoming a man is realizing who (and whose requests) can
> >> and should be ignored.

>
> > that's you, spinny, isn't it?

>
> So far as I can tell, they're two different people obsessed with personal
> status and how other people perceive them to the exclusion of any
> personal development in terms of actual accomplishments or skills.
>
> But I can see how you'd wonder. *Kenny spells better, for the most part..


kenny's only caught the "to be a real man..." stuff recently

doesn't any of this apply to girls?

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-04, Nick Keighley <(E-Mail Removed)> wrote:
> kenny's only caught the "to be a real man..." stuff recently


Yeah. I think he's imprinting on spinny.

> doesn't any of this apply to girls?


Only as an attempt to impress them by being stupid.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
sandeep
Guest
Posts: n/a
 
      06-05-2010
Richard Heathfield writes:
>> printf("%hhd\n",*c);

>
> %hhd? Short short int, perhaps?


?

The man page for printf on my system recommends hh modifier to print
integer value of a character. If you use %c then this will display it as
an ASCII character.

>> c=malloc(1);

>
> For one thing, the memory to which c points may not
> be correctly aligned for an int.


I thought pointers returned by malloc would be aligned for anything.

> The program fails because it abuses void *. Like any powerful tool, void
> * can be abused. Don't be surprised if abusing it produces surprising
> results.
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line vacant - apply within


I still don't see why C lets you compile code correctly that will execute
incorrectly...
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-05-2010
On 2010-06-05, sandeep <(E-Mail Removed)> wrote:
> The man page for printf on my system recommends hh modifier to print
> integer value of a character. If you use %c then this will display it as
> an ASCII character.


What system are you using? I have never seen any system even suggest the
existence of an 'h' modifier in printf. There is one for scanf, but even
there, there's nothing suggesting that you could ever use more than one
of them.

> I thought pointers returned by malloc would be aligned for anything.


If you allocate a single byte of memory, and try to use it for something
larger than one byte, not so much.

> I still don't see why C lets you compile code correctly that will execute
> incorrectly...


Then may I humbly suggest that you stop wasting your time and everyone
else's trying to use C, if you're unwilling to learn it, or listen to people
when they answer your questions the first five or six times?

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
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
implicit cast in templated function hweekuan@yahoo.com C++ 2 11-26-2008 04:59 PM
malloc() and implicit cast arnuld C Programming 7 04-16-2008 07:52 PM
Avoid implicit cast from enum to double Flo C++ 2 07-18-2006 01:27 PM
DynPtr: A pointer with implicit dynamic cast Calum Grant C++ 2 12-07-2005 04:54 AM
Implicit cast to object that implements overloaded operator pnsteiner@gmail.com C++ 7 10-12-2005 02:08 PM



Advertisments