-2

What is purpose of using sizeof using malloc in C? Why its necessary?

I mean we can use sizeof but we already know the size(=1000 bytes) what we are allocating in memory?

I am beginner in C so. This might be a very obvious question.

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
Deepak
  • 1,503
  • 3
  • 22
  • 45
  • `mallocated` appears to have absolutely nothing to do with what you just `malloc()`ed, so what's your question? – Crowman Feb 27 '14 at 18:51
  • this is one of the question in previous tests I attempted so just wanted to know what is the right answer? – Deepak Feb 27 '14 at 18:52
  • 1
    It's impossible to tell what your question is, based on what you posted. – Crowman Feb 27 '14 at 18:53
  • 1
    `shunya` and `ohm` involved in question-answer. Nice combination. – ajay Feb 27 '14 at 18:53
  • `sizeof(size_of_this)` will not be `1000`, here. It'll be whatever size pointers are on your system, likely either 4 or 8 bytes. – Crowman Feb 27 '14 at 18:56
  • @PaulGriffiths He simply wants to know what's the benefit of using `sizeof` instead of literal size. – m0skit0 Feb 27 '14 at 18:57
  • sorry I realised there was error in question I just edited it now – Deepak Feb 27 '14 at 18:58
  • @m0skit0: Not if the code he posted has anything to do with his question. None of the answers here seem to have anything to do with this question, although his question is spectacularly unclear. – Crowman Feb 27 '14 at 18:59
  • Edited code makes even less sense... – m0skit0 Feb 27 '14 at 18:59
  • ok the question was just without code I was trying to explain it with some code so it will be clear but I think I made it more confusing. so I just removed the code part. – Deepak Feb 27 '14 at 19:01

3 Answers3

2

DataTypes And Memory Matter

The answer is in the data types. While you know how many objects you want to allocate memory for, you may not want to go through the trouble of mentally computing their size.

So Does Maintainability

Also, what if the composition of these changes later? Then you're screwed and have to edit everything.

So that's irrelevant in your example, without context. But in the case where you want to allocate memory for more complex things (say, a struct with a number of fields), it does become quite important and useful.

Example 1 (not so important here):

char *s = malloc(100 * sizeof(char));

But hey, it's just chars, it's fine, you could have done that:

char *s = malloc(100);

Generally works. But shooting yourself in the foot, as you'll see below.

Example 2 (matters a bit already):

int *i = malloc(100 * sizeof(int));

Sure, you could have written that:

int *i = malloc(100 * 4);

That is, assuming you develop for just one architecture you know pretty well.

Example 3 (starting to matter quite a bit!):

typedef struct s_listelement{
    int                  dataitem;
    struct s_listelement *link;
}              t_listelement;

t_listement *le = malloc(sizeof(t_listelement));

Now imagine that linked-list item's structure contains a bunch of other fields...

Would you want to trust sizeof(), or go have fun and do the math yourself?

haylem
  • 22,460
  • 3
  • 67
  • 96
  • 1
    Make sure, that you [do not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – pzaenger Feb 27 '14 at 19:04
  • The use of an anonymous `struct` as a list node is also asking for trouble, you're going to get incompatible pointer warnings all over the place doing it this way. – Crowman Feb 27 '14 at 19:05
  • @pzaenger: true, actually I wouldn't. I was just using his example and one I grabbed off from www.cs.cf.ac.uk/Dave/C/node11.html. – haylem Feb 27 '14 at 19:05
  • @PaulGriffiths: same as for the other one. Darn, i should have been less lazy :) – haylem Feb 27 '14 at 19:06
  • @pzaenger, PaullGriffits: thanks to both for keeping me on my toes. – haylem Feb 27 '14 at 19:09
0

Suppose you want to allocate memory for storing ints. Your initial guess will be, okay I need to have n integers and each integer requires x bytes. So space will be n*x bytes.

But actually this x depends on architecture. It maybe 16 bit, 32 bit, etc. So to make your code able to work in different environments, it is recommended to use n*sizeof(int) instead of n*x.

For example, if you need to allocate space for 10 integers:

int *p=malloc(sizeof(int)*10);
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
0

What happens if you change the size of mallocated?

You will have to 1) calculate again the new size (letting aside multiple target architectures problems, where you will have to calculate the new size in each target), 2) search for all malloc(1000), make sure it refers to mallocated (which might be not obvious) and replace with new size.

You don't have any of these 2 problems using sizeof. Thus using sizeof leads to a more maintainable and readable code.

m0skit0
  • 25,268
  • 11
  • 79
  • 127