0

As I explained in many questions, I'm trying to move a software from a 32-bit system to a 64-bit system. I had some problem with malloc() function, but now I solved it by correcting a parameter.

In that part of my code, if I run on a 32-bit system, I can use:

(int**) malloc (const * sizeof(int))

But, on a 64-bit system, I have to use:

(int**) malloc (const * sizeof(int64_t))

I'd like to manage these crossroads with an if() condition, so I need a boolean isIt64system() function that behaves this way:

if(isIt64system()) then [64-bit code]

else [32-bit code]

Does this function exist in C++? Is there any function that tells me if software's running on a 32-bit system or 64-bit system?

Community
  • 1
  • 1
DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
  • So you mean to say you are running the same binary on the two different platforms? – Matt Apr 19 '12 at 12:31
  • 1
    possible duplicate of [Determining 32 vs 64 bit in C++](http://stackoverflow.com/questions/1505582/determining-32-vs-64-bit-in-c) – Anonymous Apr 19 '12 at 12:33
  • 3
    The correct way to do it would be `(int**) malloc (count * sizeof(int*))` or even better `new int*[count]`. – Henrik Apr 19 '12 at 12:33
  • You seem to be confusing the host operating system with how your application is built. You can't redefine malloc at runtime. It must be compiled as 32-bit or 64-bit, and in either case it will take an argument of type size_t. On PC platforms size_t will be 32 bits wide if compiled as 32-bit, or 64 bits if compiled as 64-bit. There's no way to change that at runtime. – Carey Gregory Apr 19 '12 at 12:50
  • @Henrik: or even better to use vectors ;) – PlasmaHH Apr 19 '12 at 13:03
  • Not an exact dupe since Davide's justification allows for a more elegant solution. – Robᵩ Apr 19 '12 at 13:27

3 Answers3

5

Rather than writing two size-dependent branches, just write one correct, portable code path. In your case:

(int**)malloc(count*sizeof(int*));

This will work correctly regardless of the sizeof of int* on your system.


Postscript: As you can see from this literal answer to your question, you are better off not having an if:
if(sizeof(int*) == sizeof(int))
    x = (int**)malloc(count*sizeof(int));
else if (sizeof(int*) == sizeof(int64_t))
    x = (int**)malloc(count*sizeof(int64_t));

Hopefully you can see how absurdly redundant that code is, and how it should be replaced by a single well-constructed malloc call.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks. Would other solution `new int*[count]` the same? – DavideChicco.it Apr 19 '12 at 13:48
  • 1
    That solution would also be correct, and would be more appropriate for a C++ program. Note that any pointer allocated with `new[]` must be released with `delete[]`, any pointer allocated with `malloc()` must be released with `free()`. – Robᵩ Apr 19 '12 at 13:59
1

Your compiler will have preprocessor defines that will let you check 32bit versus 64bit.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
0

The best would be to use something like this

#ifdef __LP64__
    <64bit code>
#else
    <32bit code>
#endif

But if you really need a function for it then this should work.

bool is64bit() {
    if (sizeof(int*) == 4) {
        return false;
    }
    return true;
}
MacAndor
  • 205
  • 2
  • 6