In the Malloc function we are using the syntax as ptr=(int*)malloc(sizeof(int)); Why we are using the int there? I mean we know the size of int is 2. Then is it ok that we can give (int*) malloc(2)? Which is better?
Question asked by visitor reg
In the Malloc function we are using the syntax as ptr=(int*)malloc(sizeof(int)); Why we are using the int there? I mean we know the size of int is 2. Then is it ok that we can give (int*) malloc(2)? Which is better?
Question asked by visitor reg
Malloc function requires size of the data type or expression as an argument to allocate memory space dynamically. Malloc will not return any error message if we will write 2(or any integer) insted of writing int as argument.
But we should practice to write datatype as argument of malloc function to avoid calculating size of expressions. It is not easier to calculate size of structure variables. For such calculations we are using computer. Let computer do its work... & be relax.
Niladri
defalut return type of malloc function is void . so to explicitly convert it we need to specify (int *)
basicaly malloc function requires the amount of memory that you are requesting for.ofcourse one can easily write "2" instead of sizeof(int).
But it will not be smart programming since on different plantforms the size of integers varies from 2 to 4 .therefore sizeof(int) is much more flexible code then the other one ,provides platform independency.
Good basic question. Here is the answer.
Many C programs assume that the size of an integer was 16 bits i.e. 2 bytes. But, this is not guaranteed. C standard does not specify the sizes of the basic data types, except to specify that
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(float) <= sizeof(double).
What this means is that depending on the machine architecture, sizeof(int) can be either 2 or 4. For example, on a 32bit operating system, sizeof(int) is 4 bytes/32 bits.
So, if you want your program to run on many different machines without any problems, please do not use assume the sizes of basic data types and rely on the sizeof() operator to write correct programs. Please note that by using sizeof(), your program is Portable i.e. can run anywhere, irrespective of the machine architecture, etc.
As an addition, a similar problem is encountered in structure sizes. For example, consider the following structure declaration:
struct aaa {
int a;
char b;
int c;
}
What is the size of this structure??
- if size of int is assumed to be 2 bytes, then is the struct size 5 bytes??
- if size of int is assumed to be 4 bytes, then is the struct size 9 bytes??
Both the above answers are wrong, even if I told you the actual size of an integer. This is because of the concept of Padding. The C compiler is free to add any amount of padding to structure to ensure proper alignment. Why this is necessary is the topic of another post. But, "portable" programming specifies that, just like the int case above, one should not assume the size of a struct, but use the sizeof() operator to find the size of a struct for using it in malloc() and other places.
Hope this helps.