![]() |
| | #16 |
| Blank Join Date: Aug 2001
Posts: 1,034
| to the wrong type. |
| Nick is offline |
| | #17 |
| Registered User Join Date: Sep 2002
Posts: 272
| If you're so asleep as to cast it to the wrong type then you're not aware of the type(amount) of memory you're allocating, so cast or no cast you're in trouble.
__________________ Joe |
| JoeSixpack is offline |
| | #18 | |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Quote:
Code: mytype *t = malloc(10 * sizeof(*t)); | |
| Dave_Sinkula is offline |
| | #19 | |
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
t = malloc(20 * sizeof(t)); Did you really want 20 t's? t is just a variable name. It is possible that due to an oversight you believed t was of type mytype2 and not mytype. If you want 20 mytype2's, you can guarantee that if there's room enough and t is a pointer to mytype2 then this'll get them if it doesn't issue an error or warning - t = (mytype2*)malloc(20 * sizeof(mytype2));
__________________ Joe | |
| JoeSixpack is offline |
| | #20 | |||||||
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Quote:
Quote:
Code: int *t = (int*)malloc(N * sizeof(int*)); Code: anytype *t = malloc(N * sizeof(*t)); Quote:
Quote:
Quote:
Quote:
Quote:
I'm sorry. I've been down this road and back again several times. I have yet to see any justifyable advantage to casting. The only thing mentioned lately is for use with pre-ANSI compilers. Frankly I'd rather have the warnings pop up to let me know that I'm using an ancient compiler. | |||||||
| Dave_Sinkula is offline |
| | #21 | |||||||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
Quote:
int i = 0; //some code i=10; what is i? It hasn't lost it's type. But the programmer is using a variable called i without explicitly using the type. Quote:
Quote:
Quote:
Quote:
__________________ Joe | |||||||
| JoeSixpack is offline |
| | #22 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| Regarding the usage of malloc, I prefer: TYPE *ptr; ptr = malloc( sizeof( TYPE ) * NUMBER_TO_ALLOCATE ); There is no benefit I can see to ever casting, unless you are implicitly converting from one type to another. In malloc's case, there is no reason to ever cast that I can envision. The return type of malloc is to a continuous block of N bytes where N is: sizeof( TYPE ) * NUMBER_TO_ALLOCATE As such, it really doesn't matter what it's cast into, the cast is a moot point. Once a pointer is pointing at some block of memory, it could care less what type it was cast to when it was originally set to the address. Setting a pointer to point to an address does not change its type. As such, there is nothing that casting pointers from one type to the next provides, other than preventing your compiler from yelling at you. Example: char *c; int *i; float *f; f = (float*) i = (int*) c; This will (I believe, haven't bothered trying it) prevent your compiler from *****ing at you, but the end result is irrelevent. In the end, they all point to the same spot in memory, and in the end, when you increment any of them, they only increment by N bytes, where N is the size of each variable. So the end result is, it doesn't matter what you cast a pointer to, it will still be its own type, and nothing you can do will change that, unless you implicitly force cast it in incrementation: ((float*)c)++; Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline |
| | #23 | |||||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
The problem isn't with the allocated memory, as casting isn't going to help if you balls up here. But with how this memory is manipulated. Quote:
Quote:
Quote:
An example - Code: #include<stdlib.h>
int main(void)
{
char *c;
double *d;
//lots of code
//Lets say I want 20 integers
//this is wrong because I've
//assigned it to a character pointer
//even though I have the correct amount
//of memory
c = malloc(20*sizeof(int));
//this is wrong because I've
//made an error in assuming that
//d is a pointer to an integer
d = malloc(20*sizeof(*d));
//A cast indicating my intentions
//on either of the previous 2 would
//have brought the error to my attention
//with a compiler error/warning
//As they stand this program is
//buggy as hell
return(0);
}
__________________ Joe | |||||
| JoeSixpack is offline |
| | #24 | |||||||
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Quote:
Quote:
Quote:
Quote:
Quote:
If t is an int pointer, then *t is an int. And if t is a pointer to some other type, then *t is whatever type that is. Or is that ass-backwards? | |||||||
| Dave_Sinkula is offline |
| | #25 | |||
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| Quote:
You're just asking your compiler to yell at you by possibly casting it to the wrong type. There is no benifit what so ever in casting a void pointer. Period. Ever. End of story. Quote:
The only reason you could ever possible get this wrong is if you typed the single malloc line wrong: char * c = malloc( sizeof(float) * 100 ); And even then, it still won't care. The only way it would care, would be if you cast it implicitly. Why on earth would you? char * c = (float*) malloc( sizeof( float ) * 100 ); And that's absurd. Why on earth would you ever do that? There is no point in EVER casting void pointers. Period. Quote:
Your argument is absurd, as is your example. I cannot envision this ever possibly happening. By all means, feel free to cast, but there is absolutely no point in ever doing so. Your example is incredibly far fetched. You're basicly saying "Hey, forget that you just declared a variable, and pretend that you think it's some other type!" Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |||
| quzah is offline |
| | #26 | |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| There is one interesting point here that is almost illustrated. Quote:
Code: int x = 5.0; | |
| Dave_Sinkula is offline |
| | #27 | ||||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
Quote:
Quote:
If you cannot show me how casting would not highlight the errors (and therefore help in the fixing of those errors) in my example posted above then my point stands no matter how absurd you believe it to be. End of story.
__________________ Joe | ||||
| JoeSixpack is offline |
| | #28 |
| Registered User Join Date: Dec 2001
Posts: 88
| int* p; p=(char*)malloc(100); so what? Compiler says: failure! But what is the benefit? It's a false sense of security: p=(int*)malloc(100); The compiler don't say anything - but it's also wrong... What does it help? If you don't know which type p is, you are completly lost. I don't want to discuss such things. Go and read K&R. Do they cast?? Or look for other gurus! They will tell you all the same. The only reason why one casts a void* is to keep compability with C++, there is no other reason.
__________________ Hope you don't mind my bad english, I'm Austrian! |
| Shade is offline |
| | #29 | ||||
| Registered User Join Date: Sep 2002
Posts: 272
| Quote:
Quote:
Quote:
Quote:
__________________ Joe | ||||
| JoeSixpack is offline |
| | #30 | ||
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| Quote:
You're 500 lines into your program and you need to allocate some memory, here's what you see, Code: //.. temp = malloc ( sizeof ( type ) * NUM_OF_ELEMENTS ); //.. Yeah you could argue that you can just scroll up to temp's declaration, but then you'd never pass the wrong type to sizeof(), so what's your argument? You're f'ed both ways! Casting doesn't help, in any way, IMO. Is your compiler gonna stop you from doing this : Code: //.. int *iPtr; iPtr = malloc ( sizeof ( char ) * NUM_OF_ELEMENTS); //.. Quote:
Thus, less checking, less typing. So c'mon how can casting a malloc() return be beneficial? And the beat goes ooooooooon! Ladadadadee, Ladadadadaa. | ||
| The Dog 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 |