size-t

unsigned int vs. size_t

I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL. I am curious as to the reason for this and the benefits it brings. ...

Cross platform format string for variables of type size_t?

On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is there an elegant way to handle this? ...

overflows in size_t additions

I like to have my code warning free for VS.NET and GCC, and I like to have my code 64 bit ready. Today I wrote a little module that deals with in memory buffers and provides access to the data via a file-style interface (e.g. you can read bytes, write bytes, seek around ect.). As the data-type for current read position and size I used ...

Does "std::size_t" make sense in C++?

In some code I've inherited, I see frequent use of size_t with the std namespace qualifier. For example: std::size_t n = sizeof( long ); It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from C?). Isn't it true that size_t is built into C++ and therefore in the global namespace? Is a ...

64 bit portability issues

All this originated from me poking at a compiler warning message (C4267) when attempting the following line: const unsigned int nSize = m_vecSomeVec.size(); size() returns a size_t which although typedef'd to unsigned int, is not actually a unsigned int. This I believe have to do with 64 bit portability issues, however can someone exp...

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like "long" does? If so, how? [1] en.wikipedia.org/wiki/64-bit#64-bit_data_models ...

size_t can not be found by g++-4.1 or others on Ubuntu 8.1

This has happened before to me, but I can't remember how I fixed it. I can't compile some programs here on a new Ubuntu install... Something is awry with my headers. I have tried g++-4.1 and 4.3 to no avail. g++ -g -frepo -DIZ_LINUX -I/usr/include/linux -I/usr/include -I/include -c qlisttest.cpp /usr/include/libio.h:332: error: ‘siz...

Issue regarding size_t

If you go in my post history you'll see that i'm trying to develop an interpreter for a language that i'm working on. I want to use *size_t* using two different codes, but they all return nothing. Here is the post of what i was trying: http://stackoverflow.com/questions/1215688/read-something-after-a-word-in-c When i try to use the ...

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t, and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t. I can'...

size_t vs. intptr_t

The C standard guarantees that size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type. I've read on some sites that I found on the Googles that this is legal and/or should always work: void *v = malloc(10); size_t s = (size_t) v; So then in C99, the standard introduce...

Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

I have some C++ code that prints a size_t: size_t a; printf("%lu", a); I'd like this to compile without warnings on both 32- and 64-bit architectures. If this were C99, I could use printf("%z", a);. But AFAICT %z doesn't exist in any standard C++ dialect. So instead, I have to do printf("%lu", (unsigned long) a); which is really...

Can we change the size of size_t in C?

Can we change the size of size_t in C? ...

When to use std::size_t?

Hi, I'm just wondering should I use std::size_t for loops and stuff instead of int? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) // std::size_t OK here? Or should I use, say, unsigned int instead? } In general, what is the the best practice regarding when to use std::size_t? ...

Pimpl idiom: What size_type to use if implementation is unknown?

I have a class that holds an array of elements, and I want to give it a GetSize member function. But what return type should I give that function? I'm using the pimpl idiom, and so in the header file it is not known what the implementation will use to store the elements. So I cannot just say std::vector<T>::size_type, for example: cla...

Is size_t only in C++ standard or C standard as well?

Is size_t only in C++ standard or C standard as well? I cannot find a C header in the "/usr/include" tree that defines size_t. If it is not in the C std, is GCC just doing some magic to make things work? Thanks, Chenz ...

What is size_t in C?

Hi, I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly it is? Is it a datatype? Let's say I have a for loop int i; or size_t i; //which one should i use? for(i = 0; i < some_size; i++) ...

Why does converting from a size_t to an unsigned int give me a warning?

I have the code: unsigned int length = strlen(somestring); I'm compiling with the warning level on 4, and it's telling me that "conversion from size_t to unsigned int, possible loss of data" when a size_t is a typedef for an unsigned int. Why!? Edit: I just solved my own problem. I'm an XP user, and my compiler was checking for 64 ...

Should I always include stddef.h if I use sizeof and size_t

Hi, if I'm using the sizeof operator and making use of size_t in my code, do I have necessarily have to include stddef.h? I haven't included stddef.h, and my code compiles without warning with both MVS2008 and with Borland C++ BuilderX. Thanks a lot... ...

casting size_t to int to declare size of char array

I am trying to declare a size of a char array and I need to use the value of the variable that is declared as a size_t to declare that size. Is there anyway I can cast the size_t variable to an int so that I can do that? ...

What is a portable method to find the maximum value of size_t?

I'd like to know the maximum value of size_t on the system my program is running. My first instinct was to use negative 1, like so: size_t max_size = (size_t)-1; But I'm guessing there's a better way, or a constant defined somewhere. ...