Is there some logic behind the syntax of malloc function?
malloc
Collapse
X
-
Tags: None
-
Um, I'm not entirely sure what you're asking here, but the answer is yes. malloc() allows you enter in the number of bytes you need allocated as a parameter so that you can get the amount you need. It returns a void* so that you can cast it to whatever type of pointer you need. I think you may be asking about the logic of casting syntax, which I think of as a sort of "multiplication ". For example,
[CODE=c]char* c;
int* i = (int*)c;
[/CODE]
says to me "take this pointer and 'multiply' by the new type", thus changing the way pointer arithmetic works with it and how the compiler interprets the data it points to. -
What is it? M not so sure that i got you.Originally posted by oler1sParul, have you ummm, considered checking documentation?Comment
-
first=(*struct linklist)malloc (sizeof(struct linklist));
Here the * mark should have been at the end;
first =(struct linklist*)mallo c(sizeof(struct linklist));
but when i wrote the earlier statement; my compiler showed the error that linklist unexpected.
linklist is the name of my struct.
Does somebody know why do we use * at the end only?
And is it used for symbolic representation of multiplication? ; as i got it from some earlier replies?
Is that the logic behind using *? or is it something else?Comment
-
Originally posted by Parul Bagadiafirst=(*struct linklist)malloc (sizeof(struct linklist));
Here the * mark should have been at the end;
first =(struct linklist*)mallo c(sizeof(struct linklist));
but when i wrote the earlier statement; my compiler showed the error that linklist unexpected.
linklist is the name of my struct.
Does somebody know why do we use * at the end only?
And is it used for symbolic representation of multiplication? ; as i got it from some earlier replies?
Is that the logic behind using *? or is it something else?
I think you need to check the pointer basics.
* at the beginning means the value hold by the pointer and * at the end dosent mean multiplication, but u are saying it is of that pointer type.
RaghuramComment
-
The * is overloaded in C.
Sometimes it means de-reference. Sometimes itr means pointer and sometimes it means multiplication.
Each meaning is exclsive from the rest. Therefore, the exact meaning of the * is by the context in which it is used.
As to your malloc() question:
1) malloc() allocates a block of memory in bytes.
2) the number of bytes to allocate is the malloc() argument
3) and * used in the argument is multiplication.
4) malloc returns the address of the allocation as a void*
5) the * in void* means pointer.
6) you can cast this pointer to your own pointer.
7) you will use an * again for the cast to mean pointer.Comment
-
Thanx for explaining so nicely.Originally posted by weaknessforcatsThe * is overloaded in C.
Sometimes it means de-reference. Sometimes itr means pointer and sometimes it means multiplication.
Each meaning is exclsive from the rest. Therefore, the exact meaning of the * is by the context in which it is used.
As to your malloc() question:
1) malloc() allocates a block of memory in bytes.
2) the number of bytes to allocate is the malloc() argument
3) and * used in the argument is multiplication.
4) malloc returns the address of the allocation as a void*
5) the * in void* means pointer.
6) you can cast this pointer to your own pointer.
7) you will use an * again for the cast to mean pointer.
But according to your first point where is it used as de-reference?
As in m asking for example.
And what exactly is de-refernce? Is it to free some reference or what?Comment
-
Parul: Dereferencing is anytime you access (or assign) the data a pointer points to using *. For example:[CODE=c]
int* x = (int*)(malloc(s izeof(int));
*x = 4;
[/CODE]
Thus, a 4 is now held in the memory whose address is at x.
dhranidar: Please make your own thread rather than hijacking this one. Thank you.Comment
-
I didnt get the first word of your last line.Originally posted by LaharlParul: Dereferencing is anytime you access (or assign) the data a pointer points to using *. For example:[CODE=c]
int* x = (int*)(malloc(s izeof(int));
*x = 4;
[/CODE]
Thus, a 4 is now held in the memory whose address is at x.
dhranidar: Please make your own thread rather than hijacking this one. Thank you.Comment
-
How the sizeof operatof used in malloc makes malloc machine independant?
m not sure about what it makes machine independent.Comment
-
I was addressing the last sentence to the other user, Parul. As to how sizeof makes malloc or any other code more platform independent, on a 32bit machine, sizeof(int) is 4. This covers nearly all computers released in the last 20 years. However, recently, 64bit machines have begun to come out, on which sizeof(int) would be 8. Hardcoding the 4 on one of those machines would lead to unexpected outcomes, like thinking arrays are twice as long as they actually are.Comment
-
There are still plenty of 8 and 16 bit micro-processors in production today with C compilers whose sizeof(int) is often 2.Originally posted by LaharlAs to how sizeof makes malloc or any other code more platform independent, on a 32bit machine, sizeof(int) is 4. This covers nearly all computers released in the last 20 years
See, PIC, Rabit or AVR micro-processors for an example.Comment
Comment