stack memory

stack memory

Hi,Please explain to my basic question, if i am returning a local address from a function, it should throw a warning "returning address of local variable" . but i am not getting any warning as well as getting proper output. according to my understand after return from the function, stack would clear the local memory. then how i am getting the exact output?ThankyouMuthu

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Try -W4 and -W5 (icl specific)

As for getting the correct output, it is just a luck. In this case, the result could vary depending on what happens next.

Jennifer

Quoting muthucrazyPlease explain to my basic question, if i am returning a local address from a function, it should throw a warning "returning address of local variable" . but i am not getting any warning...

[SergeyK] It is possible that it is disabled. For example,for Microsoft C++ compiler you should look
for:

#pragma warning ( disable : 4172 )
I will post a Test-Case some time later.

Best regards,
Sergey

Here isa Test-Case:

...

char * GetStringA( void );
char * GetStringA( void )

{

	char szText[128] = { 0x0 };
//	memset( szText, '1', sizeof( char ) * 32 );

//	memset( szText, 0x31, sizeof( char ) * 32 );

	strcpy( &szText[0], "11111111111111111111111111111111" );
	return ( char * )&szText[0];		// Warning C4172: returning address of local variable or temporary

}
char * GetStringB( void );
char * GetStringB( void )

{

	char *pszText = NULL;
	pszText = new char[32+1];

//	pszText = ( char * )malloc( ( 32+1 ) * sizeof( char ) );
//	memset( pszText, '1', sizeof( char ) * 32 );

//	memset( pszText, 0x31, 32 * sizeof( char ) );

	strcpy( &pszText[0], "11111111111111111111111111111111" );
	return ( char * )&pszText[0];

}
char * GetStringC( void );
char * GetStringC( void )

{

	char *pszText = NULL;
	pszText = new char[32+1];

//	pszText = ( char * )malloc( sizeof( char ) * ( 32+1 ) );
//	memset( pszText, '1', sizeof( char ) * 32 );

//	memset( pszText, 0x31, sizeof( char ) * 32 );
	return ( char * )strcpy( &pszText[0], "11111111111111111111111111111111" );

}

...
void main( void )

{

	...

	// Sub-Test 1

	{

		char *pszTextA1 = NULL;

		pszTextA1 = GetStringA();

		printf( "Sub-Test 1.1 - Text Returned: %sn", pszTextA1 );
		char szTextA2[128] = { 0x0 };

		strcpy( szTextA2, GetStringA() );

		printf( "Sub-Test 1.2 - Text Returned: %sn", szTextA2 );

	}
	// Sub-Test 2

	{

		char *pszTextB1 = NULL;

		pszTextB1 = GetStringB();

		pszTextB1[32] = 0x0;

		printf( "Sub-Test 2.1 - Text Returned: %sn", pszTextB1 );

		if( pszTextB1 != NULL )

			delete [] pszTextB1;

	}
	// Sub-Test 3

	{

		char *pszTextC1 = NULL;

		pszTextC1 = GetStringC();

		pszTextC1[32] = 0x0;

		printf( "Sub-Test 3.1 - Text Returned: %sn", pszTextC1 );

		if( pszTextC1 != NULL )

			delete [] pszTextC1;

	}

	...

}

...and output of the Test-Case is as follows:

...
Sub-Test 1.1 - Text Returned: 11111111111111111111111111111111
Sub-Test 1.2 - Text Returned: 11111111111111111111111111111111
Sub-Test 2.1 - Text Returned: 11111111111111111111111111111111
Sub-Test 3.1 - Text Returned: 11111111111111111111111111111111
...

I used Intel C++ Composer XE 2011 and it provides the correct warning.

c:\>icl tstcase.cpp

Intel C++ Intel 64 Compiler XE for applications running on Intel 64, Version 12.1.1.258 Build 20111011

Copyright (C) 1985-2011 Intel Corporation. All rights reserved.

tstcase.cpp

tstcase.cpp(14): warning #1251: returning pointer to local variable

return ( char * )&szText[0];

// Warning C4172: returning address of local variable or temporary

^

tstcase.cpp(49): warning #1079: return type of function "main" must be "int"

void main( void )

^

Microsoft Incremental Linker Version 10.00.40219.01

Copyright (C) Microsoft Corporation. All rights reserved.

-out:tstcase.exe

tstcase.obj

Quoting Om Sachan (Intel)

...tstcase.cpp(14): warning #1251: returning pointer to local variable

return ( char * )&szText[0]; // Warning C4172: returning address of local variable or temporary
...

It is also reproducible with a MinGW C++ compiler andhere is its output:

...
../../Common/PrtTests.cpp: In function `RTtchar* GetStringA()':
../../Common/PrtTests.cpp:1784: warning: address of local variable `szText' returned
...

Leave a Comment

Please sign in to add a comment. Not a member? Join today