C optimization bug - by mimique

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 650346 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/9/2011 3:02:41 AM
Access Restriction Public


The following Win32 console application crashes when Release mode selected (no problems with Debug mode). It seems to me (when looking asm listing) that the compiler incorrectly handles parameters in the push() function when calling it indirectly.

#include <stdio.h>

typedef struct {
	char   name[3];
	int    height;
} Titem;

#define MAXN 1
typedef enum {NOT_OK, OK} Tboolean;
typedef struct {
	Titem array[MAXN];
	Titem *top;
	Titem *maxpointer;
	Titem *minpointer;
} Tstack;

void     initialize_stack (Tstack *Pstack);
Tboolean push( Tstack *Pstack, Titem item);

Tstack stack;
Titem  item;

void testfun(void) {
	push(&stack, item);

void main(void) {
	void (*P)(void);

	printf("This program does not work\n");
	P = testfun;


void initialize_stack (Tstack *Pstack)  {
     Pstack->maxpointer = &(Pstack->array[MAXN-1]);
     Pstack->minpointer = Pstack->array;
	 Pstack->top        = Pstack->minpointer-1;

Tboolean push(Tstack *Pstack, Titem item) {
    if (Pstack->top == Pstack->maxpointer )
	else {
		*++Pstack->top = item;
		return OK;

Sign in to post a comment.
Posted by Eric [MSFT] on 3/11/2011 at 12:31 PM

Thanks very much for including a narrowed down test case which shows the failure. This is indeed a compiler optimization bug that will be fixed in a future release.

If you need a workaround, you can either mark "push" as __declspec(noinline), or rewrite "*++Pstack->top = item" to copy each field one at a time:

        (*++Pstack->top).name[0] = item.name[0];
        (*++Pstack->top).name[1] = item.name[1];
        (*++Pstack->top).name[2] = item.name[2];
        (*++Pstack->top).height = item.height;

Thank you,
Eric - VC++ Compiler Team
Posted by Microsoft on 3/9/2011 at 3:13 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)