3

In C/C++ each library has its own data types for primitive types. For example:

byte, word64, DWORD, LWORD, uint, unsigned int, size_t, ...

However in high-level languages such as Java and C#, everything is uniform (across all libraries).

In C/C++ programming, [beginner] people really get confused which data type should they use. For example, byte is defined as unsigned char, so why do we need byte at all?

I think this could be a source for many memory leaks and other problems (such as many vulnerabilities) at least for beginners. I still can't figure out what's the point of having such data types while a minimal set of them is enough?

The same problem is for null pointer as we have:

NULL, 0, _null, ...

And all are defined as 0.


Update #1:

As @CoryKramer stated the reason could be because of cross-platform compatibility, and interoperability. So, another question which comes to my mind, is that why don't standards define a uniform, cross-platform and inter-operable data types?

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 9
    A combination of historical reasons, cross-platform compatibility, and interoperability much of which are too broad for a Stack Overflow answer. – Cory Kramer Dec 10 '15 at 20:38
  • @CoryKramer historical reasons? – frogatto Dec 10 '15 at 20:40
  • 2
    One reason is that integer widths vary across platforms. Thankfully, we have standard `[u]intN_t` types now. Also, there's `nullptr` for a proper null pointer. I've never seen `_null`. – chris Dec 10 '15 at 20:40
  • 1
    Yes, specifically things like `DWORD` and `LWORD`, [read here](https://stackoverflow.com/questions/2995251/why-in-c-do-we-use-dword-rather-than-unsigned-int) – Cory Kramer Dec 10 '15 at 20:41
  • C does not define `byte`. Possible some compiler you used does that various reasons, but the language does not do it. – chux - Reinstate Monica Dec 10 '15 at 20:43
  • Same reason as we have 2,3,4,5,6,7,8,9 when 0,1 as a minimal set is enough? For a truly minimal set see [Turing Machine](https://en.wikipedia.org/wiki/Turing_machine) – chux - Reinstate Monica Dec 10 '15 at 20:45
  • @chris: `nullptr` isn't C, but C++. – alk Dec 10 '15 at 20:46
  • One reason is just the fact that there's typedef in C and C++, but not in C#. You can make your own types, but basically they are just aliases. C++11 and nullptr solves the NULL issues. – juzzlin Dec 10 '15 at 20:46
  • Please understand that C and C++ are different languages. It mostly never is a good idea to tag question C *and* C++. – alk Dec 10 '15 at 20:47
  • To answer your update, they *do* have fixed-size integral types for those reasons, they're in the [``](http://en.cppreference.com/w/cpp/header/cstdint) header – Cory Kramer Dec 10 '15 at 20:52
  • If doing C, check if POSIX support/compatibility is an option and switch to using the types from `stdint.h`: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html – alk Dec 10 '15 at 20:52
  • @chris `_null` is the pre C++11 GCC version of `nullptr`. – sbabbi Dec 10 '15 at 20:53
  • @CoryKramer So, why don't _all_ libraries comply those standards for data types and instead define their own data types using `typedef`? – frogatto Dec 10 '15 at 20:55
  • 1
    Because C is a "free" language, not "owned" by anybody? ;-) (since long) – alk Dec 10 '15 at 21:00
  • 2
    @HiI'mFrogatto - Some of these code bases are from long before C++ was standardized. Windows with DWORD, LPARAM etc started out in 1985! Also, Java and C# can *get away* with a single size for each type, because they both only run on a single platform, the JVM or .NET. C and C++ are designed to be able to run on a *very* wide variety of systems. – Bo Persson Dec 10 '15 at 21:00
  • @sbabbi, Interesting, thanks. I'm surprised I didn't know that then. – chris Dec 10 '15 at 21:02
  • @BoPersson: "*Java ... because .. [it] ... only run[s] on a single platform...*" Really? – alk Dec 10 '15 at 21:03
  • @alk - Yes, really. Before you can run Java on your platform, you have to port a JVM. And that one is standardized. – Bo Persson Dec 10 '15 at 21:06
  • @BoPersson: ok but on the other hand, there is also Rust, which runs on most systems and also has only standard integer types. So having something like the JVM or .NET is not really the reason that a language can get away with this, that's only incidental. You don't need to have a special runtime to have guaranteed fixed size integer types -- you just might not be as portable if you do – Chris Beck Dec 11 '15 at 00:05
  • @BoPersson: Sure, you are correct. I somehow fell back to some piece other discussion ... ;-) – alk Dec 11 '15 at 07:22

0 Answers0