473,395 Members | 2,446 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,395 software developers and data experts.

define a size_t constant

Is there a suffix for size_t?

I want to define MAX_LINE_LENGTH as a size_t.
The best I could come up with was
#define MAX_LINE_LENGTH ((size_t)80)
but I wonder if there's a more beautiful construct, like
/* Error: Invalid suffix "ZU" */
/* #define MAX_LINE_LENGTH 80ZU */
Oct 18 '08 #1
9 9211
On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
Is there a suffix for size_t?
size_t is a typedef, not a built-in type. It is a typedef for unsigned int
on some systems, and there the suffix is U. It is a typedef for unsigned
long on some other systems, and there the suffix is UL. And those are not
the only two possibilities.
I want to define MAX_LINE_LENGTH as a size_t.
Why do you want to do this? Does size_t offer anything of use for
constants that the default type doesn't? Depending on why you want this,
there may be other options.
The best I could come up with was

#define MAX_LINE_LENGTH ((size_t)80)
You cannot use this in #if expressions. That's fine if you don't need to,
but macros for constants are generally useable there.
but I wonder if there's a more beautiful construct,
const size_t MAX_LINE_LENGTH = 80;

It has its own limitations: you cannot use this as a static array length,
for example. What do you want to do with MAX_LINE_LENGTH?
Oct 18 '08 #2
Harald van D©¦k wrote:
On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
Is there a suffix for size_t?

size_t is a typedef, not a built-in type.
Ah! Of course.
I got used to thinking of size_t as a built-in type.
I want to define MAX_LINE_LENGTH as a size_t.

Why do you want to do this? Does size_t offer anything of use for
constants that the default type doesn't? Depending on why you want this,
there may be other options.
I want to minimize the number of casts and warnings of my programs.
Some functions take a size_t as a parameter and with a plain #define
the compiler issues a warning; other functions take an int and
complain with a size_t.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH_AS_INT 80
#define MAX_LINE_LENGTH_AS_SIZE_T ((size_t)80)

int main(void)
{
char *data_int;
char *data_size_t = malloc(MAX_LINE_LENGTH_AS_SIZE_T);
if (data_size_t) {
/* warning: passing argument 2 of ¡®fgets¡¯ with different width
due to prototype */
fgets(data_size_t, MAX_LINE_LENGTH_AS_SIZE_T, stdin);
/* fgets(data_size_t, (int)MAX_LINE_LENGTH_AS_SIZE_T, stdin);
*/ /* ok with cast */
free(data_size_t);
}

/* warning: passing argument 1 of ¡®malloc¡¯ with different width
due to prototype */
data_int = malloc(MAX_LINE_LENGTH_AS_INT);
/* data_int = malloc((size_t)MAX_LINE_LENGTH_AS_INT); */ /* ok
with cast */
if (data_int) {
fgets(data_int, MAX_LINE_LENGTH_AS_INT, stdin);
free(data_int);
}
return 0;
}
I think it's better to cast a size_t to int, than the other way
around.
but I wonder if there's a more beautiful construct,

const size_t MAX_LINE_LENGTH = 80;

It has its own limitations: you cannot use this as a static array length,
for example. What do you want to do with MAX_LINE_LENGTH?
This looks good, thank you.
I'm going to try some variations with const too.
Oct 18 '08 #3
Mara Guida <ma*******@gmail.comwrites:
Harald van D©¦k wrote:
>On Sat, 18 Oct 2008 07:58:37 -0700, Mara Guida wrote:
Is there a suffix for size_t?

size_t is a typedef, not a built-in type.
Ah! Of course.
I got used to thinking of size_t as a built-in type.
I want to define MAX_LINE_LENGTH as a size_t.

Why do you want to do this? Does size_t offer anything of use for
constants that the default type doesn't? Depending on why you want this,
there may be other options.
I want to minimize the number of casts and warnings of my programs.
Some functions take a size_t as a parameter and with a plain #define
the compiler issues a warning; other functions take an int and
complain with a size_t.
[...]
#define MAX_LINE_LENGTH_AS_INT 80
#define MAX_LINE_LENGTH_AS_SIZE_T ((size_t)80)
[...]
/* warning: passing argument 1 of ¡®malloc¡¯ with different width
due to prototype */
data_int = malloc(MAX_LINE_LENGTH_AS_INT);
/* data_int = malloc((size_t)MAX_LINE_LENGTH_AS_INT); */ /* ok
with cast */
[...]

I don't think I've never seen such a warning. So simply calling

malloc(80)

would produce the same warning? I think your compiler is being overly
picky.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Oct 18 '08 #4
Keith Thompson wrote:
I don't think I've never seen such a warning. So simply calling
Double negatives are *so* confusing. Is it yes or no?
>
malloc(80)

would produce the same warning? I think your compiler is being overly
picky.
gcc will do that for you (if you ask politely):

$ cat t.c
void foo(long long a) { }
void bar(void) { foo(1); }
$ gcc -Wconversion -fsyntax-only t.c
t.c: In function ‘bar’:
t.c:2: warning: passing argument 1 of ‘foo’ with different width due to prototype

--
Huibert
"Hey! HEY! Curious cat, here!" -- Krosp I (GG)
Oct 18 '08 #5
On Sat, 18 Oct 2008 09:03:00 -0700, Mara Guida wrote:
I want to minimize the number of casts and warnings of my programs. Some
functions take a size_t as a parameter and with a plain #define the
compiler issues a warning; other functions take an int and complain with
a size_t.
[...]
#define MAX_LINE_LENGTH_AS_INT 80
[...]
/* warning: passing argument 1 of ‘malloc’ with different width
due to prototype */
data_int = malloc(MAX_LINE_LENGTH_AS_INT)
This is what gcc's -Wconversion or -Wtraditional-conversion (depending on
the version) does. It is meant to help you write code that will compile
and run correctly both on modern compilers and old compilers that don't
support function prototypes. Your code will not work on those old
compilers anyway, so I can't see why you would use this option. The code
is correct, so the simple solution is to not tell your compiler to warn
you about it.
Oct 18 '08 #6
Huibert Bol <hu*********@quicknet.nlwrites:
Keith Thompson wrote:
>I don't think I've never seen such a warning. So simply calling

Double negatives are *so* confusing. Is it yes or no?
Sorry, that was just a typo; I meant "I don't think I've ever seen
such a warning."

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Oct 18 '08 #7
Harald van D©¦k wrote:
On Sat, 18 Oct 2008 09:03:00 -0700, Mara Guida wrote:
I want to minimize the number of casts and warnings of my programs.
[...]
#define MAX_LINE_LENGTH_AS_INT 80
[...]
/* warning: passing argument 1 of ¡®malloc¡¯ with differentwidth
due to prototype */
data_int = malloc(MAX_LINE_LENGTH_AS_INT)

This is what gcc's -Wconversion or -Wtraditional-conversion (depending on
the version) does.
[...]
Thank you for the comprehensive answer.
I've been tweaking my compilation alias with what appears here every
now and then. I read about what the specific option does before adding
it in, and I don't remember thinking the -Wconversion would be bad for
me (or maybe I skipped its description).
Oct 18 '08 #8
Harald van D©¦k <true...@gmail.comwrote:
...This is what gcc's -Wconversion or -Wtraditional-
conversion (depending on the version) does. ...
The code is correct, so the simple solution is to
not tell your compiler to warn you about it.
In this case, turning it off is a viable option, but
it's amazing how many people still have a pathological
urge to modify perfectly correct code just to make it
compile without warnings against -W -Wall.

--
Peter
Oct 19 '08 #9
Peter Nilsson wrote:
Harald van D©¦k <true...@gmail.comwrote:
>...This is what gcc's -Wconversion or -Wtraditional-
conversion (depending on the version) does. ...
The code is correct, so the simple solution is to
not tell your compiler to warn you about it.

In this case, turning it off is a viable option, but
it's amazing how many people still have a pathological
urge to modify perfectly correct code just to make it
compile without warnings against -W -Wall.
-Wconversion is AFAIK not part of -W -Wall

Bye, Jojo
Oct 20 '08 #10

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
7
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? ...
5
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
13
by: Allen | last post by:
I defines FileLogConstant class as following. class FileLogConstant { public: static const INT32 REGISTER_LOGGER; static const INT32 REGISTER_METHOD; static const INT32 MAX_LOGGER_COUNT;
20
by: subramanian100in | last post by:
Suppose I have #included <stdint.h> #define MAX_LINE_SIZE SIZE_MAX or const size_t maxLineLength = SIZE_MAX ; Between the above #define and const declaration, which should be...
7
by: miaohua1982 | last post by:
the code is as follows: #include<stdio.h> int arr={1,2,3,4,5,6,7}; #define size (sizeof(arr)/sizeof(arr)) int main() { int index = -1; int x = 0; int t;
50
by: jacek.dziedzic | last post by:
Hi! On a machine where size_t is 64-bit, unsigned long is 32-bit, how does one construct a size_t literal that says 2^32? Typing in size_t x = 4294967296UL; complains about the value being...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.