FIX: Operator ++ In Return Statement May Cause Access Violation (205681)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q205681

SYMPTOMS

A compiled program may cause access violation if it contains a function returning a struct by value, and the return expression has either a postfix increment or decrement operator.

RESOLUTION

There following are workarounds for this problem.
  1. Remove the postfix increment/decrement operator from the return expression.
  2. Use the prefix increment/decrement operator.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This bug was corrected in Visual Studio 6.0 Service Pack 3. For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:

194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed

MORE INFORMATION

To reproduce the problem, build and run the following sample program.


Sample
----------------

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
 int   type;
 int   offset;
}
ST;

int i;

struct
{
 ST  *tab;   
 int used;  
}
STAdmin;

ST MyFunction ()
{
#ifdef WORKAROUND_1
   int temp = i++;
   return STAdmin.tab[temp];
#endif

#ifdef WORKAROUND_2
   return STAdmin.tab[++i - 1];
#endif<BR/>

   return STAdmin.tab[i++];
}

void main(void)
{
   ST a;
   int i;

   STAdmin.tab = (ST*) malloc(sizeof(ST)*10);
   STAdmin.used = 0;

   // Initialize the Array 
   for(i=0;i<10;i++)
   {
       STAdmin.tab[i].offset=i;
       printf("%d\n",STAdmin.tab[i].offset);
   }

   // Read in the Array 
   for(i=0;i<10;i++)
   {
	a = MyFunction();
	printf("%d\n",a.offset);
   }
}
// End of Sample Code
				

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:kbbug kbfix kbVS600sp3fix KB205681