PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > malloc() and implicit cast
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
malloc() and implicit cast

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 15h29   #1
arnuld
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut malloc() and implicit cast

I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html


FAQ discusses a special case when programmer has forgotten to do
#include <stdlib.h>. I am including this header and I am not doing any
explicit cast:



#include <stdlib.h>


enum ARRSIZE { MAXSIZE = 100 };


struct dummy
{
int i;
};


int main( void )
{

char *pc;
struct dummy *ptrDummy;

pc = malloc( MAXSIZE );
ptrDummy=malloc(sizeof(struct dummy));

return 0;
}

============ OUTPUT ============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
/home/arnuld/programs/C $



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 ?




--
http://lispmachine.wordpress.com/


  Réponse avec citation
Vieux 15/04/2008, 15h32   #2
Stephen Sprunk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

"arnuld" <ullu@kullu.com> wrote in message
news:pan.2008.04.15.14.28.54.687100@kullu.com...
>I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html
>
> FAQ discusses a special case when programmer has forgotten to do
> #include <stdlib.h>. I am including this header and I am not doing any
> explicit cast:


There is no such thing as an "explicit cast". There are implicit and
explicit conversions; the latter uses a cast, and the former does not.

> #include <stdlib.h>

....
> char *pc;
> struct dummy *ptrDummy;
>
> pc = malloc( MAXSIZE );
> ptrDummy=malloc(sizeof(struct dummy));

....
> 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>.


There is no such thing as an "implicit cast". There are implicit and
explicit conversions; the latter uses a cast, and the former does not.

Second, a warning is only expected when you _don't_ include the proper
header and you _don't_ use a cast. Since you're including the proper
header, there is no reason for a warning.

What's the problem?

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

  Réponse avec citation
Vieux 15/04/2008, 16h13   #3
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

arnuld wrote:
> I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html


OK.

> FAQ discusses a special case when programmer has forgotten to do
> #include <stdlib.h>. I am including this header and I am not doing any
> explicit cast:


Yes, that how it should be, if you really read the FAQ: include the header,
don't use the cast.

> 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>.


Of course, you don't. In C language 'void*' pointers are implicitly convertible
to and from other pointer types. What warnings did you expect and why?

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
Vieux 15/04/2008, 16h22   #4
Morris Dovey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

arnuld wrote:

I don't understand. Do you think there /should/ be a problem?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
  Réponse avec citation
Vieux 16/04/2008, 03h00   #5
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

On Tue, 15 Apr 2008 08:13:43 -0700, Andrey Tarasevich
<andreytarasevich@hotmail.com> wrote in comp.lang.c:

> arnuld wrote:
> > I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html

>
> OK.
>
> > FAQ discusses a special case when programmer has forgotten to do
> > #include <stdlib.h>. I am including this header and I am not doing any
> > explicit cast:

>
> Yes, that how it should be, if you really read the FAQ: include the header,
> don't use the cast.
>
> > 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>.

>
> Of course, you don't. In C language 'void*' pointers are implicitly convertible
> to and from other pointer types. What warnings did you expect and why?


To and from other object pointer type. There is no defined conversion
between pointers to functions and pointers to object types, even
incomplete object types like void.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 16/04/2008, 03h42   #6
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

Jack Klein wrote:
> Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
> > arnuld wrote:
> > > I have checked the FAQ:
> > > http://c-faq.com/malloc/mallocnocast.html
> > > FAQ discusses a special case when programmer has
> > > forgotten to do #include <stdlib.h>. I am including
> > > this header and I am not doing any explicit cast:

> >
> > Yes, that how it should be, if you really read the
> > FAQ: include the header, don't use the cast.


I _really_ read the FAQ and took notice of the parenthetical
comment at the end. It highlights that the real issue lies
with using unprototyped functions. Sensible programmers
will use compilers that advise of such things. [Of course,
conforming C90 compilers are not required to issue
diagnostics, but nevertheless the issue has been around
long enough that you'll be hard pressed to find any
conforming compiler that isn't capable of alerting you
to the use of an unprototyped function. Personally, I
think a C programmer be insane not to use that feature
if it was available.]

> > > 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>.

> >
> > Of course, you don't. In C language 'void*' pointers
> > are implicitly convertible to and from other pointer
> > types. What warnings did you expect and why?

>
> To and from other object pointer type. There is no
> defined conversion between pointers to functions and
> pointers to object types, even incomplete object types
> like void.


Except for the case of null pointer constants.

--
Peter
  Réponse avec citation
Vieux 16/04/2008, 06h20   #7
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

arnuld wrote:
> Jack Klein wrote:
> > Andrey Tarasevich:
> > > Of course, you don't. In C language 'void*' pointers are implicitly
> > > convertible to and from other pointer types. What warnings did you
> > > expect and why?

> >
> > To and from other object pointer type. There is no defined conversion
> > between pointers to functions and pointers to object types, even
> > incomplete object types like void.

>
> so an int* is implicitly converted to a void*


What int*?

> which then can be implicitly converted to char* without any
> warning at all.


Consider...

int *ip = malloc(N * sizeof *ip);

The malloc function knows nothing about the type being allocated.
It returns a void * to a region suitably aligned for any object.

There is an implicit conversion from void * to int * in the assignment
of the void * to ip, but as you say there is (generally) no warning.
Nor should you expect there to be one. The implicit conversion of
void * to and from other object or incomplete types is a language
_feature_. [Not necessarily a good one, but a deliberate feature
nonetheless.]

--
Peter
  Réponse avec citation
Vieux 16/04/2008, 06h46   #8
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: malloc() and implicit cast

arnuld said:

>> On Tue, 15 Apr 2008 21:00:17 -0500, Jack Klein wrote:

>
>> To and from other object pointer type. There is no defined conversion
>> between pointers to functions and pointers to object types, even
>> incomplete object types like void.

>
> so I conlcude:
>
> 1.) Function Pointers: pointer to function returning an int can be
> implicitly converted into char* without any any warning message.


I don't see why you conclude this from what Jack said, because it simply
isn't true. A pointer to function, of no matter what return type, cannot
be implicitly converted into *any* other type, let alone a char *.

> 2.) Pointers to object types: compiler can implicitly convert and int*
> into char* without any warning message to the programmer.


No, there is no implicit conversion between int * and char *.

> C [does not] require any warning in this case


Implementations are required to diagnose an attempt to assign int * to char
* and vice versa.

> Is that what you mean ?


I doubt it, because it's completely wrong.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h03.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14018 seconds with 16 queries