- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[cpp]... 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; } ... }
[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
...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
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
...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
...

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page