new alignment defaults?

This is a discussion on new alignment defaults? within the C++ Programming forums, part of the General Programming Boards category; What is the default alignment of data/objects allocated by new?...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    new alignment defaults?

    What is the default alignment of data/objects allocated by new?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    At least 8 bytes. Most likely 16 or more. To really find out, you'd have to make some experiments or analyze the code that actually allocates memory. Just do somehting like this:
    Code:
       for(int i = 0; i < SOMELARGENUMBER; i++)
       {
         int x;
         void *p = new char;
         x = (int)p;
         if (x & (4-1)) unalign4++;
         if (x & (8-1)) unalign8++;
         ...
        }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > What is the default alignment of data/objects allocated by new?
    At least enough to support any of the native data types your compiler supports.

    Anything else is way off into implementation specific territory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As hinted above, you can write something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct amap {
        int alignmask;
        char *name;
        int  count;
    } amap;
    
    #define T(x) { x-1, "Align" #x, 0 }
    
    amap arr[] = {
        T(4),
        T(8),
        T(16),
        T(32),
        T(64),
        T(128),
        T(256)
    };
    
    #define arrsize(a) (sizeof(a) / sizeof(a[0]))
    
    int main()
    {
        int i;
        int j;
        for(i = 0; i < 100; i++)
        {
    	char *p = malloc(32);
    	int v = (int) p;
    	for(j = 0; j < arrsize(arr)-1 && !(arr[j+1].alignmask & v); j++) ;
    	arr[j].count++;
        }
        for(i = 0; i < arrsize(arr); i++)
    	printf("%s: %d\n", arr[i].name, arr[i].count);
        return 0;
    }
    However, as Salem explains, that is implementation dependent, and there is no guarantee that under some circumstances the alignment will be different from another time (e.g. it depends on what the allocation was before this allocation). But in gcc-mingw, the above example gives an output that shows that the only guaranteed alignment is 8. This may change if you use a different version of compiler/C library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alignment tutorial needed!!
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2008, 04:41 AM
  2. Template Defaults
    By Cactus_Hugger in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2008, 01:25 AM
  3. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 08:34 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. trouble with printf alignment
    By hyaline in forum C Programming
    Replies: 5
    Last Post: 09-22-2001, 01:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21