Re: Bytes allocated by the code ??



"onkar" <onkar.n.m@xxxxxxxxx> writes:
how many bytes will be allocted by following code -

#include<stdio.h>
#define MAXROW 3
#define MAXCOL 4
int main(int argc,char **argv){
int (*p)[MAXCOL];
p=(int (*)[MAXCOL])malloc(sizeof(*p)*MAXROW);
printf("%d\n",sizeof(*p));
return 0;
}

The number of bytes allocated by a call to malloc() is simply the
value of the argument passed to malloc(). (That's assuming the
allocation succeeds; if it fails, malloc() returns a null pointer, and
you should *always* check for that.)

*p is of type int[MAXCOL], or int[4], so sizeof(*p) is 4*sizeof(int).
Multiplying by MAXROW, or 3, gives us 12*sizeof(int), so that's the
number of bytes allocated. (We don't know what sizeof(int) is on your
system.)

But there are some problems with your code.

Don't cast the result of malloc(). If you call malloc(), you must
have a "#include <stdlib.h>" to make its declaration visible.
(Casting the result can mask the error message triggered by calling
malloc() with no visible declaration. It's like cutting the wires to
the oil light on your car's dashboard rather than adding oil; either
solution will turn off the warning light, but only one actually fix
the problem.)

printf's "%d" format expects an int argument. sizeof yields a result
of type size_t. To print a size_t value, you can convert it to some
type that printf knows about. For example:

printf("%lu\n", (unsigned long)sizeof *p);

C99 has "%zu", which accepts a size_t value directly, but not all
implementations yet support it.

You may be thinking that the value printed reflects the number of
bytes allocated. It doesn't. If p points to the first element of an
array, sizeof(*p) gives you just the size of that element, not the
size of the entire array.

Arrays and pointers are tricky, and pointers to arrays are seldom
useful. I suggest reading section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Can array[]=malloc()ed?
    ... > int main ... > in the above I have used array but derefered as a pointer since ... > first element in a array I wanted to malloc the array y. ...
    (comp.lang.c)
  • Re: Can array[]=malloc()ed?
    ... >> int main ... >> in the above I have used array but derefered as a pointer since ... >> first element in a array I wanted to malloc the array y. ...
    (comp.lang.c)
  • Re: C++ Allocating & Using Array of struct
    ... >everything I've tried to code the allocation or reference to it either ... >fails to compile or produces segmentation faults due to incorrect ... you demonstrate that you know the return value from malloc is to ... "incomplete" array type; that is, an array of pointers to P_RInfo's ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Which allocator used by malloc
    ... Autoconf tests are fun, haha. ... if k is the size of the malloc control block. ... Here, you are calling malloc with an argument of type int, which is ... That suggests that allocation from size buckets is going on''. ...
    (comp.lang.c)
  • Re: Storing the size of an array in the structure itself
    ... >> I think every C programmer can relate to the frustrations that malloc ... >> the size of an array must be stored separately to be a nightmare. ... > Internally a lot of libraries do this. ... > for memory allocation and deallocation. ...
    (comp.lang.c)