Click here to Skip to main content
Click here to Skip to main content

Is There a Way to Make the type size_t 32-bit in a 64-bit program?

, 2 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Is there a way to make the type size_t 32-bit in a 64-bit program?

Introduction

While porting code from a 32-bit system to a 64-bit one, you may want to make the types size_t/ptrdiff_t 32-bit again to reduce the number of errors and warnings generated by the compiler. This wish is usually justified by the supposition that the program will not have to deal with much memory and many objects. Here is an instance of such a dispute on the forum: "Can use 32 bit size_t in x64 VC2008 STL?".

This is a brief answer to begin with: you must not and should not think that way. Concentrate on correcting errors and warnings. There are a lot of reasons for this answer. Here are some of them.

Suppose you managed to redefine the type size_t as a 32-bit one in your code by resorting to some tricks (typedef, #define). Then:

  1. The code will become incompatible with libraries built with the size_t of the standard size.
  2. You will get more errors. For example:
    #define size_t unsigned
    void Errors(void *ptr, CArray<int> &arr)
    {
      size_t a = (size_t)ptr;
      ptr = (void *)a; //Invalid pointer
      //Infinity loop if array size > UINT_MAX
      for (size_t i = 0; i != arr.GetSize(); i++)
        arr[i] = 0;
    }
  3. Many operations will lead to warnings and become potentially incorrect. For example:
    #define size_t unsigned
    void A(float *p1, float *p2)
    {
      size_t s = p1 - p2; //Warning C4244
    }

Let us summarize. You should not try to "hack" the types size_t/ptrdiff_t and change their sizes. You might need more person-hours to solve the problem of linking your code with libraries and correcting new errors and compiler-generated warnings than to perform refactoring of the code to provide it with the full 64-bit support. Resorting to such a "hacking", you are taking a risk of bringing a lot of hidden defects into the code that will be difficult to detect for a long time. For example, a code where a pointer is stored in a 32-bit integer variable may work quite well for a long time while the pointer refers to an object lying inside the four low-order Gbytes of memory. But the object may be created outside these four low-order Gbytes at any point. And this is most likely to happen while you are actively exploiting the program rather than testing it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Karpov Andrey
Architect Program Verification Systems, Co Ltd
Russian Federation Russian Federation

Andrey Karpov is technical manager of the OOO "Program Verification Systems" (Co Ltd) company developing the PVS-Studio tool which is a package of static code analyzers integrating into the Visual Studio development environment.

PVS-Studio is a static analyzer that detects errors in source code of C/C++ applications. There are 3 sets of rules included into PVS-Studio:

  1. Diagnosis of 64-bit errors (Viva64)
  2. Diagnosis of parallel errors (VivaMP)
  3. General-purpose diagnosis

Awards: MVP, Intel Black Belt

Andrey Karpov is also the author of many articles on the topic of 64-bit and parallel software development. To learn more about the PVS-Studio tool and sources concerning 64-bit and parallel software development, please visit the www.viva64.com site.

Best Articles:

My page on LinkedIn site: http://www.linkedin.com/pub/4/585/6a3

E-mail: karpov@viva64(dot)com

Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 3 Apr 2012
Article Copyright 2012 by Karpov Andrey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid