0

define const char aa bb cc variables, the alignment of memory map seems likes difference between const char bb and const char bb[1]

const char aa = 0;
const char bb = 1;
const char cc = 2;

.rodata.aa
                0x0005dbb0        0x1 ./source/main.o
                0x0005dbb0                aa
.rodata.bb
                0x0005dbb1        0x1 ./source/main.o
                0x0005dbb1                bb
.rodata.cc
                0x0005dbb2        0x1 ./source/main.o
                0x0005dbb2                cc
const char aa = 0;
const char bb[1] = {1};
const char cc = 2;

.rodata.aa
                0x0005dbb0        0x1 ./source/main.o
                0x0005dbb0                aa
*fill*          0x0005dbb1        0x3
.rodata.bb
                0x0005dbb4        0x1 ./source/main.o //why align to 4 byte here?
                0x0005dbb4                bb
.rodata.cc
                0x0005dbb5        0x1 ./source/main.o
                0x0005dbb5                cc

why, const char bb[1] align to 4 byte ?

how to make const char bb[1] align to 1 byte, like const char bb

yyd
  • 33
  • 5
  • What does `printf("%d\n", CHAR_BIT);` print on that platform? – Ted Lyngmo Oct 28 '22 at 17:37
  • Do you get the same result if you enable optimizations? – ikegami Oct 28 '22 at 17:51
  • Please fix your formatting. /// Please use different names for the different variables. Asking for the difference `bb` and `bb` is needlessly confusing. – ikegami Oct 28 '22 at 17:56
  • @TedLyngmo Thank you for replying, my IDE the `CHAR_BIT` is `#define _EWL_CHAR_BIT 8` – yyd Oct 28 '22 at 18:06
  • @ikegami Thank you for replying, i have tried `-O2` or `-O0`, both optimizations `bb[1]` aligned to 4 byte – yyd Oct 28 '22 at 18:11
  • That doesn't answer Ted's question `_EWL_CHAR_BIT` is not `CHAR_BIT`. (No idea why @TedLyngmo think this matters, since the question is the same whether `CHAR_BIT` is 8, 32 or something in between.) – ikegami Oct 28 '22 at 18:18
  • @ikegami I was thinking that a `CHAR_BIT` of 32 could explain it but I was just clutching at straws. Molly, you could `#include ` and then do `printf("%d\n", CHAR_BIT);` to give the proper answer though. – Ted Lyngmo Oct 28 '22 at 18:21
  • Sorry for my English, and this is my first-time using Stack overflow. I just want to know the rule of the alignment of char array in my complier. thank you – yyd Oct 28 '22 at 18:22
  • @TedLyngmo , I typed `CHAR_BIT` and open declaration, find `#define CHAR_BIT _EWL_CHAR_BIT`, and open declaration of `_EWL_CHAR_BIT`, find `#define _EWL_CHAR_BIT 8`. because the embedded development board is at company, I cannot try `printf` now. – yyd Oct 28 '22 at 18:29
  • @molly The macro definitions you find like that may or may not be relevant. Compiling the program (`printf("%d\n", CHAR_BIT);`) for your target platform and then running it will give the proper answer. – Ted Lyngmo Oct 28 '22 at 18:30
  • hi @TedLyngmo, I checked the `CHAR_BIT` is 8. using code `const char test = CHAR_BIT`, and check the map address of `test`, and the address value at srec file is 8. @ikegami FYI – yyd Oct 28 '22 at 18:46
  • @molly Ok, it was a wild-goose chase on my part then, – Ted Lyngmo Oct 28 '22 at 18:52
  • hi, @TedLyngmo Is there any reason to explain the question. – yyd Oct 28 '22 at 19:20
  • molly: As I mentioned when @ikegami wondered about my queston too, I thought that `CHAR_BIT` might have been 32 on your platform and that that would explain the alignment. Don't think about it anymore. I'll leave my ill-educated questions here to prevent anyone else from going down that rabbit-hole. – Ted Lyngmo Oct 28 '22 at 19:23
  • Re "*and that that would explain the alignment*", No it wouldn't. The addresses are clearly in terms of `char`-sized units since &cc = &bb + 1 – ikegami Oct 28 '22 at 19:51
  • @TedLyngmo, i use my target platform and then running it, `printf("%d\n", CHAR_BIT)` is 8. – yyd Oct 29 '22 at 07:15
  • Hi, @ikegami, using code `const char bb[1] __attribute__ ((aligned (1))) = {1}` is helpful to align `bb[1]` at 1 byte. Looking forward to hearing from you. – yyd Oct 29 '22 at 12:16

0 Answers0