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

Can size_t be used as a substitute to unsigned long int ?

For eg :

struct mesh
{
size_t nvert; /* number of vertices */
size_t ntri; /* number of triangles */
.....
};

The reason I want to use size_t is because I've read that it is
capable of supporting size of the largest possible integer in the C
implementation. In my case, the value of nvert can vary from any thing
like 30-40 to even 10 million.
Jun 27 '08 #1
21 1882
On Thu, 26 Jun 2008 09:58:53 -0700, pereges wrote:
For eg :

struct mesh
{
size_t nvert; /* number of vertices */
size_t ntri; /* number of triangles */
.....
};

The reason I want to use size_t is because I've read that it is capable
of supporting size of the largest possible integer in the C
implementation.
I doubt that is what you read. It's more likely that you read something
similar, and misunderstood. size_t is capable of holding the size of the
largest possible object in the C implementation, but it's not always, and
maybe not even usually, capable of holding the largest possible integer.
On my system, for example, the largest integer has a value of
18446744073709551615, but I cannot store that in a size_t, and I cannot
even come close to creating any object that large.
In my case, the value of nvert can vary from any thing
like 30-40 to even 10 million.
Jun 27 '08 #2
On Jun 26, 10:06 pm, Harald van D©¦k <true...@gmail.comwrote:
>
I doubt that is what you read. It's more likely that you read something
similar, and misunderstood. size_t is capable of holding the size of the
largest possible object in the C implementation, but it's not always, and
maybe not even usually, capable of holding the largest possible integer.
On my system, for example, the largest integer has a value of
18446744073709551615, but I cannot store that in a size_t, and I cannot
even come close to creating any object that large.
But the value that a size_t type variable can hold must be integer ?
Then what do you mean by largest possible object ?

Jun 27 '08 #3
In article <a0**********************************@z24g2000prf. googlegroups.com>,
pereges <Br*****@gmail.comwrote:
For eg :

struct mesh
{
size_t nvert; /* number of vertices */
size_t ntri; /* number of triangles */
.....
};

The reason I want to use size_t is because I've read that it is
capable of supporting size of the largest possible integer in the C
implementation. In my case, the value of nvert can vary from any thing
like 30-40 to even 10 million.
size_t is appropriate for counting things that are represented as C
objects. It's not necessarily the largest (unsigned) integral type,
but it's effectively a limit on the size of arrays.

It would not be appropriate for counting things that are not
represented by C objects, such as the age of the universe or the
length of a line.

-- Richard

--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #4
On Jun 26, 7:58 pm, pereges <Brol...@gmail.comwrote:
For eg :

struct mesh
{
size_t nvert; /* number of vertices */
size_t ntri; /* number of triangles */
.....

};

The reason I want to use size_t is because I've read that it is
capable of supporting size of the largest possible integer in the C
implementation. In my case, the value of nvert can vary from any thing
like 30-40 to even 10 million.
If you want the largest unsigned integer supported by your
implementation, use uintmax_t (<stdint.h>).
SIZE_MAX (in <limits.h>) is at least 65535.
ULONG_MAX (in <limits.h>) is at least 4294967295.

It's possible that SIZE_MAX ULONG_MAX.
Find the upper limit, then use a type whose MAX is larger than that
upper limit.
In this case, unsigned long int.
Jun 27 '08 #5
pereges wrote:
On Jun 26, 10:06 pm, Harald van D?k <true...@gmail.comwrote:
>>
I doubt that is what you read. It's more likely that you read
something similar, and misunderstood. size_t is capable of holding
the size of the largest possible object in the C implementation, but
it's not always, and maybe not even usually, capable of holding the
largest possible integer. On my system, for example, the largest
integer has a value of 18446744073709551615, but I cannot store that
in a size_t, and I cannot even come close to creating any object that
large.

But the value that a size_t type variable can hold must be integer ?
The type of size_t is an unsigned integer, so yes, it must hold an
integer value.
Then what do you mean by largest possible object ?
It's the size of the largest object that an implementation could create.
Since the exact size will vary for different implementations (example
it might be 64Kb under small model DOS, or it might be ~4Gb Linux), the
exact type of size_t is implementation defined. The maximum value a
particular instance of size_t can hold is specified by the macro
SIZE_MAX in stdint.h.

Jun 27 '08 #6
pereges wrote:
For eg :

struct mesh
{
size_t nvert; /* number of vertices */
size_t ntri; /* number of triangles */
.....
};

The reason I want to use size_t is because I've read that it is
capable of supporting size of the largest possible integer in the C
implementation. In my case, the value of nvert can vary from any thing
like 30-40 to even 10 million.
Under C90 the largest possible integer value can be represented by
unsigned long[1]. Size_t may not be sufficient, (example it could be
aliased to unsigned int.) For a C99 based system use uintmax_t if you
need the absolute largest supported unsigned integer type.

1. And the signed variants of course.

Jun 27 '08 #7
pereges wrote:
...
The reason I want to use size_t is because I've read that it is
capable of supporting size of the largest possible integer in the C
implementation.
...
That's incorrect. 'size_t's range is big enough to store the size in
bytes of the largest possible object in C implementation. That's it.

It is not guaranteed to be capable of storing such quantities as, for
example, the number of different objects. And, of course, it is not
guaranteed to be the largest possible integer.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #8
Richard Tobin wrote:
...
size_t is appropriate for counting things that are represented as C
objects.
...
Maybe I misunderstood you, but there's no guarantee that 'size_t' can
store the total number of C objects I can allocate in dynamic memory.

For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #9
On Jun 27, 2:29 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Richard Tobin wrote:
...
size_t is appropriate for counting things that are represented as C
objects.
...

Maybe I misunderstood you, but there's no guarantee that 'size_t' can
store the total number of C objects I can allocate in dynamic memory.

For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.
So indeed, you have misunderstood.
Jun 27 '08 #10
vi******@gmail.com writes:
On Jun 27, 2:29 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
>Richard Tobin wrote:
...
size_t is appropriate for counting things that are represented as C
objects.
> ...

Maybe I misunderstood you, but there's no guarantee that 'size_t' can
store the total number of C objects I can allocate in dynamic memory.

For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.
So indeed, you have misunderstood.
But you would not necessarily use a size_t to store that count.

size_t is generally used as a size or length holder, not the number of
times something has been done.

So he is right. Kind of.
Jun 27 '08 #11
In article <lz************@stalkings.ghoti.net>,
Keith Thompson <ks***@mib.orgwrote:
>uintptr_t, if it exists, can represent a distinct value for each
possible void* value. Therefore, uintptr_t, if it exists, can safely
be used as an object count.
An application of the "pigeonhole principle".

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Jun 27 '08 #12
vi******@gmail.com wrote:
>>
For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.
Each time I do

char* p = malloc(1);
*p = '\0';

I create a 'char' object with 'allocated' storage duration (it's pointed
by 'p'). So counting these dynamically allocated objects will be
"counting things represented as C objects".

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #13
On Jun 28, 1:09 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
vipps...@gmail.com wrote:
For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.

Each time I do

char* p = malloc(1);
*p = '\0';

I create a 'char' object with 'allocated' storage duration (it's pointed
No you don't, you possibly invoke undefined behavior.
It does not create a char object, it allocates sufficient space to
hold a char value.
by 'p'). So counting these dynamically allocated objects will be
"counting things represented as C objects".
Wrong. You count successful allocations, not the size of an object.

Jun 28 '08 #14
vi******@gmail.com writes:
On Jun 28, 1:09 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
>vipps...@gmail.com wrote:
>For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.

Each time I do

char* p = malloc(1);
*p = '\0';

I create a 'char' object with 'allocated' storage duration (it's pointed
No you don't, you possibly invoke undefined behavior.
It does not create a char object, it allocates sufficient space to
hold a char value.
malloc creates space for an object. An object is just a region of
storage whose contents represent values. Do you really want to split
hairs and claim that the object pointed to by malloc's (successful)
return is not a char object, even though it is an object and it can be
used to represent char values?
>by 'p'). So counting these dynamically allocated objects will be
"counting things represented as C objects".
Wrong. You count successful allocations, not the size of an object.
I suspect there must be a language problem here. How can you have any
objection to counting successful allocations as being an example of
"counting things represented as C objects"?

--
Ben.
Jun 28 '08 #15
On Jun 28, 5:01 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
vipps...@gmail.com writes:
On Jun 28, 1:09 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
by 'p'). So counting these dynamically allocated objects will be
"counting things represented as C objects".
Wrong. You count successful allocations, not the size of an object.

I suspect there must be a language problem here. How can you have any
objection to counting successful allocations as being an example of
"counting things represented as C objects"?
I completely misunderstood "counting things represented as C objects".
Mr Tarasevich is also right. Sorry for the confusion.
Jun 28 '08 #16
On Jun 27, 12:54 pm, Richard Heathfield <r...@see.sig.invalidwrote:
It is a good idea to ensure that any value coming from an outside source
falls within the range of values that make sense for the purpose. As
someone has noted elsethread, users don't enter integers; they press keys.
If you can make sure they pressed the right keys (or at least, didn't
press the wrong keys), that's always a good place to start.
I think the compiler can take care of it.

I tried the following :

unsigned long v_index /* vertex index ranging from 0...nvert - 1 */

if (v_index < 0 || v_index nvert - 1)
{
printf("Vertex index out of range\n");
}

I got a warning from the compiler:

warning #2130: Result of unsigned comparison is constant.
Jun 28 '08 #17
On Jun 28, 6:14 pm, pereges <Brol...@gmail.comwrote:
On Jun 27, 12:54 pm, Richard Heathfield <r...@see.sig.invalidwrote:
It is a good idea to ensure that any value coming from an outside source
falls within the range of values that make sense for the purpose. As
someone has noted elsethread, users don't enter integers; they press keys.
If you can make sure they pressed the right keys (or at least, didn't
press the wrong keys), that's always a good place to start.

I think the compiler can take care of it.

I tried the following :

unsigned long v_index /* vertex index ranging from 0...nvert - 1 */

if (v_index < 0 || v_index nvert - 1)
{
printf("Vertex index out of range\n");

}

I got a warning from the compiler:

warning #2130: Result of unsigned comparison is constant.
That warning is beacuse of v_index < 0.
v_index is an unsigned integer. It can never have a negative value,
thus v_index < 0 is always false.
The warning is not correct (nor it needs to be); it's not a constant
expression. A better warning would be "result of expression is always
false".
Jun 28 '08 #18
On Jun 28, 8:21 pm, vipps...@gmail.com wrote:
That warning is beacuse of v_index < 0.
v_index is an unsigned integer. It can never have a negative value,
thus v_index < 0 is always false.
The warning is not correct (nor it needs to be); it's not a constant
expression. A better warning would be "result of expression is always
false".
Ok, and are there any issues involved in reading unsigned long
integers from an ascii file ?
Jun 28 '08 #19
pereges said:
On Jun 28, 8:21 pm, vipps...@gmail.com wrote:
>That warning is beacuse of v_index < 0.
v_index is an unsigned integer. It can never have a negative value,
thus v_index < 0 is always false.
The warning is not correct (nor it needs to be); it's not a constant
expression. A better warning would be "result of expression is always
false".

Ok, and are there any issues involved in reading unsigned long
integers from an ascii file ?
If an ASCII file is one that contains only ASCII representations of text,
then it doesn't have any unsigned long integers in it, so there can't be
any issues involved in reading them, since they aren't there to read.

What you *can* do, of course, is to interpret text representations of
numbers, translating them into numbers internally. When doing this, you
need to be sure that the internal representation you choose is capable of
representing the numbers you are translating.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 28 '08 #20
pereges said:
On Jun 27, 12:54 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>It is a good idea to ensure that any value coming from an outside source
falls within the range of values that make sense for the purpose. As
someone has noted elsethread, users don't enter integers; they press
keys. If you can make sure they pressed the right keys (or at least,
didn't press the wrong keys), that's always a good place to start.

I think the compiler can take care of it.

I tried the following :

unsigned long v_index /* vertex index ranging from 0...nvert - 1 */

if (v_index < 0 || v_index nvert - 1)
{
printf("Vertex index out of range\n");
}

I got a warning from the compiler:

warning #2130: Result of unsigned comparison is constant.
It's strange that you didn't get a warning from the missing semicolon.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 28 '08 #21
vi******@gmail.com wrote:
On Jun 28, 1:09 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
>vipps...@gmail.com wrote:
>>>For example, I can successfully execute 'malloc(1)' more times (i.e.
allocate more 1-byte objects) than 'size_t' can count.
And what you'd count is the successful allocations, not 'things that
are represented as C objects'.
Each time I do

char* p = malloc(1);
*p = '\0';

I create a 'char' object with 'allocated' storage duration (it's pointed
No you don't, you possibly invoke undefined behavior.
Possibly.
It does not create a char object, it allocates sufficient space to
hold a char value.
Assuming that (p) does not compare equal to NULL:

(*p) is the lvalue of a char type object.
It does have allocated storage duration.
It wasn't always there;
if Andrey Tarasevich didn't create it, then where did it come from?

N869
3.15
[#1] object
region of data storage in the execution environment, the
contents of which can represent values

6.3.2 Other operands
6.3.2.1 Lvalues and function designators
[#1] An lvalue is an expression with an object type or an
incomplete type other than void;

When an object is said to have a particular type,
the type is specified by the lvalue used to designate the object.

--
pete
Jun 28 '08 #22

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

Similar topics

18
by: rayw | last post by:
I used to believe that size_t was something to do with integral types, and the std. Something along the lines of .. a char is 8 bits, a int >= a char a long >= int
5
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct?...
23
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
22
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
73
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may...
24
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
8
by: lubomir dobsik | last post by:
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.