Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2005, 2:29 PM   #1
sparda
Newbie
 
Join Date: Apr 2005
Location: US
Posts: 15
Rep Power: 0 sparda is on a distinguished road
Send a message via Yahoo to sparda
Question regarding Malloc() and Type casting

I've been learning C for some time, but now im stuck! I try to compile this simple source code but i get errors.
#include <stdio.h>
#include <stdlib.h>

int main()
{
     int *ptr;
     ptr = (*int)malloc(sizeof(int)*5);
     return 0;
}

I think it has to do with TYPE casting the int. I've switched it, and it works. For example:

incorrect: (*int)
correct: (int*).

My question is. What ways is the correct way to TYPE cast?
im guessing (int*) is the correct way, but then i again, i've seen it use as (*int). Im very confused. someone please help.
sparda is offline   Reply With Quote
Old Sep 26th, 2005, 2:51 PM   #2
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 555
Rep Power: 5 Benoit is on a distinguished road
(int *)...malloc() returns a void pointer which you must typecast
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Sep 26th, 2005, 6:25 PM   #3
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
I dont know where you've seen *int but in C (and C++) it should be int*, whether you are casting to or declaring an int pointer.

If you are using a C compiler and you have included stdlib.h, there is no need to cast the result from malloc() (calloc,realloc,...), infact it can cover up serious bugs (see here for more info).
Animatronic is offline   Reply With Quote
Old Sep 27th, 2005, 4:48 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
casting malloc is a holdover from pre C89 compilers. When you do that you mask errors, important errors.

PS: you should check to be sure ptr is not NULL after the malloc call. malloc returns NULL when it hits a memory error.
jim mcnamara is offline   Reply With Quote
Old Sep 27th, 2005, 11:04 PM   #5
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
(* int) is incorrect!
susam_pal is offline   Reply With Quote
Old Sep 28th, 2005, 8:56 AM   #6
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
you don't need to type cast anymore like Animatronic said, saves you alotta trouble.
Serinth is offline   Reply With Quote
Old Sep 29th, 2005, 4:12 AM   #7
Ringo
Newbie
 
Join Date: Sep 2005
Location: Cambridge, UK
Posts: 13
Rep Power: 0 Ringo is on a distinguished road
Thats an excellent link anmatronic. It's bookmarked!

As it says, most experienced programmers use "p = malloc ( n * sizeof *p );"

I may have added a few parenthesis ie. sizeof(*p) but this makes perfect sense. It's completely portable and you aren't covering up the potential problem you will mask by casting (ie an error if you haven't added stlib.h).
Ringo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC