Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 21st, 2006, 05:56 PM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default What's wrong with this code?

char* PutDataInMemory (const CString &rkcstrData)

{



int nDataSize = rkcstrData.GetLength();



char buffer[2000];

char *pdata = buffer;



for (int nCounter=0; nCounter < nDataSize; nCounter++)

{

pdata[nCounter] = rkcstrData.GetAt(nCounter);

}



return pdata;



}



Reply With Quote
  #2 (permalink)  
Old September 26th, 2006, 11:46 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

the problem with you code is that you are returning a local variable outside the function (out side the scope)

when a function is called, all it's local variables are created in the stack, so your buffer[2000] is in the stack, and when the function returns the local variables does not exist any more.

you can do this :

//================================================== ==========//
void PutDataInMemory(const CString &rkcstrData, char* buffer){
   int nDataSize = rkcstrData.GetLength();

   for(int nCounter=0; nCounter<nDataSize; nCounter++){
      buffer[nCounter] = rkcstrData.GetAt(nCounter);
   }
}


Reply With Quote
  #3 (permalink)  
Old November 25th, 2006, 08:17 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i agree to the one previous


Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
what's wrong with my code? DyerOppenheimer BOOK: Beginning Ajax with ASP.NET 0 January 7th, 2008 08:46 AM
What's wrong with this code? AlDugan XSLT 3 May 19th, 2006 12:06 PM
What is wrong with code? rtr1900 Classic ASP Databases 1 April 3rd, 2006 03:20 AM
What is wrong with this code? SaharaWizard VB Databases Basics 2 January 28th, 2006 08:20 AM
what's wrong with this code? miguel.ossa ASP.NET 1.0 and 1.1 Basics 2 January 21st, 2004 11:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.