So I want to make an executable with three functions:
This would be an example:
float vec(int opt, int t)
{
int i;
float * v = new float[t];
// Do stuff with v
return * v;
}
}
It gets arguments opt and t from main. Then main needs to get v back to use it in another function.
What is the best way to do this?
The return type of vec should be float *, and your return statement should be return v. Dereferencing v with * just gives you the first element of the array.
I'll note that this kind of design is bad style. It relies on the caller to free the dynamically allocated array. It would be better to have the caller create an array and pass it by reference to vec, or have vec return a different container statically. #Mat suggested std::vector; that's probably a good choice.
You also have an extra } at the end of your function. Get rid of it.
Well, your signature says that the function returns a single float, not an array of floats. To answer your question directly...
float *vec(int t)
{
float * v = new float[t];
// Do stuff with v
return v;
}
What is the best way to do this?
Well that's a different question entirely. The "best" way would be to return a std::vector<float> and avoid manual memory management altogether.
Related
Suppose I have the following function:
void sum(const std::vector<int*>& input) {
return ... ; // the sum
}
I store a vector of int pointers somewhere
...
std::vector<std::unique_ptr<int>> my_ints;
Is there a way to pass my_ints to sum() without any extra allocations such as an intermediate vector of the unique_ptrs converted to a vector of raw pointers?
Obviously, I could refacor sum() to take a vector of unique ptrs instead. Or overload it. But I'm hoping to find a way where I don't have to, and let the user decide whether or not to use a vector of unique_ptrs or raw pointers.
Not like you want, but you should think about sum() differently. It looks like an algorithm that operates on a range, so you should make it more like this:
template <typename It>
ValueType sum(It begin, It end) {
// ... iterate and calculate sum
return sum;
}
Then suddenly, you can start to use ranges to do cool things!
std::vector<std::unique_ptr<int>> my_ints;
auto range = my_ints | ranges::views::transform
(
[](auto smart_ptr) {
return smart_ptr.get();
}
);
This is a range that will transform as you use it! Then you could enter it into your sum() like this:
auto my_sum = sum(std::begin(range), std::end(range));
Also look up std::accumulate(), which does what you want here, I would say.
No, there is absolutely no way to pass those pointer values to that sum method without changing the method.
I got this library of mathematical routines ( without documentation ) to work on some task at college. The problem I have with it is that all of its functions have void return type, although these functions call one another, or are part of another, and the results of their computations are needed.
This is a piece of ( simplified ) code extracted from the libraries. Don't bother about the mathematics in code, it is not significant. Just passing arguments and returning results is what puzzles me ( as described after code ) :
// first function
void vector_math // get the (output) vector we need
(
double inputV[3], // input vector
double outputV[3] // output vector
)
{
// some variable declarations and simple arithmetics
// .....
//
transposeM(matrix1, matrix2, 3, 3 ); // matrix2 is the result
matrixXvector( matrix2, inputV, outputV) // here you get the result, outputV
}
////////
// second function
void transposeM // transposes a matrix
(
std::vector< std::vector<double> > mat1, // input matrix
std::vector< std::vector<double> > &mat2, // transposed matrix
int mat1rows, int mat1columns
)
{
int row,col;
mat2.resize(mat1columns); // rows
for (std::vector< std::vector<double> >::iterator it=mat2.begin(); it !=mat2.end();++it)
it->resize(mat1rows);
for (row = 0; row < mat1rows; row++)
{
for (col = 0; col < mat1columns; col++)
mat2[col][row] = mat1[row][col];
}
}
////////
// third function
void matrixXvector // multiply matrix and vector
(
std::vector< std::vector<double> > inMatrix,
double inVect[3],
double outVect[3]
)
{
int row,col,ktr;
for (row = 0; row <= 2; row++)
{
outVect[row]= 0.0;
for (ktr = 0; ktr <= 2; ktr++)
outVect[row]= outVect[row] + inMatrix[row][ktr] * inVect[ktr];
}
}
So "vector_math" is being called by the main program. It takes inputV as input and the result should be outputV. However, outputV is one of the input arguments, and the function returns void. And similar process occurs later when calling "transposeM" and "matrixXvector".
Why is the output variable one of the input arguments ? How are the results being returned and used for further computation ? How this kind of passing and returning arguments works ?
Since I am a beginner and also have never seen this style of coding, I don't understand how passing parameters and especially giving output works in these functions. Therefore I don't know how to use them and what to expect of them ( what they will actually do ). So I would very much appreciate an explanation that will make these processes clear to me.
EXTRA :
Thank you all for great answers. It was first time I could barely decide which answer to accept, and even as I did it felt unfair to others. I would like to add an extra question though, if anyone is willing to answer ( as a comment is enough ). Does this "old" style of coding input/output arguments have its name or any other expression with which it is referred ?
This is an "old" (but still popular) style of returning certain or multiple values. It works like this:
void copy (const std::vector<double>& input, std::vector<double>& output) {
output = input;
}
int main () {
std::vector<double> old_vector {1,2,3,4,5}, new_vector;
copy (old_vector, new_vector); // new_vector now copy of old_vector
}
So basically you give the function one or multiple output parameter to write the result of its computation to.
If you pass input parameters (i.e. you don't intend to change them) by value or by const reference does not matter, although passing read only arguments by value might be costly performance-wise. In the first case, you copy the input object and use the copy in the function, in the latter you just let the function see the original and prevent it from being modified with the const. The const for the input parameters is optional, but leaving it out allows the function to change their values which might not be what you want, and inhibits passing temporaries as input.
The input parameter(s) have to be passed by non-const reference to allow the function to change it/them.
Another, even older and "C-isher" style is to passing output-pointer or raw-arrays, like the first of your functions does. This is potentially dangerous as the pointer might not point to a valid piece of memory, but still pretty wide spread. It works essentially just like the first example:
// Copies in to int pointed to by out
void copy (int in, int* out) {
*out = in;
}
// Copies int pointed to by in to int pointed to by out
void copy (const int* in, int* out) {
*out = *in;
}
// Copies length ints beginning from in to length ints beginning at out
void copy (const int* in, int* out, std::size_t length) {
// For loop for beginner, use std::copy IRL:
// std::copy(in, in + length, out);
for (std::size_t i = 0; i < length; ++i)
out[i] = in[i];
}
The arrays in your first example basically work like pointers.
Baum's answer is accurate, but perhaps not as detailed as a C/C++ beginner would like.
The actual argument values that go into a function are always passed by value (i.e. a bit pattern) and cannot be changed in a way that is readable by the caller. HOWEVER - and this is the key - those bits in the arguments may in fact be pointers (or references) that don't contain data directly, but rather contain a location in memory that contains the actual value.
Examples: in a function like this:
void foo(double x, double output) { output = x ^ 2; }
naming the output variable "output doesn't change anything - there is no way for the caller to get the result.
But like this:
void foo(double x, double& output) { output = x ^ 2; }
the "&" indicates that the output parameter is a reference to the memory location where the output should be stored. It's syntactic sugar in C++ that is equivalent to this 'C' code:
void foo(double x, double* pointer_to_output) { *pointer_to_output = x ^ 2; }
The pointer dereference is hidden by the reference syntax but the idea is the same.
Arrays perform a similar syntax trick, they are actually passed as pointers, so
void foo(double x[3], double output[3]) { ... }
and
void foo(double* x, double* output) { ... }
are essentially equivalent. Note that in either case there is no way to determine the size of the arrays. Therefore, it is generally considered good practice to pass pointers and lengths:
void foo(double* x, int xlen, double* output, int olen);
Output parameters like this are used in multiple cases. A common one is to return multiple values since the return type of a function can be only a single value. (While you can return an object that contains multiple members, but you can't return multiple separate values directly.)
Another reason why output parameters are used is speed. It's frequently faster to modify the output in place if the object in question is large and/or expensive to construct.
Another programming paradigm is to return a value that indicates the success/failure of the function and return calculated value(s) in output parameters. For example, much of the historic Windows API works this way.
An array is a low-level C++ construct. It is implicitly convertible to a pointer to the memory allocated for the array.
int a[] = {1, 2, 3, 4, 5};
int *p = a; // a can be converted to a pointer
assert(a[0] == *a);
assert(a[1] == *(a + 1));
assert(a[1] == p[1]);
// etc.
The confusing thing about arrays is that a function declaration void foo(int bar[]); is equivalent to void foo(int *bar);. So foo(a) doesn't copy the array a; instead, a is converted to a pointer and the pointer - not the memory - is then copied.
void foo(int bar[]) // could be rewritten as foo(int *bar)
{
bar[0] = 1; // could be rewritten as *(bar + 0) = 1;
}
int main()
{
int a[] = {0};
foo(a);
assert(a[0] == 1);
}
bar points to the same memory that a does so modifying the contents of array pointed to by bar is the same as modifying the contents of array a.
In C++ you can also pass objects by reference (Type &ref;). You can think of references as aliases for a given object. So if you write:
int a = 0;
int &b = a;
b = 1;
assert(a == 1);
b is effectively an alias for a - by modifying b you modify a and vice versa. Functions can also take arguments by reference:
void foo(int &bar)
{
bar = 1;
}
int main()
{
int a = 0;
foo(a);
assert(a == 1);
}
Again, bar is little more than an alias for a, so by modifying bar you will also modify a.
The library of mathematical routines you have is using these features to store results in an input variable. It does so to avoid copies and ease memory management. As mentioned by #Baum mit Augen, the method can also be used as a way to return multiple values.
Consider this code:
vector<int> foo(const vector<int> &bar)
{
vector<int> result;
// calculate the result
return result;
}
While returning result, foo will make a copy of the vector, and depending on number (and size) of elements stored the copy can be very expensive.
Note:
Most compilers will elide the copy in the code above using Named Return Value Optimization (NRVO). In general case, though, you have no guarantee of it happening.
Another way to avoid expensive copies is to create the result object on heap, and return a pointer to the allocated memory:
vector<int> *foo(const vector<int> &bar)
{
vector<int> *result = new vector<int>;
// calculate the result
return result;
}
The caller needs to manage the lifetime of the returned object, calling delete when it's no longer needed. Faililng to do so can result in a memory leak (the memory stays allocated, but effectively unusable, by the application).
Note:
There are various solutions to help with returning (expensive to copy) objects. C++03 has std::auto_ptr wrapper to help with lifetime management of objects created on heap. C++11 adds move semantics to the language, which allow to efficiently return objects by value instead of using pointers.
I'm new to C++ programming and i want to make a function that gives you all the common dividers into an array, and afterwards use it in int main()
Is returning an array from a function possible :D?
Thank you!
Code: http://pastebin.com/K8195wzF
Returning a dynamically-sized container like std::vector has the best use case here:
#include <vector>
...
std::vector<int> dividers(int x, int y) {
std::vector<int> divi;
...
return divi;
}
Returning by value creates a copy, ensuring no dangling pointers. Such is the case when returning a local array through a pointer: the memory will be lost when the function exits leaving the pointer to point to garbage.
Yes it is possible to return an array in form of a pointer, but this is not recommended as you won't know its size in advance. But take care not to return the address of a local variable (allocated on the stack!!). Another problem with your code is that you didn't initialize d. The preferred C++ way would be to return a vector of the type (you do not need d anymore)
#include <vector>
....
vector<int> dividers(int y,int x){
int yx;
vector<int> divi;
divi.reserve(100);
if(y > x){
yx = y;
}
if(x > y){
yx = x;
}
for (int n = 1; n <= yx; n++){
if ((n%y==0) && (n%x==0)){
divi.push_back(n);
}
}
return divi;
}
You should read about iterators too to modify the loop that is in your main function.
Big mistake in my code removed... Thanks for pointing it out. I did the same mistake with vector that what you had with the array. Fixed now... Stupid me...
No it's not possible. You could return a vector, or you could pass a pointer to the first element of an array to your function.
Looking at your code, the pointer option would look something like this
int dividers(int y,int x, int* divi){
...
}
int main()
{
int divi[100];
...
dividers(x, y, divi);
}
The vector solution might look like this
std::vector<int> dividers(int y,int x){
std::vector<int> divi;
...
divi.push_back(n); // when you want to add a divider to your vector
...
return divi;
}
int main()
{
std::vector<int> divi;
...
divi = dividers(x, y);
}
You could do the following:
typedef int arrT[10]
arrT *func() {}
whereby one is defining arrT to be an alias of an array of 10 ints or
int (*func())[10]{}
whereby one is defining func when dereferenced to return an array of 10 ints.
A using declaration such that using arrT = int[10] is also possible.
As others have noted, returning a std::vector from a function containing the type desired is possible with a function declared as std::vector<int> func(){}.
Also, you could use another container std::array<> and forget built-in arrays altogether.
Reference: C++ Primer; Lippman, Lajoie, Moo
I have a std::vector of type boost::variant which contains pointers to standard built-in types.
In code
std::vector<boost::variant<int *, double *> > bv;
bv.resize(2);
int n = 42;
bv[0] = &n;
double d = 102.4;
bv[1] = &d;
This works well, and I can access the values in the vector by using boost::get<int *> or boost::get<double *>.
However, I was wondering if I could set the value of an element in the vector directly, instead of pointing it to a reference. In other words, I would like to do something like this
*bv[0] = 42;
*bv[1] = 102.4;
but this doesn't compile. Does anyone have advice on how to go about this?
Thanks.
EDIT:
Apparently, I don't have enough points to answer my own question, so figured I'd put my solution in the original question instead. Hope this helps someone who stumbles across this page.
I figured I'd answer my own question, in case it helps someone. I wrote a visitor class, that modifies what the pointer in the variant refers to. Here's the code for the visitor class (of course, this can be made a templated class).
class specific_visitor : public boost::static_visitor<>
{
public:
explicit specific_visitor(int i) : i_num(i) { }
explicit specific_visitor(double d) : d_num(d) { }
void operator()(int * i) const
{
*i = i_num;
}
void operator()(double * d) const
{
*d = d_num;
}
private:
int i_num;
double d_num;
};
In order to use it for the vector of variants in the original question, here's the code:
int i_num = 34;
boost::apply_visitor(specific_visitor(i_num), bv[0]);
double d_num = 9.81;
boost::apply_visitor(specific_visitor(d_num), bv[1]);
If you really need pointers, then
the answer is no, you can't,
because - most probably - there is no feasible way
to allocate and later free the needed memory.
If you need to allocate the memory for (almost?) all
the values, then boost::variant<int, double> is
a way to go.
If you need to store pointers to (almost?) all of
the values and allocate memory for a minority of them,
then you need a more complex solution (anyway).
I recently hit a problem and the only way I can see to avoid it is to use const_cast - but I'm guessing there is a way I'm not thinking of to avoid this without otherwise changing the function of the code. The code snippet below distills my problem into a very simple example.
struct Nu
{
Nu() {v = rand();}
int v;
};
struct G
{
~G()
{
for(auto it = _m.begin(); it != _m.end(); it++) delete it->first;
}
void AddNewNu()
{
_m[new Nu] = 0.5f;
}
void ModifyAllNu()
{
for(auto it = _m.begin(); it != _m.end(); it++) it->first->v++;
}
float F(const Nu *n) const
{
auto it = _m.find(n);
// maybe do other stuff with it
return it->second;
}
map<Nu*, float> _m;
};
Here, suppose Nu is actually a very large struct whose layout is already fixed by the need to match an external library (and thus the "float" can't simply be folded into Nu, and for various other reasons it can't be map<Nu, float>). The G struct has a map that it uses to hold all the Nu's it creates (and ultimately to delete them all on destruction). As written, the function F will not compile - it cannot cast (const Nu *n) to (Nu n) as expected by std::map. However, the map can't be switched to map<const Nu*, float> because some non-const functions still need to modify the Nu's inside _m. Of course, I could alternatively store all these Nu's in an additional std::vector and then switch the map type to be const - but this introduces a vector that should be entirely unnecessary. So the only alternative I've thought of at the moment is to use const_cast inside the F function (which should be a safe const_cast) and I'm wondering if this is avoidable.
After a bit more hunting this exact same problem has already been addressed here: Calling map::find with a const argument
This is because the map expects Nu* const, but you have given it a const Nu*. I also find it highly illogical and don't understand why, but this is how it is.
"find" in your case will return a const_iterator. putting:
map<Nu*,float>::const_iterator it = _m.find(n);
...
return it->second;
should work I think.
Since you are in a const method you can only read your map of course, not write/modify it