Access in uninitialized stack

From RAD Studio
Jump to: navigation, search

Go Up to Access Errors


Accessing an uninitialized area of the stack will cause this error. In the following code sample, a pointer to a local variable on the stack is changed inside a function. When the function returns, that part of the stack is no longer valid, and an error occurs when accessed.

#include <tchar.h>
#pragma hdrstop
#include<stdio.h>
#include<dir.h>
class someclass{
	int fnumber;
  public:
	int getnumber(){return fnumber;}
	void setnumber(int nw){fnumber = nw;}
	int doublev(int val){return val*2;}
	int publicalval;

};


void locfunc(int **locp)
{
	int local_var;
	*locp = &local_var;
}

void myf()
	{
		int *locp;
		locfunc(&locp);
		*locp = 10; // error
	}

int _tmain(int argc, _TCHAR* argv[])
{
	myf();
	return 0;
}