473,797 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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/
Jun 27 '08 #1
17 2528
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/
Jun 27 '08 #2
"arnuld" <ul**@kullu.com wrote in message
news:pa******** *************** *****@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

Jun 27 '08 #3
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
Jun 27 '08 #4
On Tue, 15 Apr 2008 08:13:43 -0700, Andrey Tarasevich
<an************ **@hotmail.comw rote 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.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #5
Jack Klein wrote:
Andrey Tarasevich <an************ **@hotmail.comw rote:
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
Jun 27 '08 #6
On Tue, 15 Apr 2008 21:00:17 -0500, Jack Klein wrote:
>On Tue, 15 Apr 2008 08:13:43 -0700, 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* which then can be
implicitly converted to char* without any warning at all.

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

find my email ID at the above address.
Jun 27 '08 #7
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.

2.) Pointers to object types: compiler can implicitly convert and int*
into char* without any warning message to the programmer. C doe snot
require any warning in this case

Is that what you mean ?


--
http://lispmachine.wordpress.com/
Jun 27 '08 #8
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
Jun 27 '08 #9
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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
11465
by: pertheli | last post by:
Hello all What is the difference between Method 1 and Method 2 below? Is Method 2 safe to use? typedef short Word; typedef unsigned char Char; int nAllocSize = large number;
34
6446
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
231
23250
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
27
4772
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning: assignment makes pointer from integer without a cast ================================================================== when the following piece of code was compiled. The offending statement is calloc. A similar statement in the main() function...
42
2182
by: Joris Adriaenssens | last post by:
This is my first posting, please excuse me if it is off-topic. I'm learning to program in C. It's been almost ten years I've been programming and a lot of things have changed apparently. I understand from other postings that casting a result from malloc isn't good. In the past I have always been casting the malloc. I think it was even necessary. (But that's a long time ago, I hadn't heard of a standard for C these days). Was it...
36
2287
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program code ... */
54
7874
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
68
15718
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
24
1981
by: Norbert Leister | last post by:
Hi NG, I've the problem, that a malloc call is not returning. <source-snip> printf("a\n"); my_pointer = (struct_pointer)malloc(struct_size); /*size 1420*/ printf("b\n"); </source-snip>
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.