Memory Alignment

This is a discussion on Memory Alignment within the C++ Programming forums, part of the General Programming Boards category; hi all, I have a block of memory, and I am storing arbitrary number of basic types ( floats, ints, ...

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    Memory Alignment

    hi all,

    I have a block of memory, and I am storing arbitrary number of basic types ( floats, ints, chars, and char* ( strings )) in this block of memory.

    --------------------------------------
    | int | int | float .....
    --------------------------------------

    When I want to access these variables, I use a pointer to the first byte of this memory block and an index, something like this

    eg. to access the second int,
    Code:
     char* block_pointer = new char[1024];
     ...
     ...
     // to access the second int
     int second_int = *(block_pointer + sizeof(int));
    my question is, would this approach produce undesired effects ( alignments problems )
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If the integers are not aligned on correct alignment boundry, asscess will be slower but the data should still be ok.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > my question is, would this approach produce undesired effects ( alignments problems )
    Yes.
    Maybe not immediately, but long term it is not a good approach.

    Here's someone else who's managed to dig a pit for themselves
    How to fix misaligned assignment statements in the source code?
    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
    Registered User
    Join Date
    May 2006
    Posts
    12
    If you store the values as ints or floats or the correct type no...
    //value = int type
    *((int*)(block_pointer + sizeof(int))) = int_value;
    //Next value = double...
    *((double*)(block_pointer + 2*sizeof(int))) = double_value;
    //...
    int second_int = *((int*)(block_pointer + sizeof(int)));
    double third_double = *((double*)(block_pointer + 2*sizeof(int)));

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > *((double*)(block_pointer + 2*sizeof(int))) = double_value;
    How does this solve anything?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. memory alignment of a struct
    By George2 in forum C Programming
    Replies: 4
    Last Post: 04-04-2007, 12:34 PM
  4. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM

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