views:

162

answers:

3

In the following code,what is the meaning of buf = malloc(n * sizeof(char));

is n*sizeof(char) necessary,if yes.. please elaborate.

int n;

char* buf;

fstat(fd, &fs);

n = fs.st_size;

buf = malloc(n * sizeof(char));

EDIT1 And What if I write (n*sizeof(double))

+6  A: 

The malloc function allocates bytes and takes as input the number of bytes you would like to allocate. The sizeof operator returns the number of bytes for a given type. In this case a char is 1 byte, but in the case of an int it is most likely 4 bytes or double is most likely 8 bytes. The expression n * sizeof(char) converts the number of char into the number of bytes that are desired.

In the case illustrated, using char, computing the number of bytes is probably not needed, but it should be done as it helps to convey your intent.

What the expression is doing is allocating the desired amount of memory needed to hold at most n number of char's and returning you a pointer, buf, to the beginning of that allocated memory.

linuxuser27
Perhaps the OP is asking because sizeof(char) is defined to always be 1.
Ed Swangren
I thought of that, but all that it will ever cost anyone is a multiply by 1 instruction in the event they are doing `char`. I think conveying intent here weights out.
linuxuser27
@linuxuser27: ...and the compiler will optimize away the multiplication by 1.
Jonathan Leffler
@Jonathan Leffler - Bingo ;) Just for intent
linuxuser27
+1  A: 

The ISO standard defines a byte as the same size as a char.

You never need sizeof(char) for malloc

codaddict
A: 

Yes, it's necessary. Think of malloc as giving you a box. In this box you can do all kinds of magical stuff. All you need to tell malloc how big the box needs to be.

n * sizeof(char) is the size of the box (n x the size of the char type dictated by the compiler -- I think it's always 1 for variations of char though, but you should never count on these things, especially if developing for embedded systems).

Basically, you cannot do something like buf = malloc() as it wouldn't even compile.

Edit: To answer your second question, try running and compiling this:

#include <iostream>

int main()
{
 std::cout << sizeof(double) << std::endl;
 std::cout << sizeof(char);
}

You'll see that sizeof(double) is, or should be, 8, and sizeof(char) is, or should be, 1. So in your case it would make the box have a "size" of 1 x n bytes in the case of the char or 8 x n bytes in the case of the double.

Usually, it's not smart to allocate a ton of memory if you don't use it.

David Titarenco
Why the downvote? :(
David Titarenco
I didn't downvote, but I disagree with "you should never count on these things." It is *required* to be 1 in both C89 and C99, and any embedded environment that has otherwise has a serious bug. Further, this is a C question, and you're unnecessarily posting C++. Finally `sizeof(double)` is not required to be 8.
Matthew Flaschen
@David Titarenco - Does this mean that we are allocating memory more than what is required.PLease check edit2.
Pavitar
Yes, as Matt says, it is in the language spec. I'm pretty sure you can count on it being 1.
Ed Swangren
It's polite to answer questions in the language they're asked in, surely? C++ != C.
ijw