473,472 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regarding size_t

Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

Thanks in advance.

Jan 13 '07 #1
13 1530
On 13 Jan 2007 01:39:22 -0800, sa*****@yahoo.co.in wrote:
>Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

Thanks in advance.
size_t is a pre-defined type, which, according to the C Standard, is
"the unsigned integral type of the result of the sizeof operator". The
type size_t is unsigned, and it cannot possibly be defined as type
int, which must be signed.

--
jay
Jan 13 '07 #2
sam_...@yahoo.co.in wrote:
Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?
size_t is a typedef for some unsigned integer type meant to be capable
of storing the size of objects. It is not pre-defined: it is defined in
several of the standard headers, but if you don't include any of them,
you're free to use size_t as an ordinary identifier in your own code.
Which specific type it has depends on the implementation, and it would
be best not to assume anything more than you need to about it.

Jan 13 '07 #3
Harald van D?k wrote:
sam_...@yahoo.co.in wrote:
>I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.
No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski
Jan 13 '07 #4
CBFalconer wrote:
Harald van D?k wrote:
sam_...@yahoo.co.in wrote:
I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?
size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.

No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.
Citation, please?

Jan 13 '07 #5
jaysome wrote:
On 13 Jan 2007 01:39:22 -0800, sa*****@yahoo.co.in wrote:
>Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

Thanks in advance.

size_t is a pre-defined type, which, according to the C Standard, is
"the unsigned integral type of the result of the sizeof operator". The
type size_t is unsigned, and it cannot possibly be defined as type
int, which must be signed.
A minor clarification: size_t is "pre-defined" if you include
one or more of the headers that define it, but is not "baked in"
to the compiler in the way sizeof is. The following is a legal C
function, albeit a stupid one:

#include <math.h>
double size_t(double FILE, double NULL, double offsetof) {
return (-NULL + sqrt(NULL*NULL - 4*FILE*offsetof))
/ (2*FILE);
}

The following translation unit is not legal C:

/* no header inclusions */
size_t elementCount(size_t arraySize, size_t elementSize) {
return arraySize / elementSize;
}

.... because size_t is not defined.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 13 '07 #6
sa*****@yahoo.co.in wrote:
Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?
It can't be. 'int' is a signed integer type; 'size_t' is an unsigned
integer type.
Jan 13 '07 #7
Harald van D?k wrote:
CBFalconer wrote:
>Harald van D?k wrote:
>>sam_...@yahoo.co.in wrote:

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.

No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.

Citation, please?
>From N869:
7.1.3 Reserved identifiers

[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.

-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.

-- All identifiers that begin with an underscore are
always reserved for use as identifiers with file scope
in both the ordinary and tag name spaces.

-- Each macro name in any of the following subclauses
(including the future library directions) is reserved
for use as specified if any of its associated headers
is included; unless explicitly stated otherwise (see
7.1.4).

-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.143)

____________________

143The list of reserved identifiers with external linkage
includes errno, setjmp, and va_end.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

Jan 13 '07 #8
CBFalconer wrote:
Harald van D?k wrote:
CBFalconer wrote:
Harald van D?k wrote:
sam_...@yahoo.co.in wrote:

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.

No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.
Citation, please?
From N869:

7.1.3 Reserved identifiers
7.1.3 applies, but the part that applies is what follows the list of
cases: "No other identifiers are reserved." size_t does not start with
an underscore, its header is not included, and the standard definition
of it has no linkage, so none of the cases that say identifiers are
reserved apply. (You missed one, by the way.)

Jan 13 '07 #9
CBFalconer wrote:
Harald van D?k wrote:
CBFalconer wrote:
Harald van D?k wrote:
sam_...@yahoo.co.in wrote:

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.

No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.
Citation, please?
From N869:

7.1.3 Reserved identifiers

[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.

-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.

-- All identifiers that begin with an underscore are
always reserved for use as identifiers with file scope
in both the ordinary and tag name spaces.

-- Each macro name in any of the following subclauses
(including the future library directions) is reserved
for use as specified if any of its associated headers
is included; unless explicitly stated otherwise (see
7.1.4).

-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.143)

____________________

143The list of reserved identifiers with external linkage
includes errno, setjmp, and va_end.
You left out

-- Each identifier with file scope listed in any of the following
subclauses (including the future library directions) is reserved
for use as a macro name and as an identifier with file scope in
the same name space if any of its associated headers is included.

So which case applies to an identifier that does not start with _, does
not have its header included, and the standard definition of which has
no linkage? (My apologies if I reply to your message more than once;
I'm having some problems.)

Jan 13 '07 #10
CBFalconer <cb********@yahoo.comwrites:
Harald van D?k wrote:
>CBFalconer wrote:
>>Harald van D?k wrote:
sam_...@yahoo.co.in wrote:

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,
>
void *malloc(size_t size);
>
so what is size_t is it a pre-defined typedef to int?

size_t is a typedef for some unsigned integer type meant to be
capable of storing the size of objects. It is not pre-defined: it
is defined in several of the standard headers, but if you don't
include any of them, you're free to use size_t as an ordinary
identifier in your own code. Which specific type it has depends
on the implementation, and it would be best not to assume
anything more than you need to about it.

No, you are not free to use it for other purposes. You MAY get
away with it on some implementations. You are not free to use
anything defined in any standard headers.

Citation, please?
>>From N869:

7.1.3 Reserved identifiers

[#1] Each header declares or defines all identifiers listed
in its associated subclause, and optionally declares or
defines identifiers listed in its associated future library
directions subclause and identifiers which are always
reserved either for any use or for use as file scope
identifiers.
So <stddef.hand a couple of other headers declare size_t. Based
only on what we've seen so far, if I don't include a header that
declares size_t, I can declare it myself.
-- All identifiers that begin with an underscore and
either an uppercase letter or another underscore are
always reserved for any use.
This doesn't apply to size_t (its name doesn't begin with an
underscore).
-- All identifiers that begin with an underscore are
always reserved for use as identifiers with file scope
in both the ordinary and tag name spaces.
This doesn't apply to size_t (its name doesn't begin with an
underscore).
-- Each macro name in any of the following subclauses
(including the future library directions) is reserved
for use as specified if any of its associated headers
is included; unless explicitly stated otherwise (see
7.1.4).
This doesn't apply to size_t (it's not a macro name).
-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.143)
This doesn't apply to size_t. Only identifiers that denote objects or
functions have linkage; typedef names do not.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 13 '07 #11
sa*****@yahoo.co.in writes:
I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?
This is the kind of question that can be answered by looking up
"size_t" in any decent C reference. Do you have one? Even if you
don't, you can download a free copy of a draft of the C standard; it's
not easy reading, but it's the definitive reference.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 13 '07 #12
<sa*****@yahoo.co.inwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Hi Everyone,

I was looking at the function prototype of malloc() function in
stdlib.h and i found that to be,

void *malloc(size_t size);

so what is size_t is it a pre-defined typedef to int?

Thanks in advance.
In the good old days of 'C' when integers were 32 bits and 2^32 was more
memory (and probably disk space) than any machine could ever hope to have,
the model of having everything be an integer was fine.

But, over the years, trouble crept in. Specifically, machines might now
have more memory than the most convenient size of integer for the
architecture.

And so, the need for size_t.

Things were simpler when everything was an integer and type definitions were
fast and loose. We should move immediately to 256-bit integers and that
should keep everything simple for a hundred years or so.
------------------------------------------------------------
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 14 '07 #13

"David T. Ashley" <dt*@e3ft.comwrote in message
<sa*****@yahoo.co.inwrote in message
Things were simpler when everything was an integer and type definitions
were fast and loose. We should move immediately to 256-bit integers and
that should keep everything simple for a hundred years or so.
Problem is that committees react to changes by proliferating the number of
language constructs. It always easier to add than to remove, with the result
that the standard steadily deteriorates.
64-bits will hold an address space for many years to come. int should be 64
bits. Then practically everything can just use ints and be done with it.
If the machine has a 64-bit adress bus, the amount of memory used by an
integer is unlikely to be a problem, except maybe in one or two big arrays.
So the memory-conscious can use bitfields in the normal case when those
records are structures. short will be either 16 or 32 bits in the rare cases
that you need a shorter integer.

It does of course inconvenience a very few people who want both 32 bit and
16 bit integers. Compilers can easily provide platform-specific options. So
we are left with a few people who need to write portable code and need fixed
integer sizes and can't use bitfields. No change is ever perfect. Removal of
long / long long types and size_t from the vast majority of code is a big
enough benefit to outweigh the problems this small group will face.
Jan 14 '07 #14

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

Similar topics

2
by: xuatla | last post by:
Hi, I am not sure whether I describe the subject correctly. What I want to do is for a matrix class --------------------------- class CMatrix { private:
12
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com...
11
by: raghu | last post by:
Hello Every one, I am writing a program in gcc compiler which contains many functions. In main I created memory for a variable and tried to free in another function. But I am getting error as...
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
10
by: somenath | last post by:
Hi All, I was trying to write a function which will read one line from a specified file and return the line. It is currently working fine. But it would be very much helpful for me if some one...
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...
2
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
3
by: Giuseppe:G: | last post by:
Hi, any idea on what is this code doing: //================================= 1 #include <boost/scoped_ptr.hpp> .... 3 typedef std::vector<std::vector<pair<size_t, size_t ParamsType; 5 /*
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
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
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
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...
1
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.