473,586 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector return by value: on stack?

Hi

If I have a function:

std::vector<int my_rbv()
{
std::vector<int x;
// some stuff
return x;
}

will the returned vector arrive on the stack when this function gets called?

Thanks,

NL

Dec 6 '07 #1
3 2925
LR
NewLine wrote:
Hi

If I have a function:

std::vector<int my_rbv()
{
std::vector<int x;
// some stuff
return x;
}

will the returned vector arrive on the stack when this function gets called?

Maybe.

For sake of argument, let's suppose it does.

I'm going to assume that you're concerned about the overhead associated
with placing a std::vector<on the stack.

There's probably not much for that. The memory that a std::vector<>
uses when you add elements to it isn't allocated on the stack and AFAIK
the sizeof(std::vec tor<YourTypeHer e>) will be constant.

You might want to try this little piece of code for yourself.

#include <iostream>
#include <vector>

int main() {

std::vector<int v;
for(int i=0; i<5; i++) {
std::cout << i << " " << sizeof(v) << " " <<
v.size() << std::endl;
v.push_back(100 );
}

}

On my system, the output for this code is:

0 16 0
1 16 1
2 16 2
3 16 3
4 16 4

Which implies that if a std::vector<int is returned on the stack it'll
take up 16 bytes on my system, no matter how many elements the
std::vector has.

I haven't taken a look at the standard, but I suspect much of this will
be implementation specific, so Your Mileage Will Almost Certainly Vary.

And as I suggested, there may be other mechanisms for returning the
value. You may want to search for "Name Return Value Optimization" or
similar.

I found this
http://blogs.msdn.com/slippman/archi.../03/66739.aspx article
about a particular implementation, so the compiler you use is almost
certain to differ in this regard.

Also, AFAIK I don't think there is a requirement for a C++
implementation to have a stack. At least not a hardware stack. Which
implies that there may other mechanisms for returning values from functions.

LR


Dec 6 '07 #2
LR a écrit :
NewLine wrote:
>Hi

If I have a function:

std::vector<in tmy_rbv()
{
std::vector<in tx;
// some stuff
return x;
}

will the returned vector arrive on the stack when this function gets
called?
I assume you are actually asking wether the vector elements will be put
on the stack.

That depends on your vector implementation. Some STL implementation may
allocate an initial vector<object that can hold a limited number of
elements (let say 10) and then use the heap when the size exceeds it; I
would not expect it but it is possible.

Of course, it is possible that optimisations make that your
std::vector<int x will never actually exists in the memory space.
>

Maybe.

For sake of argument, let's suppose it does.

I'm going to assume that you're concerned about the overhead associated
with placing a std::vector<on the stack.

There's probably not much for that. The memory that a std::vector<>
uses when you add elements to it isn't allocated on the stack and AFAIK
the sizeof(std::vec tor<YourTypeHer e>) will be constant.
sizeof() is computed at compile type so you can safely expect it to be
constant.
>
You might want to try this little piece of code for yourself.

#include <iostream>
#include <vector>

int main() {

std::vector<int v;
for(int i=0; i<5; i++) {
std::cout << i << " " << sizeof(v) << " " <<
v.size() << std::endl;
v.push_back(100 );
}

}

On my system, the output for this code is:

0 16 0
1 16 1
2 16 2
3 16 3
4 16 4

Which implies that if a std::vector<int is returned on the stack it'll
take up 16 bytes on my system, no matter how many elements the
std::vector has.

I haven't taken a look at the standard, but I suspect much of this will
be implementation specific, so Your Mileage Will Almost Certainly Vary.

And as I suggested, there may be other mechanisms for returning the
value. You may want to search for "Name Return Value Optimization" or
similar.
NRVO is a compiler optimisation.
The compiler is free to do anything provided the execution present the
same behavior as guaranteed by the standard, the only thing you can do
it for such matters is test it for your plateform with your specific
version of compiler and with your specific set of compiler options.

Also, AFAIK I don't think there is a requirement for a C++
implementation to have a stack. At least not a hardware stack. Which
implies that there may other mechanisms for returning values from
functions.
Dec 6 '07 #3
Michael DOUBEZ wrote:
NRVO is a compiler optimisation.
The compiler is free to do anything provided the execution present the
same behavior as guaranteed by the standard,
Btw, if the compiler uses return value optimization, is a valid copy
constructor required even if it's never called? (IOW, does the compiler
check if the copy constructor can be called even though it never
actually generates the code that calls it?)
Dec 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9781
by: Hitesh Bhatiya | last post by:
Hi all, I have written a small program to accept some socket connections, which are then added to a vector (using push_back). But after a few calls to the push_back function, it deleted the object that was added last. Could someone please tell me why this happens ? Am I doing something wrong here ?
9
3200
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
8
6842
by: James Brown | last post by:
Hi, I am using the std::vector class as follows: vector <myclass *> stack1; and am pushing myclass objects onto the end of the vector like so: myclass *ptr = new myclass(); stack1.push_back(ptr);
34
4148
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want...
32
69667
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
3
6540
by: capes | last post by:
Hi, I have a structure called Tissue and I use it in a matrix like this: vector < vector < Tissue* tissueArray(cRows); This whole function works great for a matrix size of 2X2 to 15X15. Beyond that it crashed with a STATUS_STACK_OVERFLOW exception!!! 1. Why does his happen? 2. How do I fix it? 3. At some point I may go to a 300X300...
4
1695
by: Christian Schmidt | last post by:
Hi all, I'm trying to implement a std::vector-like wrapper for IList. The hard part seems to be operator, because it returns an unmanaged reference. Probably I have to use pin_ptr to achieve this, but I don't know how. Can anybody help? Thanks, Christian
8
14648
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't include all of it- it's over 1k lines long). In addition, I'm including an "include" file where structures like "stack" are defined. Again, it's really...
10
2187
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.