Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Variable / Object Initialization

Mark_D_1
Beginner
431 Views

When I declare and initialize objects with:

WCHAR MyString[1024] = {};

AStruct MyStructure = {};

 

The Intel compiler actually places a block of memory the size of the struct or array filled with '0' in the rdata section and copies the zeros into the object at run time... This behavior is bloating my compiled binary. 

 

The Microsoft compiler handles these initializations by doing a memset to zero on the new object. Is there a flag or option to have the Intel compiler use memset versus statically allocating the zeros and copying them on initialization?

 

Thanks!!

0 Kudos
6 Replies
Mark_D_1
Beginner
431 Views

Here is a specific full code example:

 

#include <stdio.h>

typedef struct bigStruct{
	char aList[2048];
}bigStruct;

int main(){
	bigStruct myStruct; // No additional static zeros in the compiled binary
	//bigStruct myStruct = bigStruct(); // No additional static zeros in the compiled binary
	//bigStruct myStruct = {}; // sizeof(bigStruct) zeros added to the compiled binary
	//bigStruct myStruct = { 0 }; // sizeof(bigStruct) zeros added to the compiled binary
	//bigStruct myStruct{}; // sizeof(bigStruct) zeros added to the compiled binary

	for (int i = 0; i < (sizeof(myStruct.aList) / sizeof(char)) - 1; ++i){
		printf("Char Val %d, Index %d\n",myStruct.aList,i);
		myStruct.aList = 0;
	}

	return 0;
}

 

0 Kudos
TimP
Honored Contributor III
431 Views

I suppose you would need to do this explicitly by removing the initializer and writing in the memset or fill() or using new and value initialization.

0 Kudos
Bernard
Valued Contributor I
431 Views
@Mark I think that you can remove from the for loop costly array size division operation and perform it at compile time. If not optimized out by the compiler it can take ~20 CPU cycles.
0 Kudos
jimdempseyatthecove
Honored Contributor III
431 Views
typedef struct bigStruct{
 char aList[2048];
 bigStruct() { memset(aList, 0, sizeof(*this)); }
} bigStruct;
Jim Dempsey
0 Kudos
TimP
Honored Contributor III
431 Views

Jim's suggestion may need -Qansi-alias to optimize.  That became a default in icpc 15.0 for linux.

0 Kudos
jimdempseyatthecove
Honored Contributor III
431 Views

Tim,

Oops,  Correction

memset(this, 0, sizeof(*this);

This is for use with POD objects that you need to zero.

Do not use if encapsulated struct/class objects are not POD.

I've been using that since ~1992. (Borland Turbo C++)

Jim Dempsey

0 Kudos
Reply