size_t is defined as "size_t type is a base unsigned integer type of C/C++ language. It is the result of sizeof operator's execution. The type's size is chosen so that it could store the maximum size of a theoretically possible array. On a 32-bit system size_t will take 32 bits, on a 64-bit one 64 bits."

What is meant by "Its the result of the sizeof operation". sizeof operator requires an argument like int,char,etc. How can it give the size required to represent a complete computer word for that system (like 8 bytes, or whatever).

>What is meant by "Its the result of the sizeof operation". sizeof operator requires an argument like int,char,etc.

You seem to be looking at a standard definition, why not look at the whole thing?

6.5.3.4 The sizeof operator
Constraints
1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.
Semantics
2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
3 When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array.85) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
4 The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

>How can it give the size required to represent a complete computer word for that system (like 8 bytes, or whatever).

Hm? I don't know if I understand your question(s).

Edited 6 Years Ago by Dave Sinkula: n/a

How can it give the size required to represent a complete computer word for that system?
What for? Please write example.

I was quoting from the following web page :

http://www.dreamincode.net/forums/showtopic129989.htm

1. I couldn't understand the following line :

"It (size_t type) is the result of sizeof operator's execution."

But, the size of the size_t type for a particular system is fixed, its 32 bits for a 32 bit system, 64 bits for a 64 bit system. sizeof operator returns the size of a particular data type, or array,etc ( it will give a different value for int, another for char ,etc). Then, how can size_t be the data type of result of the sizeof operator's execution, when the size of size_t is fixed, and the sizeof operator returns different sizes for different data types.

basically what's the meaning of that line.

2. Does it mean that the 32 bit value for the 32 bit system, and 64 bit value for the 64 bit system represents type_t value for an int onlyi.e. type_t data type can have various sizes depending upon the data type on which the sizeof operator is applied ?

size_t is a data type, not a data size. int is a data type. char is a data type. So is size_t. What goes into it is the result of executing the sizeof() 'function' -- it returns a data type of size_t.

>Then, how can size_t be the data type of result of the sizeof
>operator's execution, when the size of size_t is fixed, and the sizeof
>operator returns different sizes for different data types.

How can int hold all of those different values when the size is fixed on an implementation? You know what size_t is? A typedef:

typedef unsigned int size_t;

It's a synonym for whatever type the implementation chooses to fit the bill.

I was quoting from the following web page :

http://www.dreamincode.net/forums/showtopic129989.htm

From that page, I don't know that the following is correct:

ptrdiff_t type

ptrdiff_t type is a base signed integer type of C/C++ language. The type's size is chosen so that it could store the maximum size of a theoretically possible array. On a 32-bit system ptrdiff_t will take 32 bits, on a 64-bit one 64 bits. Like in size_t, ptrdiff_t can safely store a pointer except for a pointer to a class function. Also, ptrdiff_t is the result of an expression where one pointer is subtracted from the other (ptr1-ptr2). ptrdiff_t type is usually used for loop counters, array indexing, size storage and address arithmetic.

It seems to mince (confuse?) size_t and ptrdiff_t ; and the bit about storing a pointer is really wandering off.

1. I couldn't understand the following line :

"It (size_t type) is the result of sizeof operator's execution."

But, the size of the size_t type for a particular system is fixed, its 32 bits for a 32 bit system, 64 bits for a 64 bit system. sizeof operator returns the size of a particular data type, or array,etc ( it will give a different value for int, another for char ,etc). Then, how can size_t be the data type of result of the sizeof operator's execution, when the size of size_t is fixed, and the sizeof operator returns different sizes for different data types.

basically what's the meaning of that line.

2. Does it mean that the 32 bit value for the 32 bit system, and 64 bit value for the 64 bit system represents type_t value for an int onlyi.e. type_t data type can have various sizes depending upon the data type on which the sizeof operator is applied ?

Source code can be portable. You can compile the same C source on Windows or Linux, or perhaps some embedded devices (if written portably). While fixed at compile time, what a size_t resolves to can be different on different systems, or even different target options on the same system. What it is will be determined by the compiler at compile time.

>See also: Terminology size_t.
>And article "About size_t and ptrdiff_t".

I like how all of the references in those articles are to other articles written by the same author. :D

>See also: Terminology size_t.
>And article "About size_t and ptrdiff_t".

I like how all of the references in those articles are to other articles written by the same author. :D

It's also interesting that all posts made by "Andrey_Karpov" have a link in them to that viva64-site... But it's on-topic(-ish) so he gets the benefit of the doubt for now :)

Edited 6 Years Ago by Nick Evan: n/a

See also: Terminology size_t.
And article "About size_t and ptrdiff_t".

Following the 2nd article to this link, I find this:

ptrdiff_t type is a base signed integer type of C/C++ language. The type's size is chosen so that it could store the maximum size of a theoretically possible array. On a 32-bit system ptrdiff_t will take 32 bits, on a 64-bit one 64 bits. Like in size_t, ptrdiff_t can safely store a pointer except for a pointer to a class function. Also, ptrdiff_t is the result of an expression where one pointer is subtracted from the other (ptr1-ptr2). ptrdiff_t type is usually used for loop counters, array indexing, size storage and address arithmetic.

I had mentioned a quote here and that I don't think this is correct.

the most annoying thing about ptrdiff_t is that the standard doesn't require that pointer math must be representable within the range of this type, i.e., using it has the potential for having undefined behavior.

[aside]Oh, how I wish Daniweb would nicely/allow quoting multiple levels so I don't have try to repeat this manually. Follow some links instead, I guess.[/aside]

Anyways, one of us needs a clarification. I think what I have read says that a ptrdiff_t does not necessarily allow indexing into the maximum array size for the platform, but that a size_t does. (But I think the "size_t, ptrdiff_t can safely store a pointer ..." part just needs to go, or at the very least be reworded.)

This question has already been answered. Start a new discussion instead.