help w/ c/c++ problem

  • Thread starter Heinrich Pumpernickel
  • Start date
H

Heinrich Pumpernickel

28b (macros). write a macro, sizeof(var), that returns the size of
its argument . you are not allowed, to use the sizeof() operator for
this . for extra credit, write another macro, sizeof_t(type), that
returns the size of a type .

given is the following template..........

//////////////////////////////////////////////////////////////////////
// ASS28B.C
//////////////////////////////////////////////////////////////////////

#include <Stdio.H>

#define SIZEOF( var ) // FILL IN MACRO TEXT

void main()
{
char a;
short b;
int c;
long d;
long long e;
struct x* (*f)(struct y*);
union { int a[100]; float b[100]; } g;
void* h;

printf("sizeof a : %i\n", SIZEOF(a));
printf("sizeof b : %i\n", SIZEOF(b));
printf("sizeof c : %i\n", SIZEOF(c));
printf("sizeof d : %i\n", SIZEOF(d));
printf("sizeof e : %i\n", SIZEOF(e));
printf("sizeof f : %i\n", SIZEOF(f));
printf("sizeof g : %i\n", SIZEOF(g));
printf("sizeof h : %i\n", SIZEOF(h));
}

//////////////////////////////////////////////////////////////////////


can u hlp me w/ this ?

tnx !!!
 
R

Richard Heathfield

Heinrich Pumpernickel said:
28b (macros). write a macro, sizeof(var), that returns the size of
its argument .

#define SIZEOF(x) (sizeof(x))
you are not allowed, to use the sizeof() operator for this .

Why not? That's what it's *for*.
given is the following template..........
void main()

If your instructor gave you this template, you need a new instructor. In
C, main returns int.

<snip>
 
H

Heinrich Pumpernickel

Heinrich Pumpernickel said:


#define SIZEOF(x) (sizeof(x))

but i m not allowed to do that
Why not? That's what it's *for*.
well its just an excersise
If your instructor gave you this template, you need a new instructor. In
C, main returns int.

i dont believe that . my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !
 
R

Richard Heathfield

Heinrich Pumpernickel said:
but i m not allowed to do that

well its just an excersise

I hope that the point of the exercise is to demonstrate that there is no
satisfactory, portable solution to the problem, other than to change
the rules and allow use of the sizeof operator.

You can just about do it for objects:

#define SIZEOF(x) ((unsigned char *)(&(x)+1)-((unsigned char *)&(x))

but you can't do it for expressions or types without relying on
undefined behaviour, which is never wise if you can avoid it, and here
it can be avoided with ease by using sizeof.
i dont believe that .

Then find out for yourself, by reading the relevant language definition
specification, ISO/IEC 9899.
my c/c++ teacher (very experienced programmer)

Perhaps he is, but he doesn't know much about C if he can't even get the
entry point right.
AND the book were using both say that if main does not return a
value it can be declared void !

Then get a better book. Better still, get ISO/IEC 9899.
 
M

Martin Ambuhl

Heinrich said:
i dont believe that . my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !

As it happens, both your teacher and the author of your book are
incompetent. In both C and C++ (there is no such beast as c/c++) main
in a hosted implementation returns an int. Your book should be burnt.
Your "very experienced" teacher is probably protected from burning. It
is time for you to start learning C (or C++) and not what errors your
teacher is "very experienced" in using.
 
D

deepak

Heinrich Pumpernickel said:








I hope that the point of the exercise is to demonstrate that there is no
satisfactory, portable solution to the problem, other than to change
the rules and allow use of the sizeof operator.

You can just about do it for objects:

#define SIZEOF(x) ((unsigned char *)(&(x)+1)-((unsigned char *)&(x))
#define SIZEOF(x) (&x + 1) -(&x)
It will not work?
Please correct me If I'm wrong.
 
J

J. J. Farrell

i dont believe that .

Well, bully for you. Lots of people refuse to believe facts, but that
doesn't alter the facts.
my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !

Then you need to change your books as well as your teacher. C is well
defined by ISO Standards; it doesn't matter what nonsense is contained
in books which claims to be about C.
 
D

Denis Kasak

deepak said:
#define SIZEOF(x) (&x + 1) -(&x)
It will not work?
Please correct me If I'm wrong.

No. Apart from the fact that your macro would still only work for
objects, as Richard already pointed out, it would also always evaluate
to 1. This is a consequence of the way pointer arithmetics work
(semantically, the subtraction of pointers gives you the number of
objects of the type of x which could fit between those two pointers).
sizeof returns the size of the object in bytes and since 'byte' and
'char' are synonyms in C, subtracting char pointers evaluates to the
number of bytes which could fit between them (in this case, the size of
the object).
 
F

Flash Gordon

J. J. Farrell wrote, On 01/07/07 07:31:
Well, bully for you. Lots of people refuse to believe facts, but that
doesn't alter the facts.

It is understandable the a student would be more inclined to be believe
his teacher and text book than a random collection of people on the
internet.
Then you need to change your books as well as your teacher. C is well
defined by ISO Standards; it doesn't matter what nonsense is contained
in books which claims to be about C.

True. Also checking the relevant section of the comp.lang.c FAQ at
http://c-faq.com specifically http://c-faq.com/ansi/voidmain.html

Note that the further reading page includes a link to a post by
P.J.Plauger who is an author of a book on C, an author of standard C
libraries, and I think was on the committee that standardised C, so he
knows what he speaks of.

You can look at drafts of the C standards yourself, follow this link for
more details http://clc-wiki.net/wiki/c_standard
 
A

Army1987

Richard Heathfield said:
Heinrich Pumpernickel said:


I hope that the point of the exercise is to demonstrate that there is no
satisfactory, portable solution to the problem, other than to change
the rules and allow use of the sizeof operator.

You can just about do it for objects:

#define SIZEOF(x) ((unsigned char *)(&(x)+1)-((unsigned char *)&(x))

I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x));
return 0;
}
on my DS9K, and something very strange happened...
 
A

Army1987

Heinrich Pumpernickel said:
i dont believe that . my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !

Both your C/C++ teacher (is he actually teaching you both two
languages?) and the book you're using are wrong.
 
R

Richard Heathfield

Army1987 said:
I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x));
return 0;
}
on my DS9K, and something very strange happened...

Yes, because you can't take the address of a register. I deduce (from
the fact that you posted at all) that you survived the experience - or
did your machine post the article on your behalf, posthumously?

Anyway, your point is valid - register objects are Yet Another Reason
Not To Do This Very Silly Thing.
 
C

CBFalconer

Heinrich said:
i dont believe that . my c/c++ teacher (very experienced
programmer) AND the book were using both say that if main does
not return a value it can be declared void !

Then both the book AND your instructor are wrong, and both need
replacing.
 
A

Army1987

Richard Heathfield said:
Army1987 said:

Yes, because you can't take the address of a register. I deduce (from
the fact that you posted at all) that you survived the experience - or
did your machine post the article on your behalf, posthumously?

Anyway, your point is valid - register objects are Yet Another Reason
Not To Do This Very Silly Thing.

To be honest, the DS9K was actually MSVC, and the "something very
strange" was that it didn't compile. (The requirement for the
operand of & not to be register is a constraint.)

But I'm thinking of replacing it with:
#include <stdio.h>
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
int main(void)
{
int x = 23;
int *p = &x;
printf("%d\n", (int)SIZEOF(*p++));
return 0;
}
and running it on a DS9K. But I'm too afraid to do that.
 
C

CBFalconer

Army1987 said:
.... snip ...

I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x));
return 0;
}
on my DS9K, and something very strange happened...

Doesn't need a DS9K. Taking the address of a register variable is
illegal.
 
A

Army1987

Heinrich Pumpernickel said:
28b (macros). write a macro, sizeof(var), that returns the size of
its argument . you are not allowed, to use the sizeof() operator for
this .

#define SIZEOF( var ) sizeof var
Note that I used sizeof, without parentheses, not sizeof().
for extra credit, write another macro, sizeof_t(type), that
returns the size of a type .
Does the requirement for the previous point also apply to this one?
If so, the latter point is impossible. If not, try:
#define sizeof_t sizeof
 
A

Army1987

CBFalconer said:
Doesn't need a DS9K. Taking the address of a register variable is
illegal.

If it wasn't illegal, nothing strange would happen even on a DS9K,
since the DS9K is a conforming hosted implementation.
 
M

Mark McIntyre

well its just an excersise
OK, but in that case I suggest you write your best effort at solving
the problem, post it here, and ask for assistance. You could also
google for it.
i dont believe that . my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !

Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int. Whether you want to return
something or not, you need to at least return 0. Otherwise you are
breaking the rules of C.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Mark said:
Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int.

This is incorrect. The C standard requires implementations to accept main
returning int, but does not prohibit other return types either from being
allowed by implementations or from being used by programs.
 
R

Richard Heathfield

Harald van D?k said:
This is incorrect. The C standard requires implementations to accept
main returning int, but does not prohibit other return types either
from being allowed by implementations or from being used by programs.

True enough, but neither does it define the behaviour of such program.
The C Standard does not forbid implementations from accepting Fortran
programs, either. That doesn't mean that Fortran programs are correct C
programs.
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top