![]() |
| | #31 | ||||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
Quote:
Quote:
__________________ Joe | ||||
| JoeSixpack is offline |
| | #32 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| >Then you don't fully understand my point. Allow me to take a stab at it. You are saying that the following code is wrong if I wanted to simulate an array of integers: Code: #include <stdlib.h>
/*
** my_header.h declares a to be a pointer to double
*/
#include "my_header.h"
int main ( void )
{
a = malloc ( 24 * sizeof *a );
return 0;
}
No warnings.
Code: #include <stdlib.h>
/*
** my_header.h declares a to be a pointer to double
*/
#include "my_header.h"
int main ( void )
{
a = (int *)malloc ( 24 * sizeof *a );
return 0;
}
C:\C\C.c(9) : warning C4133: '=' : incompatible types - from 'int *' to 'double *'
-Prelude
__________________ My best code is written with the delete key. |
| Prelude is offline |
| | #33 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| Joe, after all that, I don't think that you understand your point. Your point should've been, "It helps not to be a sloppy programmer!". Then everything would make sense! Nuff said already! |
| The Dog is offline |
| | #34 | |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Quote:
*Errata for The C Programming Language, Second Edition #142 More Gurus void main() and cast to malloc malloc question Pointer Casts | |
| Dave_Sinkula is offline |
| | #35 | ||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
By all means, post keep posting links. It doesn't alter the fact that it can be beneficial to cast the return value of malloc. Also I stated this in the context of using a C99 conforming compiler (right back at the start of the thread). The reason often given (and is in a few of the links you've posted) for not casting malloc is the possible implicit prototype problem. This is no longer a problem if the compiler doesn't allow implicit prototypes.
__________________ Joe | ||
| JoeSixpack is offline |
| | #36 | ||
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Quote:
Quote:
http://groups.yahoo.com/group/c-prog/message/12179 | ||
| Dave_Sinkula is offline |
| | #37 | |
| Registered User Join Date: Sep 2002
Posts: 272
| That link is just rehashing what's already been posted here. It doesn't stop you making an error in the assumption as to what the pointer type is (you could falsely assume that ptr3 pointed to a foobar2). foobar *ptr3; /*lots of code*/ /*I now want 20 foobar2's but make an error in my assumption as to the type of pointer ptr3 is and through my stupidity assume that it has been declared to point at a foobar2s*/ ptr3 = malloc (20*sizeof(*ptr3)); /*casting the return value of malloc above would have explicitly told the compiler what I was trying to do, and made it scream at me. Therefore the method used above is less robust*/ Also, the poster in your link complaining about all the cutting and pasting needed has presumably not heard of macros. Quote:
. Probably.
__________________ Joe | |
| JoeSixpack is offline |
| | #38 |
| Registered User Join Date: Dec 2001
Posts: 88
| It's funny to read... I think noone will change their minds, so why discussing? I (and as I see some others) keep doing it like the gurus, and you JoeSixpack keep casting. I have learned that it is possible to program in a different style as the gurus, but I got to know, that it is somehow easier when you do what the gurus tell you
__________________ Hope you don't mind my bad english, I'm Austrian! |
| Shade is offline |
| | #39 | ||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
__________________ Joe | ||
| JoeSixpack is offline |
| | #40 | |
| Visionary Philosopher Join Date: Aug 2002
Posts: 212
| I had to reinvolve myself with this-- this thread is an absurd example (yet again) of people becoming lost in the syntax issues of a language or a compiler, rather than being able to see beyond such things and get to the fascination of the logic. Get's old, doesn't it. ------------------------------------------------------------- To JoeSixPack: Quote:
To resolve type mismatches at the preprocessor level. type coercion has nothing to do with execution. --------------------------------------------------------------- To the rest of you: If JoeSixPack wants to write all his code without ever using coercion-- fine. More power to this person. We've listed what coercion/casting is for. It's a tool, nothing more. I prefer to utilize all my tools, rather than taking the extreme stance that 'tool x is good for nothing' or 'you should never use tool z'. Keep on programming
__________________ It is not the spoon that bends, it is you who bends around the spoon. | |
| Sayeh is offline |
| | #41 | |
| Registered User Join Date: Sep 2002
Posts: 272
| *Joe Sxpack bangs his head violently against the wall* Quote:
And that, is timeless. Nice misquote, btw.
__________________ Joe | |
| JoeSixpack is offline |
| | #42 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| First off I would like to point out to everyone that says "don't type cast malloc()" that just because you don't need the type cast doesn't you shouldn't. malloc(), calloc(), and realloc() are part of the standard C memory manager. Check this out: Code: int *i = malloc(1); Code: int *i = (int *)malloc(1); |
| master5001 is offline |
| | #43 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline |
| | #44 |
| Registered User Join Date: Feb 2002
Posts: 6
| By the way, can any body expain how to use realloc() function, and it is better to use a example. Thanx for help! |
| pnxi is offline |
| | #45 | |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,183
| Re: realloc Quote:
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules | |
| Fordy is offline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Wiki FAQ | dwks | General Discussions | 192 | 04-29-2008 01:17 PM |
| malloc, calloc from the FAQ | salvadoravi | C Programming | 10 | 01-21-2008 03:29 AM |
| malloc casting - quote from the FAQ | salvadoravi | C Programming | 16 | 12-17-2007 06:24 PM |
| FAQ: Difference between C and C++ style casting | Queatrix | FAQ Board | 1 | 12-23-2006 12:09 PM |
| Casting malloc? | Simon | C Programming | 44 | 10-08-2002 02:25 AM |