473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bytes allocated by the code ??

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

Nov 28 '06 #1
9 2501

onkar wrote:
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

cdeclexplain 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

Nov 28 '06 #2
In article <11**********************@l39g2000cwd.googlegroups .com>,
Tom St Denis <to********@gmail.comwrote:
>
onkar wrote:
>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.
Nov 28 '06 #3
In article <11**********************@j44g2000cwa.googlegroups .com>,
onkar <on*******@gmail.comwrote:
>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.
--
All is vanity. -- Ecclesiastes
Nov 28 '06 #4
"onkar" <on*******@gmail.comwrites:
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) ks***@mib.org <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.
Nov 28 '06 #5
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 Thompson wrote:
"onkar" <on*******@gmail.comwrites:
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) ks***@mib.org <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.
Nov 29 '06 #6
"onkar" <on*******@gmail.comwrites:
Keith Thompson wrote:
>"onkar" <on*******@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 29 '06 #7

onkar wrote:
it is giving me 16 on Linux (using gcc compiler ) on 32 bit m/c.
Sir, Can u please justify it now ??
1) Please don't top-post.
Keith Thompson wrote:
"onkar" <on*******@gmail.comwrites:
....
int (*p)[MAXCOL];
....
printf("%d\n",sizeof(*p));
....
*p is of type int[MAXCOL], or int[4], so sizeof(*p) is 4*sizeof(int).
What justification is needed?

Nov 29 '06 #8
onkar wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 29 '06 #9

onkar wrote:
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;
}

Nov 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
3
by: Sakharam Phapale | last post by:
Hi All, How to get the no of memory bytes allocated to the Array of type String? Dim arrDetail(100,10) As String Thanks and Regards Sakharam Phapale
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
64
by: Robert Seacord | last post by:
The C standard doesn't say anything about what happens when you call realloc with a size argument of 0. Both glibc and openbsd appear to return a valid pointer to a zero-sized object.. e.g. the...
14
by: karthikbalaguru | last post by:
Hi, In the case of heap , to keep track of a single chunk of memory it requires 8 bytes of information. That is, it requires 4 bytes to hold the size, and 4 bytes to hold the pointer to the next...
0
by: George2 | last post by:
Hello everyone, I am not sure whether I am wrong or the Windows Internals Book 4th version is wrong. Here is what the book says in Chapter 7, Memory Management from Page 444 to Page 445 ...
58
by: sh.vipin | last post by:
is there any way to find out number of bytes freed on a particular free() call in C
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.