NO

Author Topic: Alignment  (Read 2104 times)

Grincheux

  • Guest
Alignment
« on: December 12, 2019, 08:59:13 PM »
Are we sure that strings are four bytes aligned when compiling in 64 bits?

Example:

char szNullString[] = "\0" ;
char szString1[] = "1" ;
char szString1[] = "12" ;
char szString1[] = "123" ;
char szString1[] = "1234" ;

is it equal to the following asm code?

szNullString Byte 0,0,0,0
szString1 BYTE "1",0,0,0
szString2 BYTE  "12",0,0
szString3 BYTE "123",0
szString4 Byte "1234",0,0,0,0

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2144
Re: Alignment
« Reply #1 on: December 14, 2019, 11:31:05 AM »
NO, even msvc does that ?
May the source be with you

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Alignment
« Reply #2 on: December 15, 2019, 05:05:09 PM »
No reason to give char a stricter alignment than byte (I'm not convinced many assembler does it either).

In C11 mode you can always override the default alignment:

Code: [Select]
#include <stdalign.h>
...
alignas(int) char string[] = "...";
...
/Pelle

Offline Vortex

  • Member
  • *
  • Posts: 896
    • http://www.vortex.masmcode.com
Re: Alignment
« Reply #3 on: December 15, 2019, 06:41:50 PM »
Hi Grincheux,

Testing this  code :

Code: [Select]
#include <stdio.h>

int main()
{
char szNullString[] = "\0" ;
char szString1[] = "1" ;
char szString2[] = "12" ;
char szString3[] = "123" ;
char szString4[] = "1234" ;

return 0;
}

The diassembly of the object module :
Code: [Select]
.386
option dotname
.model flat

public _main

_text   SEGMENT PARA PUBLIC 'CODE'

_main   PROC NEAR
        push    ebp
        mov     ebp, esp
        sub     esp, 16
        mov     byte ptr [ebp-2H], 0
        mov     byte ptr [ebp-1H], 0
        mov     byte ptr [ebp-4H], 49
        mov     byte ptr [ebp-3H], 0
        mov     byte ptr [ebp-7H], 49
        mov     byte ptr [ebp-6H], 50
        mov     byte ptr [ebp-5H], 0
        mov     byte ptr [ebp-0BH], 49
        mov     byte ptr [ebp-0AH], 50
        mov     byte ptr [ebp-9H], 51
        mov     byte ptr [ebp-8H], 0
        mov     byte ptr [ebp-10H], 49
        mov     byte ptr [ebp-0FH], 50
        mov     byte ptr [ebp-0EH], 51
        mov     byte ptr [ebp-0DH], 52
        mov     byte ptr [ebp-0CH], 0
        mov     eax, 0
        mov     esp, ebp
        pop     ebp
        ret
_main   ENDP

_text   ENDS
Code it... That's all...

Grincheux

  • Guest
Re: Alignment
« Reply #4 on: December 17, 2019, 09:04:45 PM »
I think it would be better to give the string a length with modulo 4 is 0.
I will try this.

Code: [Select]
#include <stdalign.h>
...
alignas(int) char string[] = "...";

Thx