Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C

explicit cast to malloc()

 
Jump to:  
 
mohi
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: 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
 

 
Eric Sosman
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: Re: explicit cast to malloc()
       
mohi wrote:
Quote:
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.

--
Eric.Sosman@sun.com
 

 
Keith Thompson
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: Re: explicit cast to malloc()
       
mohi <mohangupta13@gmail.com> writes:
Quote:
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) kst-u@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"
 

 
CBFalconer
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: Re: explicit cast to malloc()
       
mohi wrote:
Quote:

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.
 

 
Eric Sosman
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: Re: explicit cast to malloc()
       
mohi wrote:
Quote:
[...]
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)

--
Eric.Sosman@sun.com
 

 
Keith Thompson
PostPosted: Fri Jul 25, 2008 7:46 pm    Post subject: Re: explicit cast to malloc()
       
David Resnick <lndresnick@gmail.com> writes:
Quote:
On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.org> wrote:
[...]
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) kst-u@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"
 

 
William Pursell
PostPosted: Fri Jul 25, 2008 7:57 pm    Post subject: Re: explicit cast to malloc()
       
On 25 Jul, 20:46, mohi <mohangupt...@gmail.com> wrote:
Quote:
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.
 

 
David Resnick
PostPosted: Fri Jul 25, 2008 8:14 pm    Post subject: Re: explicit cast to malloc()
       
On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.org> wrote:
Quote:
mohi <mohangupt...@gmail.com> writes:
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
 

 
mohi
PostPosted: Fri Jul 25, 2008 8:24 pm    Post subject: Re: explicit cast to malloc()
       
On Jul 26, 1:14 am, David Resnick <lndresn...@gmail.com> wrote:
Quote:
On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.org> wrote:



mohi <mohangupt...@gmail.com> writes:
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
Smile
mohan
 

 
Greg Comeau
PostPosted: Fri Jul 25, 2008 10:26 pm    Post subject: Re: explicit cast to malloc()
       
In article <84799233-4bea-453e-8f32-a97c1626a015@s21g2000prm.googlegroups.com>,
mohi <mohangupta13@gmail.com> wrote:
Quote:
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 ==> LINK
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Stwarzanie świata - Miłosz Czesław t2006-bg Koktajle Podczas Świąt - Lipska Ewa koszulki