News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Alignment

Started by Grincheux, December 12, 2019, 08:59:13 PM

Previous topic - Next topic

Grincheux

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

TimoVJL

NO, even msvc does that ?
May the source be with you

Pelle

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:

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

Vortex

Hi Grincheux,

Testing this  code :

#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 :
.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

I think it would be better to give the string a length with modulo 4 is 0.
I will try this.

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


Thx