-3

I've heard that different micro controllers will have different word length.So the structure padding on a c program differs in 8 bit,16 bit,32 bit and 64 bit micro controllers respectively. If that's the case, also tell me the size of a size of a int and a size of a pointer with word length on those controllers

Meganathan
  • 25
  • 6
  • One can only speak about *specific* architecture with a *specific* compiler (and even *specific* compiler settings). Also, what is "word" in C? – Eugene Sh. Jun 13 '19 at 14:00
  • 4
    It can even vary from compiler to compiler when compiling for the same architecture. The C++ standard offers certain minimum guarantees (e.g. `int` is at least 16 bits) and you can use types with a defined size (e.g. `int64_t`) if they are available. But I don't think you get much more than that. – john Jun 13 '19 at 14:02
  • https://en.wikipedia.org/wiki/Word_(computer_architecture) Just look for your architecture~ – Eugene Mart Jun 13 '19 at 14:04
  • Possible duplicate https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be – john Jun 13 '19 at 14:05
  • 3
    `sizeof(char) <= sizeof(short) && sizeof(short) <= sizeof(int) && sizeof(int) <= sizeof(long) && sizeof(long) <= sizeof(long long)` is generally considered to be true everywhere. – Some programmer dude Jun 13 '19 at 14:05
  • @EugeneMart Machine word size is mostly decoupled from C (or C++) type sizes. – Eugene Sh. Jun 13 '19 at 14:06
  • You will have to use `sizeof()` in your code to determine this. Or you may want to use defined sized types. – drescherjm Jun 13 '19 at 14:07
  • 2
    You've tagged C++, but only mention C in your question. – Thomas Jager Jun 13 '19 at 14:09
  • @eugene-sh size_t and size of a pointer are always equal to machine word. So is float. And a double is twice as big. int, short int, long, long long, may be different. – Eugene Mart Jun 13 '19 at 14:12
  • A machine's **word** size is usually (but not necessarily) the same as C's **int** size. And on some architectures, such as 68000, one could legitimately make claim the **word** size is 16-bit (because of the memory bus) or 32-bit (because of the CPU's register size). – Eljay Jun 13 '19 at 14:13
  • @eugene-sh well, not really. There can be far and near pointer, but that's a detail :D – Eugene Mart Jun 13 '19 at 14:13
  • 2
    @EugeneMart There's no reason that that would be true. – Thomas Jager Jun 13 '19 at 14:13
  • 5
    @EugeneMart That's not true. Lots of microcontrollers have extended addressing modes but can only process smaller amounts of data. This is the case for pretty much every 8 and 16 bit MCU out there. – Lundin Jun 13 '19 at 14:13
  • @EugeneMart These claim require a reference. `float` and `double` are single-precision and double precision as per IEEE 754 (well, a compiler setting might set them both as `double`) without any relation to machine word size which can be a single byte. – Eugene Sh. Jun 13 '19 at 14:14
  • @EugeneSh. • a `float` and `double` are often IEEE 754 or close-to on many platforms, but I don't think the C and C++ standards require any specific implementation. (I presume that's irrelevant to the point you made, though.) – Eljay Jun 13 '19 at 14:16
  • @EugeneSh it seems you're right. Sorry. – Eugene Mart Jun 13 '19 at 14:16
  • @Eljay You are correct and I was about to add that, but omitted. I have never seen any other representation even on 8-bit controllers. – Eugene Sh. Jun 13 '19 at 14:16
  • 1
    @EugeneMart No worries, we learn every day :) – Eugene Sh. Jun 13 '19 at 14:17
  • 1
    In the world of microcontrollers, you can't make generalities. You can probably say that each controller is different and there is a wide variety. There is no guarantee that the word size is the same as the pointer or address size. Some controllers can have large address space, but small word size. With some controllers, the word size depends on its operating mode. The best you can do is to understand the data sheet, and how the addressing and data busses are implemented, then set up your build tools accordingly. – Thomas Matthews Jun 13 '19 at 14:28
  • One example is the 8051 and it's derivatives. The derivatives have expanded the addressing range by using a *paging* system. Some microcontrollers may multiplex the high order addressing bits for GPIO and other purposes. The ARM processors can perform 8 or 32-bit fetching and can accomodate odd address fetching (they can also be configured as a 16-bit word machine). – Thomas Matthews Jun 13 '19 at 14:32
  • The rules of structure padding are simple. Add extra bytes so that members are aligned with the processor's fetching size. If a processor can only fetch 32-bits, then the compiler may insert up to 3 padding bytes to align to 32-bit addresses. The insertion of padding bytes may depend on the optimization setting. Aligning data member addresses to the fetch size is optimal performance. – Thomas Matthews Jun 13 '19 at 14:36
  • This is a C question not a target architecture question. The authors of the C compiler choose the size. Just look at x86 as an example even within compiler product families (okay most folks here are not that old). There can and in cases will exist multiple C compilers for a target that define an int differently. if your goal is to understand struct packing and boundaries you are incorrectly using structs per the C language and will make life more painful. dont use structs incorrectly. Unless your goal is job security and not a good product then by all means go for it. – old_timer Jun 13 '19 at 15:17
  • Answering your question serves little purpose, and is far too broad if you are asking for all possible architectures. Moreover the sizes can vary between compilers and operating systems regardless of the architecture. In cases where it matters, you should use the `` types and compiler specific directives for structure packing. – Clifford Jun 13 '19 at 22:12

1 Answers1

1

What will be the word length, ... on a 8,16,32 & 64 bit micro controllers

Depends on the micro controller. There is a table of word lengths of different CPU architectures on wikipedia. If your chip isn't listed, then you need to consult the manual, or contact the vendor.

size of int & pointer ... on a c program?

These depends on the target system (the specific architecture, as well as possibly the system software as well).

The size of int will be exactly sizeof(int) bytes, which is exactly sizeof(int) * CHAR_BIT bits. It will be no less than 16 bits.

The size of a pointer to T will be exactly sizeof(T*) bytes, which is exactly sizeof(T*) * CHAR_BIT bits. The size of char* and void* will be sufficient to represent all addresses. It is fairly common on modern systems for all data pointers to have the same size, but this is not guaranteed by the standard.

eerorika
  • 232,697
  • 12
  • 197
  • 326