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",s izeof(*p));
return 0;
} 9 2519
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
In article <11************ **********@l39g 2000cwd.googleg roups.com>,
Tom St Denis <to********@gma il.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.
In article <11************ **********@j44g 2000cwa.googleg roups.com>,
onkar <on*******@gmai l.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",s izeof(*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
"onkar" <on*******@gmai l.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",s izeof(*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_Keit h) 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.
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*******@gmai l.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",s izeof(*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_Keit h) 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.
"onkar" <on*******@gmai l.comwrites:
Keith Thompson wrote:
>"onkar" <on*******@gmai l.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",s izeof(*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 implementation s 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_Keit h) 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.
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*******@gmai l.comwrites:
....
int (*p)[MAXCOL];
....
printf("%d\n",s izeof(*p));
....
*p is of type int[MAXCOL], or int[4], so sizeof(*p) is 4*sizeof(int).
What justification is needed?
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>
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",s izeof(*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;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the first 8-bits for
example. For me, I think it would be best if I can declare the 32-bits
like this:
unsigned char bits;
|
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
Condition_String2 allocates the 72 bytes,
I am not able to get the Artimatic Numbers to staisfy the above
Number of bytes allocated,
|
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 raw MMX
and SSE power, the starting pointer MUST be starting at a 16 byte memory
boundary.
In C++ I allocate more memory than needed, and in a second phase I search
for the address that starts on a 16 byte boundary. And I then use that new...
|
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
|
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 write them to
this second file. However, when the second file is 15360 bytes long,
the program dies with a "Segmentation Fault (core dumped)" error!
I checked with gdb, and it says the last function to run was memcpy()
from libc, which would...
| |
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 return of a
malloc(0).
Does anyone know of a runtime where realloc() free'ed the object and
then returned NULL? If so, it would make the following idiom for
realloc() exploitable. Here's the idiom, snagged from an openbsd man page:
if ((p2 =...
|
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 block of memory. So, For every additional chunk,
even if it is only one byte long, these 8 bytes are required again, in
addition to the 1 byte actually needed to store the chunk itself.
So, there should be wastage of memory for managing the...
|
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
--------------------
|
by: sh.vipin |
last post by:
is there any way to find out number of bytes freed on a particular
free() call in C
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |