Struct size

A

Army1987

Eric Sosman said:
[snip] "What's the most elegant and
return n & 1;

How do you explain what & does without using the notion of 'two'?
:)
A better way:
n is even: ∃x ∈ ℤ ∣ x + x = n
n is odd: n ∈ ℤ ⋀ ¬(n is even)

(Yes, there are two x's in the sum above, but this doesn't use the
notion of 'two' anymore than the sentence "I am happy." uses the
notion of 'three' because it has three words, right?)
 
A

Army1987

Jeff said:
Can anyone tell me the best way to find out the size of a struct, without
using the sizeof function? [...]

* JEFF P. SYVERSON, PHD *
* Senior Systems Engineer *
[... twenty-four lines of boilerplate all told]
OMG. I had not even noticed it. The last line in my screenful was
the blank line below -Jeff, and I didn't even consider scrolling
down.
 
R

Richard Heathfield

Army1987 said:
Eric Sosman said:
[snip] "What's the most elegant and
return n & 1;

How do you explain what & does without using the notion of 'two'?
:)

Yields the result of performing a bitwise AND of all its operands.
A better way:
n is even: ?x ? ? ? x + x = n
n is odd: n ? ? ? ¬(n is even)

(Yes, there are two x's in the sum above, but this doesn't use the
notion of 'two' anymore than the sentence "I am happy." uses the
notion of 'three' because it has three words, right?)

For the same reason, return n & 1; doesn't use the notion of "three".
 
K

Kenneth Brody

Eric said:
Can anyone tell me the best way to find out the size of a struct, without
using the sizeof function? [...]

* JEFF P. SYVERSON, PHD *
* Senior Systems Engineer *
[... twenty-four lines of boilerplate all told]

A PhD asks (relays, more likely) this silly question?
A Senior Systems Engineer thinks sizeof is a function?
Jeee-zus!
[...]

1) His PhD may not be computer related.
2) His "PHD" may not be a PhD in the first place.
3) His "Senior Systems Engineer" title may mean that he builds
systems unrelated to computers.

Of course, the question "how do I get the size of something without
using the made-for-this sizeof operator" is along the same lines as
"how do I get the weight of something without using a scale". Sure,
it can be done, but why?

I don't believe any of the numerous posters who have asked this same
question have come back with an answer to "why do you want to?"

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

Eric said:
Richard wrote: [...]
And clearly he has an ulterior
motive for not wanting to use sizeof. I suspect home work for an
"impprovement" course.

Ayeh. One of the improvements should be the ability to
recognize and reject stupid requirements (another mark of
seniority and experience). "What's the most elegant and
efficient way to distinguish even numbers from odds without
using the notion of `two'?"

But first, define "even" and "odd" without using the notion of "two".

Then, define "prime" without using the notion of "division".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Tobin

Kenneth Brody said:
But first, define "even" and "odd" without using the notion of "two".

Easily done, recursively.
Then, define "prime" without using the notion of "division".

Doesn't the usual definition not use division? A natural number is
prime if it is not the product of two natural other than itself and
one.

-- Richard
 
R

Richard Heathfield

Kenneth Brody said:
Eric said:
Richard wrote: [...]
And clearly he has an ulterior
motive for not wanting to use sizeof. I suspect home work for an
"impprovement" course.

Ayeh. One of the improvements should be the ability to
recognize and reject stupid requirements (another mark of
seniority and experience). "What's the most elegant and
efficient way to distinguish even numbers from odds without
using the notion of `two'?"

But first, define "even" and "odd" without using the notion of "two".

F(0) yields even. For n > 0, F(n) yields odd iff F(n - 1) yields even.
Otherwise, it yields odd.

#define EVEN 0
#define ODD 1
int F(unsigned long n)
{
int result = EVEN;
if(n > 0)
{
result = F(n - 1);
}
return result;
}
Then, define "prime" without using the notion of "division".

A prime is a positive integer greater than 1 that is not a multiple of any
other positive integer other than itself and 1.
 
R

Richard Heathfield

Richard Heathfield said:
Kenneth Brody said:


F(0) yields even. For n > 0, F(n) yields odd iff F(n - 1) yields even.
Otherwise, it yields odd.

#define EVEN 0
#define ODD 1
int F(unsigned long n)
{
int result = EVEN;
if(n > 0)
{
result = F(n - 1);
}
return result;
}

Stupid. Sorry. All numbers are even!

Let me try that again:

#define EVEN 0
#define ODD 1
int F(unsigned long n)
{
int result = EVEN;
if(n > 0)
{
if(F(n - 1) == EVEN)
{
result = ODD;
}
else
{
result = EVEN;
}
}
return result;
}
 
U

user923005

Jeff said:
Can anyone tell me the best way to find out the size of a struct, without
using the sizeof function? [...]
* JEFF P. SYVERSON, PHD *
* Senior Systems Engineer *
[... twenty-four lines of boilerplate all told]

A PhD asks (relays, more likely) this silly question?
A Senior Systems Engineer thinks sizeof is a function?
Jeee-zus!

In my day, people got PhD's only if they were smart and/or
had some ability to conduct research; JEFF P. SYVERSON, PHD is
apparently unable to find Google. In my day, Senior Systems
Engineers were people of experience, battle-scarred and wise in
the ways of many languages and disciplines; JEFF P. SYVERSON, PHD
has attained the title without acquiring any knowledge of C, not
even the knowledge that would come from a first-level textbook.

Just use sizeof, Jeff. That's what it's for. Jeee-zus!

I guess that his PhD is not in the C language.
 
U

user923005

Eric said:
Richard wrote: [...]
And clearly he has an ulterior
motive for not wanting to use sizeof. I suspect home work for an
"impprovement" course.
Ayeh. One of the improvements should be the ability to
recognize and reject stupid requirements (another mark of
seniority and experience). "What's the most elegant and
efficient way to distinguish even numbers from odds without
using the notion of `two'?"
But first, define "even" and "odd" without using the notion of "two".

'Even' is when a value (converted to unsigned) is shifted right by 1
and then shifted left by 1 and remains unchanged.
Then, define "prime" without using the notion of "division".

Easy. Replace 'division' with 'repeated subtraction that yields 0
before going negative'.
 
R

Richard Tobin

But first, define "even" and "odd" without using the notion of "two".
[/QUOTE]
'Even' is when a value (converted to unsigned) is shifted right by 1
and then shifted left by 1 and remains unchanged.

This assumes the notion of shifting which is usually explained with
reference to base TWO, so I think you still have some work left.

-- Richard
 
U

user923005

This assumes the notion of shifting which is usually explained with
reference to base TWO, so I think you still have some work left.

The C language may have used the notion of 2 in implementation of the
semantics of unsigned shifts, but I didn't.
 
C

CBFalconer

Kenneth said:
.... snip ...

But first, define "even" and "odd" without using the notion of
"two". Then, define "prime" without using the notion of "division".

A number that cannot be duplicated by any multiple of any smaller
number greater or equal to two. But division is more efficient.
:)
 
C

Charlie Gordon

Kenneth Brody said:
Eric said:
Can anyone tell me the best way to find out the size of a struct,
without
using the sizeof function? [...]

Of course, the question "how do I get the size of something without
using the made-for-this sizeof operator" is along the same lines as
"how do I get the weight of something without using a scale". Sure,
it can be done, but why?

I suspect the OP wants to avoid the overhead of what he assumes to be a
function call. The answer is simple: for the purpose of computing the size
of a structure, sizeof is an operator evaluated at compile time.

Writing size=sizeof(struct foo); is exactly as efficient as writing size=16;

As a matter of fact, his proposal size=(char*)&foo_array[1]-(char*)&foo[0]
may well generate the same code, or something more complicated with a
subtraction and 2 relocations, to ultimately produce the same result.
 
R

Richard Tobin

Charlie Gordon said:
I suspect the OP wants to avoid the overhead of what he assumes to be a
function call.

Given that the OP posted through a public news server with a
disclaimer referring to a company not (as far as I can see) mentioned
anywhere else on the Internet, my guess is that he is a troll.

-- Richard
 
R

Richard Tobin

Given that the OP posted through a public news server with a
disclaimer referring to a company not (as far as I can see) mentioned
anywhere else on the Internet, my guess is that he is a troll.

A bit more searching reveals that a company with that name does exist,
and is an Indian outsourcing company. So I revise my guess to be that
he's doing that same Indian C course which has led so many other
people to ask the same question here recently.

-- Richard
 
R

Richard Heathfield

Richard Tobin said:
A bit more searching reveals that a company with that name does exist,
and is an Indian outsourcing company. So I revise my guess to be that
he's doing that same Indian C course which has led so many other
people to ask the same question here recently.

I wonder whether it's possible to killfile an entire sub-continent.
 
C

Charlie Gordon

Richard Tobin said:
A bit more searching reveals that a company with that name does exist,
and is an Indian outsourcing company. So I revise my guess to be that
he's doing that same Indian C course which has led so many other
people to ask the same question here recently.

So it was a beg indian problem.
 

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

Similar Threads

variadic function 27
soap library for python-3.x 0
SAP MM Cupertino, CA 3
Python IDLE 2
Zero-size array as struct member 160
Contact details for Eric Sosman 18
Reading struct with structs 1
Problems with os.walk 1

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top