473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to write a macro that calculate sizeof data types

plz reply,
thanks in advance.
bye,
Nov 14 '05 #1
12 4022
Sweety wrote:
plz reply,
thanks in advance.
bye,


HOMEWORK QUESTION
Nov 14 '05 #2
Sweety wrote:
plz reply,
thanks in advance.
bye,


HOMEWORK QUESTION
Nov 14 '05 #3

"Sweety" <sw************@yahoo.co.in> wrote in message

sizeof will calculate the size of a data item or type. Just redefining it as
a macro will achieve nothing except to obfusucate your code. Ask what your
teacher is looking for here.
Nov 14 '05 #4

"Sweety" <sw************@yahoo.co.in> wrote in message

sizeof will calculate the size of a data item or type. Just redefining it as
a macro will achieve nothing except to obfusucate your code. Ask what your
teacher is looking for here.
Nov 14 '05 #5
Sweety wrote:
plz reply,
Glad to.
thanks in advance.
No problem.
bye,


Got the message yet?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
Sweety wrote:
plz reply,
Glad to.
thanks in advance.
No problem.
bye,


Got the message yet?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #7
Sweety wrote:
plz reply,
thanks in advance.
bye,

Does the following

#define SIZEOF sizeof
size_t i= SIZEOF(char)

not work?
--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Nov 14 '05 #8
Sweety wrote:
plz reply,
thanks in advance.
bye,

Does the following

#define SIZEOF sizeof
size_t i= SIZEOF(char)

not work?
--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Nov 14 '05 #9
Sweety wrote:

plz reply,


Oh very well.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #10
Sweety wrote:

plz reply,


Oh very well.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #11
In <11*************************@posting.google.com> sw************@yahoo.co.in (Sweety) writes:

Except for the trivial

#define SIZEOF sizeof

sizeof(type) cannot be replaced by a macro (portably). It would
have to use a trick similar to that used by the canonical offsetof
implementation (pointer arithmetic on a null pointer of the type
in question):

#define TSIZEOF(x) ((char *)((x *)0 + 1) - (char *)(x *)0)

What can be done portably is replacing sizeof lvalue by a macro:

#define LVSIZEOF(x) ((char *)(&(x) + 1) - (char *)&(x))

Neither of these macros has the right type for sizeof, but this can be
trivially fixed.

The part that can't be implemented in standard C as a macro at all is
sizeof rvalue. This can be reduced to TSIZEOF with a GNU C extension:

#define RVSIZEOF(x) TSIZEOF(typeof(x))

Remeber: only LVSIZEOF is written in portable C, although TSIZEOF is
likely to work practically anywhere.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <11*************************@posting.google.com> sw************@yahoo.co.in (Sweety) writes:

Except for the trivial

#define SIZEOF sizeof

sizeof(type) cannot be replaced by a macro (portably). It would
have to use a trick similar to that used by the canonical offsetof
implementation (pointer arithmetic on a null pointer of the type
in question):

#define TSIZEOF(x) ((char *)((x *)0 + 1) - (char *)(x *)0)

What can be done portably is replacing sizeof lvalue by a macro:

#define LVSIZEOF(x) ((char *)(&(x) + 1) - (char *)&(x))

Neither of these macros has the right type for sizeof, but this can be
trivially fixed.

The part that can't be implemented in standard C as a macro at all is
sizeof rvalue. This can be reduced to TSIZEOF with a GNU C extension:

#define RVSIZEOF(x) TSIZEOF(typeof(x))

Remeber: only LVSIZEOF is written in portable C, although TSIZEOF is
likely to work practically anywhere.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13

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

Similar topics

0
by: MHenry | last post by:
Hi, I know virtually nothing about creating Macros in Access. I would appreciate some help in creating a Macro or Macros that automatically run(s) 14 Queries (three Make Table Queries, and 11...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
8
by: Sweety | last post by:
hello, how to use macro to calculate the sizeof some data types. plz explain . thanks in advance. bye,
6
by: Sweety | last post by:
plz reply, thanks in advance. bye,
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
1
by: mccoyn | last post by:
I have a macro that can determine the number of elements in an array type. It is defined as follows #define ARRAY_SIZE_NOGC(x) (sizeof(x) / sizeof(x) This works with unmanaged array types, but...
2
by: leo.hou | last post by:
Hi experts, I am new to linux and all the type definitions are driving me mad. What is the best way to check a type definition in linux? When I use man page to check some function definition, I...
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.