LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2005, 07:13 AM   #1
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Rep: Reputation: 30
whats the significance of void * here


hello,
I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here.
Does void * i used when function has the flexibility to return any type of value by casint that functions with appropriate data type?
 
Old 03-09-2005, 07:30 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
void * means that the function returns a pointer of some unspecified type. You'll need to cast the return value to be a pointer to the correct type for it to be of much use.

Code:
void foo (void) {
    char *mychars;
    int *myints;

    mychars = (char *) malloc (100);
    myints = (int *) malloc (100);
}
 
Old 03-09-2005, 08:37 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
In C, you want to avoid typecasting the return value of malloc(). As long as you #include <stdlib.h> then the compiler will automatically convert the void * pointer. For C++ you'll need to typecast it, but you shouldn't be using malloc() in C++ anyway. You should be using new.
 
Old 03-09-2005, 09:10 AM   #4
DaFrawg
LQ Newbie
 
Registered: Jun 2004
Location: The Netherlands
Distribution: Slackware 10.1, Debian Sarge
Posts: 28

Rep: Reputation: 15
Quote:
Originally posted by itsme86 As long as you #include <stdlib.h>
#include <cstdlib> perhaps?
 
Old 03-09-2005, 10:26 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
Originally posted by DaFrawg
#include <cstdlib> perhaps?
That's for C++. #include <stdlib.h> is for C which is what I was referring to.
 
Old 03-09-2005, 10:46 AM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally posted by trevelluk
void * means that the function returns a pointer of some unspecified type. You'll need to cast the return value to be a pointer to the correct type for it to be of much use.

Code:
void foo (void) {
    char *mychars;
    int *myints;

    mychars = (char *) malloc (100);
    myints = (int *) malloc (100);
}
Many people say that now days it's better to not cast the returning value of malloc in ANSI C, and that this is an old habit when coding in traditional C, that should now stop:
http://www.stanford.edu/~blp/writing...lloc-cast.html
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Quote:
One should not cast the return value of malloc() when writing C.

When stdlib.h has been included, malloc()'s declaration will be visible. Its return type is void*, and void* can be silently converted to other pointer types.

If one should forget to include the declaration, malloc will be assumed to return int.

ints will be converted to pointer types -- but typically that conversion will not be silent. Most, if not all, C compilers will produce a warning to the effect of "you are converting an int into a pointer without explicitly casting -- are you sure you want to?". This alerts one to the fact that one has omitted to include a header.

If you put the cast in, that message will be quashed -- after all, the point of casts is to say to the compiler, "Ignore what you think you know and do what I say" -- and a [potential] problem be left latent in your code.
As you see casting the returning value of malloc may lead to bugs that are really hard to track down.
This is something that is been discussed in this forum again. I did not know that until recently either...

Last edited by perfect_circle; 03-09-2005 at 10:50 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the significance of 256? Cyberian General 9 02-13-2005 03:41 AM
Significance of Upcoming 2.6.x Kernel...? TheOneAndOnlySM Linux - General 6 12-10-2003 08:03 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM
significance of 'core' file ? DoubleLetter Linux - General 1 08-29-2001 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration