473,769 Members | 7,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof

Hi,
How can we write a function, which functionality is similar to sizeof
function
any one send me source code
Reddy

Jun 18 '06
90 8474
Richard Heathfield <in*****@invali d.invalid> writes:
Malcolm said:
"Richard Heathfield" <in*****@invali d.invalid> wrote
There is simply no way that the compiler can know at compile time how
many elements the user will ask for at run time.

You mean to say that your compiler won't figure out that
999999999999999 999999999999999 9 is the maximum the user can enter and
allocate an array accordingly?


If you could store the value of a single bit on a hydrogen atom, and if you
managed to jam the atoms so close together that there were no inter-atomic
gaps (and somehow doing this without impairing your ability to read and
write the bits), 1e32 bytes of storage would require a memory unit 700
metres long, 700 metres wide, and 700 metres tall. For comparison, the
world's tallest building, the Taipei 101 Tower, is a mere 509 metres high.

The volume of this memory unit would be over 340 million cubic metres. For
comparison, the world's largest building (by usable volume), Boeing's
Everett Plant, near Seattle, is a mere 5.64 million cubic metres.

The memory unit would have a mass of 1.33 million tonnes, which is
considerably in excess of the mass of the Palace of the Parliament in
Romania, the world's most massive building.

That's what I call Big Iron.


And it still wouldn't help, since in the program (lost in the depths
of followup snippage), the sizeof operator when applied to the VLA
must yield the actual number of bytes requested by the user at run
time.

--
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.
Jun 20 '06 #31
Richard Heathfield wrote:
If you could store the value of a single bit on a hydrogen atom, and if you
managed to jam the atoms so close together that there were no inter-atomic
gaps (and somehow doing this without impairing your ability to read and
write the bits), 1e32 bytes of storage would require a memory unit 700
metres long, 700 metres wide, and 700 metres tall. For comparison, the
world's tallest building, the Taipei 101 Tower, is a mere 509 metres high.

The volume of this memory unit would be over 340 million cubic metres. For
comparison, the world's largest building (by usable volume), Boeing's
Everett Plant, near Seattle, is a mere 5.64 million cubic metres.

The memory unit would have a mass of 1.33 million tonnes, which is
considerably in excess of the mass of the Palace of the Parliament in
Romania, the world's most massive building.

That's what I call Big Iron.


Your chemistry isn't so good, then ...

Recycling used memory might start a flame war.

--
Chris "H to Fe ... no, hang on, H to /H/e" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Jun 20 '06 #32
Andrew Poelstra wrote:

On 2006-06-19, Malcolm <re*******@btin ternet.com> wrote:

[...]
puts("How many array elements would you like?");
char buf[32];
if(fgets(buf, sizeof buf, stdin) != NULL)
{
unsigned long n = strtoul(buf, NULL, 10);
if(n > 0)
{
int arr[n];
/* ... */

There is simply no way that the compiler can know at compile time
how many elements the user will ask for at run time.

You mean to say that your compiler won't figure out that
9,999,999,999,9 99,999,999,999, 999,999,999 is the maximum the user can
enter and allocate an array accordingly?


I've never seen a system with that much memory. Even if it was allocating
an array of char, that's still almost 10 million million million terabytes.


(BTW, does the Standard say where variable-length arrays are stored?
Can the compiler do the equivalent of malloc/free to handle it?)

I've seen systems which can "allocate" memory without actually using
any memory until something is written to it. As long as you have at
least 103 bits of address space, one could "allocate" that much memory
on such a system, as long as you didn't try to write to all of it.
(Just as I was able to create a billion-byte file on a 1MB floppy,
many years ago.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 20 '06 #33
Kenneth Brody <ke******@spamc op.net> writes:
[...]
(BTW, does the Standard say where variable-length arrays are stored?
No more than it says where anything is stored. VLAs are like any
other auto objects; they're created at the point of declaration, and
cease to exist at the end of their scope.
Can the compiler do the equivalent of malloc/free to handle it?)
Sure.
I've seen systems which can "allocate" memory without actually using
any memory until something is written to it. As long as you have at
least 103 bits of address space, one could "allocate" that much memory
on such a system, as long as you didn't try to write to all of it.
There's some debate about whether such systems are conforming.
(Just as I was able to create a billion-byte file on a 1MB floppy,
many years ago.)


<OT>Some filesystems optimize long sequences of 0 bytes (which can
have surprising results when you copy a file using a mechanism that
doesn't recognize the optimization).</OT>

--
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.
Jun 20 '06 #34
Keith Thompson wrote:

Kenneth Brody <ke******@spamc op.net> writes: [...]
I've seen systems which can "allocate" memory without actually using
any memory until something is written to it. As long as you have at
least 103 bits of address space, one could "allocate" that much memory
on such a system, as long as you didn't try to write to all of it.


There's some debate about whether such systems are conforming.


What would be non-conforming about it? (My guess is that, given
this scenario, it's possible that malloc succeeds [ie: returns a
non-NULL value], but the program fails later when it access this
memory and the O/S discovers "oops, there's really not enough
virtual memory available to allocate".)
(Just as I was able to create a billion-byte file on a 1MB floppy,
many years ago.)


<OT>Some filesystems optimize long sequences of 0 bytes (which can
have surprising results when you copy a file using a mechanism that
doesn't recognize the optimization).


In this case, it's "sparse allocation". That is, it allocates space
on the disk for the sector containing the billionth byte of the file
(along with necessary overhead), without allocating physical disk
space for the intervening sectors. Writing intervening bytes, even
if they're all zeros, would have allocated physical space, AFAIK.

I (briefly) considered this for a method of copy protection, by
writing critical data throughout this "billion byte file", which
you couldn't copy via standard copy methods.
</OT>

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 20 '06 #35
Kenneth Brody <ke******@spamc op.net> writes:
Keith Thompson wrote:

Kenneth Brody <ke******@spamc op.net> writes:

[...]
> I've seen systems which can "allocate" memory without actually using
> any memory until something is written to it. As long as you have at
> least 103 bits of address space, one could "allocate" that much memory
> on such a system, as long as you didn't try to write to all of it.


There's some debate about whether such systems are conforming.


What would be non-conforming about it? (My guess is that, given
this scenario, it's possible that malloc succeeds [ie: returns a
non-NULL value], but the program fails later when it access this
memory and the O/S discovers "oops, there's really not enough
virtual memory available to allocate".)


malloc() is supposed to "allocate space for an object" or report (by
returning a null pointer) that it's unable to do so. If it returns a
non-null pointer to memory that the program won't be able to access,
it arguably didn't really allocate it.

I'll let someone else make the argument that this behavior is
conforming.

And as a practical matter, if malloc() had visibly failed, the program
could have dealt with the error; if malloc() seems to succeed, but
some later attempt to access memory blows up, there's no way to
recover.

<OT>
Note that the program that did the allocation isn't necessarily the
one that's going to be killed. If the system decides that it's
running short on resources, it might kill processes, perhaps at
random, until the remaining processes have enough resources.
</OT>

There's something to be said for allowing this kind of
over-allocation. It can improve system efficiency if programs
commonly allocate more memory than they actually use (similar to
over-booking of airline seats). I wouldn't mind having two different
allocation functions, one that says "I want this much memory; either
guarantee that I can access it, or fail", and another that says, "I'd
like this much memory; tell me you've allocated it, but it's ok to
kill me if I try to use it". I suggest calling the former function
"malloc". You can call the latter function anything you like; I won't
be using it.

--
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.
Jun 20 '06 #36
Keith Thompson wrote:
Kenneth Brody <ke******@spamc op.net> writes:
[...]
(BTW, does the Standard say where variable-length arrays are stored?
Can the compiler do the equivalent of malloc/free to handle it?)


Sure.


this would make it impossible to use such constructs in a signal
handler (or other reentrant context).

Jun 20 '06 #37
"tedu" <tu@zeitbombe.o rg> writes:
Keith Thompson wrote:
Kenneth Brody <ke******@spamc op.net> writes:
[...]
> (BTW, does the Standard say where variable-length arrays are stored?
> Can the compiler do the equivalent of malloc/free to handle it?)


Sure.


this would make it impossible to use such constructs in a signal
handler (or other reentrant context).


Not if the implementation gets it right.

In a typical stack-based implementation, automatic objects are
allocated by advancing the stack pointer, and deallocated by setting
it back. The implementation has to do this correctly in the presence
of signal handlers.

Likewise, if VLAs are allocated by malloc, the implementation has to
free them at the right time. This might be more difficult, but I know
of no reason why it wouldn't be possible.

--
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.
Jun 21 '06 #38
Kenneth Brody <ke******@spamc op.net> wrote:
Keith Thompson wrote:

Kenneth Brody <ke******@spamc op.net> writes:

[...]
I've seen systems which can "allocate" memory without actually using
any memory until something is written to it. As long as you have at
least 103 bits of address space, one could "allocate" that much memory
on such a system, as long as you didn't try to write to all of it.


There's some debate about whether such systems are conforming.


What would be non-conforming about it? (My guess is that, given
this scenario, it's possible that malloc succeeds [ie: returns a
non-NULL value], but the program fails later when it access this
memory and the O/S discovers "oops, there's really not enough
virtual memory available to allocate".)


Yes. And this is anathema to solid programming and safe computer
systems.

Richard
Jun 21 '06 #39
Ben C <sp******@spam. eggs> wrote:
On 2006-06-21, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Kenneth Brody <ke******@spamc op.net> wrote:
What would be non-conforming about it? (My guess is that, given
this scenario, it's possible that malloc succeeds [ie: returns a
non-NULL value], but the program fails later when it access this
memory and the O/S discovers "oops, there's really not enough
virtual memory available to allocate".)


Yes. And this is anathema to solid programming and safe computer
systems.


Nevertheless I believe this is what everyday GNU/Linux actually does--
apparently gives you the block then hits you with a SIGSEGV later when
you try to use it.

A bit like when you go to the bank to withdraw your money and they say,
sorry you can't have it because we've already lent it to other people,
four times over (real banks do not use the "Banker's Algorithm").


Tuppence? *Tuppence*?

Richard
Jun 21 '06 #40

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

Similar topics

3
3556
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2469
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
9
3025
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1939
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
2415
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2538
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
4
292
by: cs | last post by:
#include <stdio.h> #include <stdlib.h> typedef struct{ unsigned a; unsigned *b; unsigned k; }tp; tp e, *ee;
5
2900
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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,...
0
10214
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, 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...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
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...
0
6674
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();...
0
5304
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.