How to define alignment for structure member variables

25 01 2018


Every data type has an alignment associated with it which is mandated by the processor architecture rather than the language itself. Aligning data elements allows the processor to fetch data from memory in an efficient manner and thereby improves performance. The compiler tries to maintain these alignments for data elements to provide optimal performance. How can we change it?

Windows provide pack directive to set the alignment for structure, union, and class members.
1. #pragma pack(n) – Sets the new alignment.
2. #pragma pack(push)- Pushes the current alignment setting on an internal stack
3. #pragma pack(pop)- Restores the alignment setting to the one saved at the top of the internal stack

struct Struct1
{
  char a;
  int b;
  char c;
};

//pushes the current alignment setting on an internal stack
#pragma pack(push) 
#pragma pack(1) //sets the alignment 
struct Struct2
{
  char a;
  int b;
  char c;
};
#pragma pack(pop) //restores the alignment

struct Struct3
{
  char a;
  int b;
  char c;
};

#pragma pack(push) //pushes the current alignment 
#pragma pack(2) //sets the alignment 
struct Struct4
{
  char a;
  int b;
  char c;
};
#pragma pack(pop) //restores the alignment

int _tmain(int argc, _TCHAR* argv[])
{
  /*| 1 | 2 | 3 | 4 |

    | a(1) | pad.................. |
    | b(1) | b(2) | b(3) | b(4)    |
    | c(1) | pad.................. | */
  int nSizeWithDefaultPadding = sizeof(Struct1); //12

  /*| 1 |

    | a(1) |
    | b(1) | 
    | b(2) | 
    | b(3) | 
    | b(4) |
    | c(1) |  */
  int nSizeWithPadding1 = sizeof(Struct2);//6
  int nSizeAfterRestored = sizeof(Struct3 );//12

  /*| 1  | 2 |

  | a(1) | pad  |
  | b(1) | b(2) |  
  | b(3) | b(4) |
  | c(1) | pad  |  */
  int nSizeWithPadding2 = sizeof(Struct4);//8
	return 0;
}


In Visual Studio 2015 and later we can use the standard alignas and alignof operators for same.

Advertisements




How to read Unicode text files

16 04 2013

Recently I met with a problem when I wrote some codes to read a file using MFC API CStdioFile. But unfortunately it failed to read UNICODE files :(. Then only I came to know that the MFC classes CFile and CStdioFile can read/write text files only in ANSI format. So how we will resolve it?

Windows provides another API fopen_s for Unicode file streams. This API supports encoding flag, where we cab mention the desired flag while reading a file or writing a file.

fopen_s(&fStream, “UnicodeFile.txt”, “r,ccs=encoding”);

    
BOOL ReadUnicodeFile()
{
    CString strFileName = _T("C:\\UnicodeFile.txt");
    FILE *fStream;
    errno_t errCode = _tfopen_s(&fStream, strFileName, _T("r, ccs=UNICODE"));
    if (0 != errCode)
    {
        return FALSE;
    }
    CStdioFile File(fStream);
    CString strLine;
    while(File.ReadString(strLine))
    {
        //reading line-by-line
    }
}

ccs=ENCODING
Specify the coded character set to use (ANSI, UTF-8, UTF-16LE, and UNICODE) for this file. This option is available in Visual C++ 2005 and later.





Happy new year to all readers

2 01 2013

-2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

19,000 people fit into the new Barclays Center to see Jay-Z perform. This blog was viewed about 59,000 times in 2012. If it were a concert at the Barclays Center, it would take about 3 sold-out performances for that many people to see it.

Click here to see the complete report.