473,465 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

explicit cast to malloc()

hello everyone ,
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the matter ?

pls help
thank you
mohan
Jul 25 '08 #1
9 2030
mohi wrote:
hello everyone ,
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the matter ?
You're compiling C++, not C. The comp.lang.c++ newsgroup
is just down the hall to your left; I hear they're planning to
have a party as soon as they find the template.

--
Er*********@sun.com
Jul 25 '08 #2
On 25 Jul, 20:46, mohi <mohangupt...@gmail.comwrote:
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the *matter ?
Most likely you are invoking gcc as a c++ compiler.
Jul 25 '08 #3
mohi <mo**********@gmail.comwrites:
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*

whats the matter ?
Does it show an error *like* "invalid conversion from void* to node*",
or does it show that exact error?

I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".

If that doesn't help, read question 7.6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>, and in the future please check the FAQ before
posting. If that doesn't help, read the rest of section 7, and
perhaps the entire FAQ. And if *that* doesn't help, feel free to post
again -- but *please* copy-and-paste your actual complete program and
the exact error message.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 25 '08 #4
On Jul 25, 4:10*pm, Keith Thompson <ks...@mib.orgwrote:
mohi <mohangupt...@gmail.comwrites:
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));
instead of
node * nod=(node *) malloc( sizeof(node));
but when i use the upper way my gcc compiler shows error like
invalid conversion from void* to node*
whats the *matter ?

Does it show an error *like* "invalid conversion from void* to node*",
or does it show that exact error?

I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".
You mean <stdlib.h>...

-David
Jul 25 '08 #5
On Jul 26, 1:14 am, David Resnick <lndresn...@gmail.comwrote:
On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.orgwrote:
mohi <mohangupt...@gmail.comwrites:
as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));
instead of
node * nod=(node *) malloc( sizeof(node));
but when i use the upper way my gcc compiler shows error like
invalid conversion from void* to node*
whats the matter ?
Does it show an error *like* "invalid conversion from void* to node*",
or does it show that exact error?
I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".

You mean <stdlib.h>...

-David
sorry guys im really sorry
i did compile it with g++
it becomes so confusing when u have to use both c and c++
is there a better way of dealing with this confusion
:)
mohan
Jul 25 '08 #6
mohi wrote:
[...]
it becomes so confusing when u have to use both c and c++
is there a better way of dealing with this confusion
Sure:

(setq use-C nil
use-C++ nil
use-lisp exclusively
tongue-in-cheek t)

--
Er*********@sun.com
Jul 25 '08 #7
David Resnick <ln********@gmail.comwrites:
On Jul 25, 4:10*pm, Keith Thompson <ks...@mib.orgwrote:
[...]
>I might expect to see an error message about converting from int to
node* if you forgot the mandatory "#include <stdio.h>".

You mean <stdlib.h>...
Yes, thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 25 '08 #8
mohi wrote:
>
as i have noted a lot many times it has been mentioned that we
shudn't cast pointers returned by malloc(). i mean its been
said that use: node * nod=malloc(sizeof(node));
instead of: node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like
invalid conversion from void* to node*

whats the matter ?
You are using a C++ compiler, not a C compiler. Probably because
you have misnamed the source file, which should be of type .c
(lower case).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 25 '08 #9
In article <84**********************************@s21g2000prm. googlegroups.com>,
mohi <mo**********@gmail.comwrote:
>as i have noted a lot many times it has been mentioned that we shudn't
cast pointers returned by malloc()
i mean
its been said that use
node * nod=malloc(sizeof(node));

instead of

node * nod=(node *) malloc( sizeof(node));

but when i use the upper way my gcc compiler shows error like

invalid conversion from void* to node*
Does it literally say void *, or does it say int?
If it literally says void * you're probably compiling in C++
mode not in C mode. If it says int, then, you're probably
not #include'ing the right header for malloc, that is, stdlib.h
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 26 '08 #10

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

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
0
by: Reece Hart | last post by:
Here's the basic issue: PostgreSQL doesn't use indexes unless a query criterion is of exactly the same type as the index type. This occurs even when a cast would enable the use of an index and...
36
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both ...
6
by: JS | last post by:
On this page under "Malloc": http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html I found this: ptr = (*int) malloc(sizeof(int)*N)
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
2
by: John Richardson | last post by:
How does a custom casting operator work? I have a class that defines one, but doesn't work as expected (or perhaps, the way I want it to). I have the following example classes:...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.