473,748 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ 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 1571
On 13 Jan 2007 01:39:22 -0800, sa*****@yahoo.c o.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.c o.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.c o.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.c o.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.c o.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(si ze_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.c o.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

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

Similar topics

2
1331
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
10717
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 http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
11
1709
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 segmentation fault at some times or glibc free Invalid Pointer at some times. Can any one of you help me out. But I must free the data. A(){ mem= (char *)malloc(sizeof(char));
14
2799
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> #define SIZE_OF_DST 3 int main(void)
10
1626
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 point out some improvement points or some error in the code. Regards, Somenath
89
5752
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 right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
2
3260
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 objects. It will be good, if explained with example. Appriciate your help in this regard. Thanks,
3
1166
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
2695
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 allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
8983
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6792
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.