473,748 Members | 4,697 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
13 1572
CBFalconer <cb********@yah oo.comwrites:
Harald van D?k wrote:
>CBFalconer wrote:
>>Harald van D?k wrote:
sam_...@yaho o.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_Keit h) 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.c o.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_Keit h) 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.goog legroups.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.comwr ote 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
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
1627
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
3261
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
1167
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
9530
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
9312
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
9238
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...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4593
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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
2775
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.