sizeof dataTypes at run time

C

Chris Dollin

Ben said:
It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!


[1] Apart from C99's variable-length arrays: are there variable-length
types to go with them?

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Meaning precedes definition.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
R

Richard Heathfield

Chris Dollin said:
Ben said:
It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrug> So use an interpreter. That way, there isn't a compile time.
 
C

Chris Dollin

Richard said:
Chris Dollin said:
Ben said:
we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrug> So use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

Using an interpreter is a detail outside the scope of the standard.

PS We both think the answer is "use sizeof, that's what it's for!".

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
"Go not to the Elves for counsel, for they will answer both no and yes."/tLotR/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
R

Richard Heathfield

Chris Dollin said:
Richard said:
Chris Dollin said:
Ben Pfaff wrote:


we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrug> So use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

But they can still both be at runtime, which is all the question
requires.
 
C

Chris Dollin

Richard said:
Chris Dollin said:
Richard said:
Chris Dollin said:

Ben Pfaff wrote:


we can never use sizeof(double) or something like this.

It's always funny when people who don't know C try to instruct
others on how to use it.

We can't use `sizeof (type)` to calculate the size of that type at
run time (as the OP was requesting), because `sizeof` is calculated
at compile-time [1]!

<shrug> So use an interpreter. That way, there isn't a compile time.

The Standard distinguishes the time-of-evaluation-of-sizeof and the
time-of-evaluation-of-non-constant-expressions, doesn't it?

But they can still both be at runtime, which is all the question
requires.

Compile-time is when sizeof happens. The /interpreter's/ runtime isn't
visible to a C program.

Urm: equine, deceased, selling/whipping?

--
JUC 2007, submit: http://hpl.hp.com/conferences/juc2007/submission.html
"You're not supposed to /think/ about it, you're supposed to say NO!"
Jill Swinburn, /The Beiderbeck Connection/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
 
R

Richard Heathfield

Chris Dollin said:

Urm: equine, deceased, selling/whipping?

<sequitur relevance="non">
I have an interesting new analogy for why casting is almost always a bad
idea - based on a true story.
</sequitur>
 
C

CBFalconer

.... snip ...

its possible
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.

No it won't, it involves undefined behaviour, even if you did
#include <stdio.h>. The type of a pointer subtraction is not int.
When you lie to a variadic function about the type of parameters
all rules are cancelled, and you may launch WWIII. In addition

printf("%d\n", (int)sizeof(int));

is considerable shorter and more understandable, besides being
defined. From N869:

6.5.3.4 The sizeof operator

Constraints

[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an expression
that designates a bit-field member.

Semantics

.... snip ...

[#4] The value of the result is implementation-defined, and
its type (an unsigned integer type) is size_t, defined in
the <stddef.h> header.

....

6.5.6 Additive operators

.... snip ...

[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements. The size of the
result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.h> header.
If the result is not representable in an object of that
type, the behavior is undefined. In other words, if the
expressions P and Q point to, respectively, the i-th and j-
th elements of an array object, the expression (P)-(Q) has
the value i-j provided the value fits in an object of type
ptrdiff_t. Moreover, if the expression P points either to
an element of an array object or one past the last element
of an array object, and the expression Q points to the last
element of the same array object, the expression ((Q)+1)-(P)
has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and
has the value zero if the expression P points one past the
last element of the array object, even though the expression
(Q)+1 does not point to an element of the array object.79)
 
C

CBFalconer

ramana said:
.... snip ...

I am new to posting to the groups. So i don't know the rules/practices
to follow. Please don't hesitate to tell me any that sort of stuff.

Read the following links:

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
 
A

Army1987

Raman said:
Is ut possible to calculate size of any standard data types at run
time i.e without using sizeof() operator

Try this:
Write a file containing the line:

#define REINVENTING_THE_WHEEL sizeof

and save it as newwheel.h.

Then use this:

#include <stdio.h>
#include "newwheel.h"
int main(void)
{
printf("%d\n", (int)REINVENTING_THE_WHEEL(int));
return 0;
}
 
I

ilan pillemer

I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."

I was doing this excerise recently as I am new to C and have been
learning from K&R2. I do not know if the technique I used was the
best.

But this is what I did. I set the bit values of the basic types to ~0.
For unsigned basic types this gave me the range. And signed the
maximum negative value. I could also determine thus by a comparison if
2's complement or 1's complement was being used. I thus had my answer
through direct computation.

I have not yet attempted to the floating-point excercise.
 
B

Ben Pfaff

ilan pillemer said:
But this is what I did. I set the bit values of the basic types to ~0.
For unsigned basic types this gave me the range.

It won't necessarily work for unsigned integer types wider than
int. For these you'll need to either use the proper type for 0,
as in e.g. ~0UL. Or you can just assign -1 to an object of the
unsigned integer type, which doesn't require such additional
care.

By the way, it's best to think of C objects as having values, not
representations or "bit values". Most operations in C operate on
values, not on representations.
And signed the maximum negative value. I could also determine
thus by a comparison if 2's complement or 1's complement was
being used. I thus had my answer through direct computation.

I think that ~0 can actually yield a trap representation.
 
K

Keith Thompson

ilan pillemer said:
I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)
 
I

ilan pillemer

Keith Thompson said:
ilan pillemer said:
I believe this is an excercise in K&R2,p 36.

"Exercise 2-1. Write a program to determine the ranges of char,
short, int and long variables, both signed and unsigned, by printing
appropriate values from standard headers and by direct
computation. Harder if you compute them: determine the ranges of
various floating point types."
[...]

The exercise computes the *ranges* of the types (e.g., 0..65535), not
their sizes (e.g., 2 bytes or 16 bits).

(These are examples; I'm not assuming that a byte is 8 bits.)


...hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?
 
R

Richard Heathfield

ilan pillemer said:
..hmm.. Would I look the at the number of bits in a char to determine
how many bits in a byte?

Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.
If this is a correct I could then fill a unsigned char with bits set
to one and then left shift them until all the bits are zeroed?

Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x > 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.
 
I

ilan pillemer

Richard Heathfield said:
ilan pillemer said:


Yes, because a char is exactly one byte wide. Incidentally, it is also
exactly CHAR_BIT bits wide.


Yes, you could do that, or you could simply do this:

unsigned char x = 1;
int char_bit = 0;
while(x > 0)
{
++char_bit;
x <<= 1;
}

or of course you could just use CHAR_BIT from <limits.h>.

I know this. I am also learning from "Advanced Programmig in the UNIX
environment." On page 27, figure 2.6 Stevens/Rago lists 19 limits that
can be found in <limits.h>. The K&R2 exercise said I should also as an
exercise determine this kind of information by direct
computation. (Excerise 2-1 on page 36). That is why I am trying to do
this without referring to <limits.h>.
 

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

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top