Dave Powell
Dave Powell

Reputation: 671

Char type on 32 bit vs 64 bit

Here is the following issue:

If I am developing on a 32 bit machine and want my code to be ported to a 64 bit machine here is the senario.

My function internally use a lot of std strings. Now if I want to provide APIs can I ask them to send char * which I can then use internally? Or ask them to send me a __int64 which I convert to a string?

Another reason to use char * in my API was that at least in one type of implementation of unix (a different version of the tool) it picks up data from stdin via argv which is a char *.

In the Windows version I am not sure what to do. I could just ask for __int64 and then convert it into a string...and make it work that way or just use char * as well?

Upvotes: 1

Views: 14085

Answers (5)

eidolon
eidolon

Reputation: 3393

You seem somewhat confused. If the code you are writing is used only within the target machine, recompile will take care of most of the problems. Just don't rely on specific memory layout and you are fine. Using strings (as opposed to wstrings) probably means that the character encoding is UTF-8 (if not, reconsider) and thus limited form of data exhance (e.g. files) between platforms is also fine.

In this case, your interface decision comes to selecting between (const) std::string(&), and (const) char*, integer_type (don't rely on null terminator, please). Deciding factor being whether or not you anticipate need to support other compilers or programming languages.

Now, if you intent to make the interface callable from other machines (i.e. network interface), you have much tougher job. In that case, specify size of everything explicitly.

Upvotes: 1

Mark B
Mark B

Reputation: 96241

If you're providing a C++ implementation, then your public interface should just use std::string.

If however for compatibility reasons (which it sounds like you may have) you need to provide a C-style interface, then using char* is precisely the way to do it. In your 32-bit library it will be a 32 bit pointer, and in the 64 bit version of the library it will be 64 bits. This will then agree with the client users' expectations regarding the API. You should absolutely convert to a std::string inside your library at the earliest possible point however.

Upvotes: 3

slashmais
slashmais

Reputation: 7155

You would do well to convert to string at the earliest opportunity, it is the recommended/standard for C++, and will help do away with all your char* problems.

There is a few scenarios you can follow to write portable code, see these questions:
What's the funniest user request you've ever had?
How to do portable 64 bit arithmetic, without compiler warnings

You would have problems achieving binary portability between different architectures, C++ provides for source-level portability.

Upvotes: 0

Šimon Tóth
Šimon Tóth

Reputation: 36433

Converting to/from char* doesn't really help if you can't represent the number on your architecture.

If you are converting a 64bit integer from its decimal (or hexadecimal) textual representation into a value, you still need 64bits to store it.

Upvotes: 0

Flinsch
Flinsch

Reputation: 4341

char is always one byte in size, both on 32-bit and 64-bit systems. However, using the std library is not the worst choice. ;) std should cope with different platforms as it is platform independent for the "most" part...

Upvotes: 0

Related Questions