define a size_t constant

M

Mara Guida

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 */
 
H

Harald van Dijk

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?
 
M

Mara Guida

Harald said:
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.
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.
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.
 
K

Keith Thompson

Mara Guida said:
Harald said:
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.
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.
 
H

Harald van Dijk

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.
 
K

Keith Thompson

Huibert Bol said:
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."

[...]
 
M

Mara Guida

Harald said:
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 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.
[...]

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).
 
P

Peter Nilsson

Harald van D©¦k said:
...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.
 
J

Joachim Schmitz

Peter said:
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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top