Size of Stucture without sizeof()

R

Robotnik

Hello All,

I want to know if we could know the size of a structyure
without the use of sizeof().
Any hints.
 
S

Skarmander

Robotnik said:
Hello All,

I want to know if we could know the size of a structyure
without the use of sizeof().
Any hints.
No. Nor is that meaningful. That's what sizeof is *for*. Various unportable
tricks suggest themselves as replacements, all of them worthless because
they're unportable tricks when a perfectly portable alternative is
available: sizeof.

If this is a homework assignment, tell the instructor to come up with better
questions.

If you have a legitimate need to know the size of a structure outside of a C
program, you can easily write a small program whose sole purpose it is to
measure a struct and write the outcome somewhere. Note that the result
depends on your platform, your compiler, the options you passed and possibly
the phase of the moon; you need to know what you are doing and why you're
doing it. Interfacing with existing binary interfaces is one possible reason.

S.
 
E

Eric Sosman

Robotnik said:
Hello All,

I want to know if we could know the size of a structyure
without the use of sizeof().
Any hints.

Hint: Get a clue. Would you make a conscious decision
to avoid the `-' operator? Or the `int' keyword? Or all
occurrences of the letter `i'?

As to your question: Yes, there is a way to do it. It
is an utterly silly way, and not worth pursuing. If you are
sufficiently determined, you can learn to sip Sauternes
through your nostrils, but that's not a good use of your
nostrils or of the Sauternes.
 
J

Jack Klein

Hello All,

I want to know if we could know the size of a structyure
without the use of sizeof().
Any hints.

No, "we" can't. I can, in all but the most pathological cases, but
you can't.
 
A

Artie Gold

Jack said:
No, "we" can't. I can, in all but the most pathological cases, but
you can't.
I can only think of one pathological case (the obvious one, where more
than one won't fit; hopefully I'm not giving too much away). Are there
others?

Thanks,.
--ag
 
R

Richard Heathfield

Artie Gold said:
I can only think of one pathological case (the obvious one, where more
than one won't fit; hopefully I'm not giving too much away). Are there
others?

You don't need more than one, even in the obvious case. I refer you to para
4 of the semantics section of C89 draft 3.3.6 paying particular attention
to the sentence which begins at the end of the line beginning "expression".
That, and a bit of explicit con, should see you right.

Is that sufficiently obscure?
 
B

Ben Pfaff

Richard Heathfield said:
You don't need more than one, even in the obvious case. I refer you to para
4 of the semantics section of C89 draft 3.3.6 paying particular attention
to the sentence which begins at the end of the line beginning "expression".
That, and a bit of explicit con, should see you right.

Is that sufficiently obscure?

Yes, but your reasoning is wrong anyway, because the published
standards (both C89 and C99) add a Very Important Paragraph to
that section:

For the purposes of these operators, a pointer to an object
that is not an element of an array behaves the same as a
pointer to the first element of an array of length one with
the type of the object as its element type.
 
R

Richard Heathfield

Ben Pfaff said:
Yes, but your reasoning is wrong anyway, because the published
standards (both C89 and C99) add a Very Important Paragraph to
that section:

For the purposes of these operators, a pointer to an object
that is not an element of an array behaves the same as a
pointer to the first element of an array of length one with
the type of the object as its element type.

Then I was *too* obscure. I was intending to imply that you don't need an
array at all. Or have I missed something? Or should we defer this
discussion until the OP's deadline has probably passed?
 
G

Gordon Burditt

I want to know if we could know the size of a structyure
without the use of sizeof().
Any hints.

The C standard says you can use sizeof().
The C standard overrides homework assignments.

So use sizeof().

Gordon L. Burditt
 
B

Ben Pfaff

Richard Heathfield said:
Then I was *too* obscure. I was intending to imply that you don't need an
array at all. Or have I missed something? Or should we defer this
discussion until the OP's deadline has probably passed?

Now I'm confused, so I vote for the latter.
 
R

Richard Heathfield

Ben Pfaff said:
Now I'm confused, so I vote for the latter.

I finally remembered about email. :) Ben and I are now in agreement over
the correctness or otherwise of this approach.
 
S

Santhosh

All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly. I just want to understand
in what ways the following piece of code can give incorrect result in
some common general purpose programming environments ? (pls, lets
ignore obscure architectures which still supports C)

#include<stdio.h>
int main() {
struct tag {
int a;
char b;
float c;
}tag;

struct tag *ptr = &tag;

printf("%u\n", (char *)(ptr+1) - (char *)ptr);
}
%

thanks,
Santhosh.
 
S

Suman

Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly. [...]
printf("%u\n", (char *)(ptr+1) - (char *)ptr);

Its not silly.Its plain UB.
 
N

Niklas Norrthon

[obscure discussion snipped]
Then I was *too* obscure. I was intending to imply that you don't need an
array at all. Or have I missed something? Or should we defer this
discussion until the OP's deadline has probably passed?

Supposing this is a homework assignment, is there anyone out there
who could give one single reason why an instructor would give such
an assignment to his students? What points could be illustrated
by such hacks? Pointer arithmetics?

/Niklas Norrthon
 
C

Christian Bau

Artie Gold said:
I can only think of one pathological case (the obvious one, where more
than one won't fit; hopefully I'm not giving too much away). Are there
others?

You don't need two. You need one plus one byte.
 
C

Christian Bau

"Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly. I just want to understand
in what ways the following piece of code can give incorrect result in
some common general purpose programming environments ? (pls, lets
ignore obscure architectures which still supports C)

Your code was fine, it is just silly not to use sizeof ().

Now try this struct:

typedef struct {
char a [2000000000];
double d;
} mystruct;

and see what your program does.
 
A

Anand

Suman said:
Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly.
[...]

printf("%u\n", (char *)(ptr+1) - (char *)ptr);


Its not silly.Its plain UB.
Agree on the silly part.
Still wondering about the UB part, can you please clarify?
 
R

Richard Bos

Suman said:
Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly. [...]
printf("%u\n", (char *)(ptr+1) - (char *)ptr);

Its not silly.Its plain UB.

Is it? Yes, if size_t is not an unsigned int, but that's hardly
relevant, is it? If we correct that, how is this UB?

printf("%lu\n", (unsigned long) ((char *)(ptr+1)-(char *)ptr));

Richard
 
C

Christian Bau

Anand said:
Suman said:
Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly.
[...]

printf("%u\n", (char *)(ptr+1) - (char *)ptr);


Its not silly.Its plain UB.
Agree on the silly part.
Still wondering about the UB part, can you please clarify?

If ptrdiff_t != int.
 
K

Krishanu Debnath

Richard said:
Suman said:
Santhosh said:
All the major contributors to clc have said that any approach to do
what OP asked without using sizeof is silly. [...]
printf("%u\n", (char *)(ptr+1) - (char *)ptr);
Its not silly.Its plain UB.

Is it? Yes, if size_t is not an unsigned int, but that's hardly
relevant, is it? If we correct that, how is this UB?

printf("%lu\n", (unsigned long) ((char *)(ptr+1)-(char *)ptr));

Richard

What it has to do with size_t or unsigned type? Pointer subtraction
gives you value of type ptrdiff_t which is signed integer type.

Krishanu
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top