🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

64 bit size_t visual studio

Started by
4 comments, last by ddlox 3 years, 5 months ago

I´m dusting of my old engine code that I haven´t worked on for a while. I haven´t worked on it for some years now and last time I worked on it I have been working on linux. My C++ is a bit rusty too.

I have been setting it up to build in visual studio now. Previously I was only building it for 32 bit. Trying to build it with 64 bit I get issues where I use std containers and return size() where size_t is 64bit and the code is set up with 32 bit integers.

I don´t use size_t in my code I have my own data types I32, U32, I64, U64 and so on.. I don´t want to use more memory so I want to still use 32 bit. I could wrap all my size() functions in a static_cast I guess, but that seems a bit messy too.

How do others solve this?

Advertisement

First, using the native size-type can actually result in faster code (if you ie. do pointer-arithmetic with 32 bit-integers in a 64 bit build, it will use an additional instruction). So unless you have specific reasons, you are actually better off using size_t.

I personally do use smaller integer-types for container sizes in certain occasions, for which I have a custom container-type which will ie. as a vector will store (data-pointer, size, capacity) and thus only need the size of two pointers instead of three when used with sizes < size_t. If you just care for the interace, you could actually subclass std::vector with your own variant, and override the properties/methods using size_t:

template<typename Type>
class vector : std::vector<Type>
{
        using vector<Type>::vector;
        
	[[nodiscard]] uint32_t size(void) const noexcept
	{
		return vector<Type>::size();
	}
};

and then you'd use your type instead of std::vector. For local variables though, where there is no reason to specifically optimize for low memory usage, as I said you are better off just using the size_t-type, and in my own code I go for “almost always auto” so there is rarely the need for casting size_t away, unless I specifically want to use a smaller datatype (which only happens occasionally).

In normal usage, size_t is only used for very few values at a time (proportional to the number of collections you are using, not to the number of items in those collections), so making size_t 64 bits instead of 32 (or 16) doesn't cause a significant waste of memory.

For cases in which you have many indices and therefore their size matters (e.g. collections of objects that own indices into an array of something else, like in a typical EC framework) you just need to convert properly between the size you want to store (plausibly even less than 32 bits) and size_t, with range checking if necessary, instead of assuming size_t coincides with the type you use for indices.

Omae Wa Mou Shindeiru

Don't assume that using 32 bit variables uses less memory than 64 bit variables. Remember, total memory used is the sum of memory used for data and the memory used for code. In 64 bit mode, every 32 bit instruction gets an extra prefix. If you're talking about a local variable in a non-recursive function, these prefixes will almost certainly add up to more than the extra four bytes it takes to store a 64 bit variable - and because they're in code, they're always going to be present whereas the data is only going to be present while the function is running.

Using 32 bit variables in 64 bit code is generally only a net win if you've got a lot of instances of that variables, e.g. in a big array.

read this to bring u up-to-date:

https://www.viva64.com/en/a/0004/

long read, enjoy ?

This topic is closed to new replies.

Advertisement