where is the return value of functions stored?

e.g
1
2
std::getline(typeFile, line2)
const char *p = line.c_str();


where is the value of line.c_str() stored?
in the stack area or constant area?

and after returning, will this storage for the return value be released or reclaimed? so that p point to a free storage?

thank you
closed account (S6k9GNh0)
constant area ?? Return values are simply copies of the original variable your wanting to return. If it's not assigned immediately, you have no way of reaching the value again because the original variable and all local variables declared are deallocated off the stack and no longer used. If you were to return char *p it would NOT work because line had been deallocated and placed out of scope. In order to fix this you would have to move outside of the stack which is where local variable are stored and you'd have to use the new operator or malloc to allocate to the heap.

EDIT: Ugh..Someone else explain, I don't know if I explained correctly :/
Last edited on
closed account (y04L1hU5)
My question is about c++

I am trying do a system program.
in this program the user can format his disks and another things...
but when I use system code
for example:
#include <stdlib.h>
system("format d:")

the computer is giving alert ! :
are you sure to format d,
please press enter if you are sure...

then the user press enter .
But I dont want to show this to user.

I want to my program press enter himself and format.
dont show this to the user.

& i have also tried this code

system( "echo.|format d: >nul 2>&1" );

but it does not work for drives & only work for usb devices.....
plz help me....
You are trying to do something that is not really kosher... but here is what I found:
http://www.ureader.com/msg/1474253.aspx

The SHFormatDrive() function will require the user to agree -- but that is a Good Thing.

Good luck!
Omg, you did the same thing I was going to do, responded to the spam post! Report him...
Last edited on
Yoinks! So I did!

Why can't people get their own threads?


nanger
The value assigned to p is typically stored in a CPU register or on the stack.

If you are asking about the array of characters it points to... it just references the std::string's internal buffer. (All STL implementations do this -- though I don't think they are required to do so.) That's why wonky stuff like the following work:
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <windows.h>

std::string GetWindowsDirectory()
  {
  UINT length = GetWindowsDirectoryA( NULL, 0 );
  std::string result( length -1, '\0' );
  GetWindowsDirectory( const_cast <char*> (result.c_str()), length );
  return result;
  }

Just keep in mind that anything that modifies the string may cause the buffer to be reallocated (meaning that anything you previously got from ::c_str() is not safe to consider valid to cache anywhere.)

Hope this helps.
Topic archived. No new replies allowed.