Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: a macro problem

Reply
Thread Tools

Re: a macro problem

 
 
Ian Collins
Guest
Posts: n/a
 
      10-14-2010
On 10/15/10 08:14 AM, Davide wrote:
> Hello comp.lang.c,
> I'm a guy from Italy. First of all, sorry for my English
> I'm attending a one-semester course in "Foundations of Programming".
> We use the C language and "A book on C", 4th edition, by Kelley-Pohl.
>
> At some point at the beginning of the book there is an example,
> the header file "pacific_sea.h". Inside this file, there is the line:
>
> #define SQ_FEET_PER_SQ_MILE (5280 * 5280)
>
> Here is my problem: the book says that writing 27878400 would be the
> same. This is what my teacher says too. He says that (5280 * 5280)
> will be automatically promoted to a type that fits that product.


As the other replies have said, that is incorrect.

Unless SQ_FEET_PER_SQ_MILE is required to be a compile time constant, it
is much clearer and less error prone to write:

const size_t sq_feet_per_sq_mile = 5280 * 5280;

--
Ian Collins
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-14-2010
Ian Collins <(E-Mail Removed)> writes:
> On 10/15/10 08:14 AM, Davide wrote:
>> Hello comp.lang.c,
>> I'm a guy from Italy. First of all, sorry for my English
>> I'm attending a one-semester course in "Foundations of Programming".
>> We use the C language and "A book on C", 4th edition, by Kelley-Pohl.
>>
>> At some point at the beginning of the book there is an example,
>> the header file "pacific_sea.h". Inside this file, there is the line:
>>
>> #define SQ_FEET_PER_SQ_MILE (5280 * 5280)
>>
>> Here is my problem: the book says that writing 27878400 would be the
>> same. This is what my teacher says too. He says that (5280 * 5280)
>> will be automatically promoted to a type that fits that product.

>
> As the other replies have said, that is incorrect.
>
> Unless SQ_FEET_PER_SQ_MILE is required to be a compile time constant, it
> is much clearer and less error prone to write:
>
> const size_t sq_feet_per_sq_mile = 5280 * 5280;


That still overflows if INT_MAX < 5280*5280.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-14-2010
Davide <(E-Mail Removed)> writes:
> Ian Collins wrote:
>> On 10/15/10 08:14 AM, Davide wrote:
>>> Hello comp.lang.c,
>>> I'm a guy from Italy. First of all, sorry for my English
>>> I'm attending a one-semester course in "Foundations of Programming".
>>> We use the C language and "A book on C", 4th edition, by Kelley-Pohl.
>>>
>>> At some point at the beginning of the book there is an example,
>>> the header file "pacific_sea.h". Inside this file, there is the line:
>>>
>>> #define SQ_FEET_PER_SQ_MILE (5280 * 5280)
>>>
>>> Here is my problem: the book says that writing 27878400 would be the
>>> same. This is what my teacher says too. He says that (5280 * 5280)
>>> will be automatically promoted to a type that fits that product.

>>
>> As the other replies have said, that is incorrect.
>>
>> Unless SQ_FEET_PER_SQ_MILE is required to be a compile time constant, it
>> is much clearer and less error prone to write:
>>
>> const size_t sq_feet_per_sq_mile = 5280 * 5280;

>
> Thank you, for your reply. But the max for size_t could be
> 65535, am I wrong? And the risk of overflow, according to what
> Keith Thompson said, still holds.


Right. size_t is for measuring sizes of objects; it's not
necessarily big enough to hold numbers of something in the real
world. No object in your program can be bigger than size_t bytes[*];
size_t is also safe for array indices (since array elements are at
least one byte). But even file sizes can exceed SIZE_MAX bytes.
Picking the right type for what you're measuring can be tricky,
and there's no really good general technique.

You're not likely to run into a system with SIZE_MAX as small
as 65535, but IMHO it's good to write code that's just a little
more portable than it needs to be.

> Maybe const long sq_feet_per_sq_mile = 5280 * 5280L;


That would be safe, given the language guarantee that
LONG_MAX >= 2**31-1. (But I'd put the "L" suffix on both
constants, just for consistency.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-14-2010
On 10/15/10 10:49 AM, Davide wrote:
>
> Sorry if I change subject. But, since malloc takes an argument
> of type size_t, does it mean that I should declare several mallocS
> if I wanted to allocate more than 65535 bytes in a safe way?


No. If you need more than can be allocated, you don't have enough memory!

--
Ian Collins
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-14-2010
Davide <(E-Mail Removed)> writes:
[...]
> Sorry if I change subject. But, since malloc takes an argument
> of type size_t, does it mean that I should declare several mallocS
> if I wanted to allocate more than 65535 bytes in a safe way?


In theory, yes, but I generally wouldn't bother. A system with
SIZE_MAX==65535 is unlikely to be able to allocate multiple objects
of that size.

SIZE_MAX is the maximum size of a single object. A system might
limit individual objects to 65535 bytes, but allow some larger
total amount of memory for all allocated objects. But most modern
systems have a monolithic address space, so the maximum size of a
single object is the same as the total available memory size.

Unless there's some system-specific reason to do something fancier,
I'd just try malloc()ing whatever I need and be prepared to deal with
failure (most likely by cleaning up and terminating the program).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-15-2010
On 10/14/2010 4:17 PM, Ian Collins wrote:
> On 10/15/10 08:14 AM, Davide wrote:
>> Hello comp.lang.c,
>> I'm a guy from Italy. First of all, sorry for my English
>> I'm attending a one-semester course in "Foundations of Programming".
>> We use the C language and "A book on C", 4th edition, by Kelley-Pohl.
>>
>> At some point at the beginning of the book there is an example,
>> the header file "pacific_sea.h". Inside this file, there is the line:
>>
>> #define SQ_FEET_PER_SQ_MILE (5280 * 5280)
>>
>> Here is my problem: the book says that writing 27878400 would be the
>> same. This is what my teacher says too. He says that (5280 * 5280)
>> will be automatically promoted to a type that fits that product.

>
> As the other replies have said, that is incorrect.
>
> Unless SQ_FEET_PER_SQ_MILE is required to be a compile time constant, it
> is much clearer and less error prone to write:
>
> const size_t sq_feet_per_sq_mile = 5280 * 5280;


Doesn't fix the problem: If INT_MAX < 27878400, this fragment
has undefined behavior. Even if the result of the multiplication
is calculated correctly, the conversion to `size_t' (which could
be a "demotion") may change the value, quite possibly to 25600.
Finally, `SQ_FEET_PER_SQ_MILE' can be used in some contexts where
`sq_feet_per_sq_mile' cannot be: `case' labels, for instance (not
a likely issue for this particular example, but let's generalize).

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments