Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Mark_D_1
Beginner
117 Views

Variable / Object Initialization

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
117 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;
}

 

TimP
Black Belt
117 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.

Bernard
Black Belt
117 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.
jimdempseyatthecove
Black Belt
117 Views

typedef struct bigStruct{
 char aList[2048];
 bigStruct() { memset(aList, 0, sizeof(*this)); }
} bigStruct;
Jim Dempsey
TimP
Black Belt
117 Views

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

jimdempseyatthecove
Black Belt
117 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

Reply