Bytes allocated by the code ??

O

onkar

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;
}
 
T

Tom St Denis

onkar said:
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);

Don't cast malloc (5th time today someone posted that...)

also

cdecl> explain int (*p)[]
declare p as pointer to array of int

p is a pointer to an array of MAXCOL ints. It isn't an array of
pointers [as you likely wanted].

Tom
 
K

Kenny McCormack

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);

Don't cast malloc (5th time today someone posted that...)

also

etc, etc...

(On the futility of getting information in clc)
Q: How many miles is it to Denver?
A: You need to paint your car.
 
W

Walter Roberson

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 required to hold an int or a pointer differ between
platforms, so there isn't just one true correct numeric answer to your
question. Worse yet, different pointers can be different sizes (with
some restrictions) on the same platform -- all that is promised about
pointers to different types is that a void* pointer is the same size as
an unsigned char* pointer.

We could give you a symbolic answer, but not a numeric one.
 
K

Keith Thompson

onkar said:
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/>.
 
O

onkar

it is giving me 16 on Linux (using gcc compiler ) on 32 bit m/c.
Sir, Can u please justify it now ??



regards,
Onkar

Keith said:
onkar said:
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/>.
 
K

Keith Thompson

onkar said:
Keith said:
onkar said:
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/>.

it is giving me 16 on Linux (using gcc compiler ) on 32 bit m/c.
Sir, Can u please justify it now ??

Please don't top-post. Read the following:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php
I've corrected it here.

It's rarely necessary to quote the entire article to which you're
replying. Trim anything that's not relevant. In particular, don't
quote signatures unless you're commenting on them.

Don't use silly abbreviations like "u" for "you", or "m/c" for
whatever "m/c" stands for. Take the time to spell out words so we
don't have to guess what you mean.

When you say it's giving you 16, do you mean that that's the output of
the program? If so, that makes sense. p is a pointer to an array of
4 ints, so sizeof(*p) is 4*sizeof(int). Apparently sizeof(int) is 4
on your system.

As I wrote above, this is not the number of bytes allocated by
malloc(). And there are still a number of problems with the code.
 
C

CBFalconer

onkar said:
it is giving me 16 on Linux (using gcc compiler ) on 32 bit m/c.
Sir, Can u please justify it now ??

u is busy on other projects. It is undefined. Don't top-post.
 
M

mark_bluemel

onkar said:
how many bytes will be allocted by following code -

Onkar's code with some additions, corrections and comments below,
unquoted so it could be cut and pasted easily. It would be interesting
to know what Onkar was actually trying to achieve...

#include<stdio.h>
#include<stdlib.h> /* if you use malloc() include
this */
#define MAXROW 3
#define MAXCOL 4
int main(int argc,char **argv){
int (*p)[MAXCOL];
/* p is a pointer to an array of 4 ints */
p=malloc(sizeof(*p)*MAXROW);
/* p now points to the first of 3 arrays of 4 ints */
/* as others say, don't cast malloc()'s result -
* it can prove dangerous, and hard to debug
* when you miss the prototype for malloc()
* e.g. on a system where void * is 64-bit and int is
32-bit...
*/
printf("Number of bytes allocated %d\n",
(int)sizeof(*p)*MAXROW);
/* on my 32-bit linux system this reports 48
* which seems reasonable
*/
printf("%d\n",sizeof(*p));
/* p is still defined as a pointer to an array of 4 ints,
* so *p is 4 ints long
* on my system this reports 16
*/
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top