Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guidelines on usage of size_t and offset_t?

Tags:

c++

This is probably a C++ 101 question: I'm curious what the guidelines are for using size_t and offset_t, e.g. what situations they are intended for, what situations they are not intended for, etc. I haven't done a lot of portable programming, so I have typically just used something like int or unsigned int for array sizes, indexes, and the like. However, I gather it's preferable to use some of these more standard typedefs when possible, so I'd like to know how to do that properly.

As a follow-up question, for development on Windows using Visual Studio 2008, where should I look to find the actual typedefs? I've found size_t defined in a number of headers within the VS installation directory, so I'm not sure which of those I should use, and I can't find offset_t anywhere.

like image 652
Charlie Avatar asked Jul 31 '09 21:07

Charlie


People also ask

When should Size_t be used?

size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.

Should I use std :: Size_t or Size_t?

So yeah, both are same; the only difference is that C++ defines size_t in std namespace.

What is the purpose of Size_t?

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


1 Answers

You are probably referring to off_t, not offset_t. off_t is a POSIX type, not a C type, and it is used to denote file offsets (allowing 64-bit file offsets even on 32-bit systems). C99 has superceded that with fpos_t.

size_t is meant to count bytes or array elements. It matches the address space.

like image 138
Martin v. Löwis Avatar answered Oct 27 '22 17:10

Martin v. Löwis