473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

vector return by value: on stack?

Hi

If I have a function:

std::vector<intmy_rbv()
{
std::vector<intx;
// some stuff
return x;
}

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

Thanks,

NL

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

If I have a function:

std::vector<intmy_rbv()
{
std::vector<intx;
// 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::vector<YourTypeHere>) will be constant.

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

#include <iostream>
#include <vector>

int main() {

std::vector<intv;
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<intis 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<intmy_rbv()
{
std::vector<intx;
// 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<intx 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::vector<YourTypeHere>) 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<intv;
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<intis 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
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...
9
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...
8
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();...
34
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...
32
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: ...
3
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....
4
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,...
8
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.