How/What to return in this case? - c++

CODE:
vector<DWORD> get_running_proc_list()
{
DWORD proc_list[1024], size;
if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
return 0; // PROBLEM HERE!!
}
vector<DWORD> _procs(proc_list, proc_list + size/sizeof(DWORD));
return _procs;
}
ERROR:
cannot convert from 'int' to 'const std::vector<_Ty>'
What is the best possible way to fix this error?
Is there a better way than just returning an empty vector?

Well, your function returns vector, not DWORD. Can't you return just an empty vector:
return std::vector< DWORD >();
or
return std::vector< DWORD >( 1, 0 );
If you really need the 0?
EDIT:
There's another option, if empty vector is not a solution (in case, that it's some kind of valid value and you need to know) - use exception. You can make your own class for exception or use some standard one. So, you can do it like this:
if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
throw MyException( "some message, if you want" );
}
I'd advice, if you choose this option, to inherit std::exception.
Or, you can return pointer to std::vector and return NULL in this case. But I would not recommend this. But it's just my opinion.

Throw an exception. That's what they're for. After all, there definitely should be a list of processes that are running.
And definitely do not fail to return by value or something. In this case, the RVO/NRVO is trivially applied. I mean, at worst, you could swaptimize.

Edit: (after reading edited version of question)
Alternatives:
You can make your function to return void and pass to it a vector by reference (or by pointer) then fill the vector in function body.
You can make your function to return boost::shared_ptr<vector<DWORD> > (or some other smart pointer), construct and fill the vector in function body (of course in dynamic memory), then return it's address or NULL.
Throw an exception if above solutions are not suitable.

if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
vector<DWORD> empty;
return empty; <--- 0 sized vector
}
You can return an empty vector<>.
As a side note, I would not recommend to return vector by value. Instead pass the vector<> as parameter to be assured that unnecessary copies will not happen.
void get_running_proc_list(vector<DWORD> &_procs) pass by reference and populate
{
...
}

Replace
return 0; // PROBLEM HERE!!
with
return vector<DWORD>(); // NO PROBLEM!!

How about a boost::optional? It adds pointer semantics to normal objects and allows them to be either set or not, without dynamic allocation.
#include <boost/optional.hpp>
typedef boost::optional<std::vector<DWORD>> vec_opt;
vec_opt get_running_proc_list()
{
DWORD proc_list[1024], size;
if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
return 0;
}
vector<DWORD> _procs(proc_list, proc_list + size/sizeof(DWORD));
return _procs;
}
And that's all you need to do, just change the return type. On the calling site:
vec_opt v = get_running_proc_list();
if(v){
// successful and you can now go through the vector, accessing it with *v
vector<DWORD>& the_v = *v;
// use the_v ...
}

You are trying to return two logically distinct bits of information: First, "What are the list of processes?" and second, "Can I compute the list of processes?". I suggest you return those in two distinct variables:
// UNTESTED
bool get_running_proc_list(vector<DWORD>& result)
{
DWORD proc_list[1024], size;
if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
return false;
}
result = vector<DWORD>(proc_list, proc_list + size/sizeof(DWORD));
return true;
}
But, I might try to save a couple memcpy's:
// UNTESTED
bool get_running_proc_list(vector<DWORD>& result)
{
result.clear();
result.resize(1024);
DWORD size;
if(!EnumProcesses(&result[0], result.size()*sizeof(DWORD), &size))
{
result.clear();
return false;
}
result.resize(size/sizeof(DWORD));
return true;
}

Related

return reference in [] operator of std::vector

Please consider this scenario.
I'm creating a function for [] operator as in the std::vector whose inner body is like this:
int temp;
if(argument >= 0 && argument < size) {
return &my_vector[i];
} else {
cout << "out of bounds" << endl;
//i need to return here something but this gives me an error: local variable cannot be returned by reference.
return &temp;
}
where argument is the argument passed to the function. The return type for this function is 'T*'.
I'm looking for both the read and write cases:
vector[100] = 1;
and int answer = vector[100];
That's why they are returned by reference.
What should be returned in the else part?
First of all, you are not returning a reference, but a pointer, which makes the method not very useful. Instead of
vector[100] = 1;
int answer = vector[100];
You would have to write
*vector[100] = 1;
int answer = *vector[100];
To get what you want you should return a reference not a pointer. Ie return type should be T& not T*.
Then you have basically two options. Either you mimic vectors operator[]. It does not do any bounds checking. It is up to the caller to make sure to pass valid indices:
return my_vector[i]; // thats all (assuming my_vector[i] returns a reference)
The other option is to throw an exception as std::vector::at does:
if(i >= 0 && i< size) {
return my_vector[i];
} else {
throw std::out_of_range("invalid index");
}
You cannot return a reference to a local variable, because that reference is dangling as soon as the method returns and the local variables lifetime ended.
You have to choose which approach fits better. std::vector offers both. Typically when the method is called in a loop:
for (size_t i=0; i<size;++i) {
foo[i] = i;
}
You do not want to incur the overhead of checking the index on each access. On the other hand sometimes indices are computed and the caller wants to have the bounds check encapsulated rather than doing it manually:
try {
size_t index = some_function(); // is this a valid index?!?
foo.at(i) = 42; // let at check it
} catch(...) {
//...
}
PS: You typically need two overloads for those accessors, one const and one non-const, returning const T& and T&, respectively.

How to properly handle if a function exits without encoutering a return

I have a function that search a vector and returns the item if it is found. But I want to know that best software appraoch to handle if it is not found.
I have created a function and could return -1 or something but that wouldn't match the return type.
koalaGraph::PVertex Koala::lookUpVertexbyName(const std::string&vertexName, const std::vector<koalaGraph::PVertex>& koalaVertices) {
for (size_t i = 0; i < koalaVertices.size(); i++) {
if(koalaVertices[i]->info.name == vertexName)
return koalaVertices[i];
}
}
If a situation is encountered where the item being searched for is not in the vector then program will exit.
You can use std::optional
#include <optional>
std::optional<koalaGraph::PVertex>
Koala::lookUpVertexbyName(const std::string&vertexName,
const std::vector<koalaGraph::PVertex>& koalaVertices) {
for (size_t i = 0; i < koalaVertices.size(); i++) {
if(koalaVertices[i]->info.name == vertexName)
return koalaVertices[i];
}
return {};
}
int main()
{
Koala k;
//...
auto maybeVertex = k.lookUpVertexByName("foo",vertices);
if(maybeVertex)
koalaGraph::PVertex&& vertex = *maybeVertex;
//alternatively
if(maybeVertex.has_value())
//found
}
You could use a for-loop and return a iterator.
std::vector<koalaGraph::PVertex>::const_iterator
Koala::lookUpVertexbyName(
const std::string&vertexName,
const std::vector<koalaGraph::PVertex>& koalaVertices)
{
for(auto iter = koalaVertices.begin(); iter != koalaVertices.end(); ++iter) {
if(koalaVertices[i]->info.name == vertexName) {
return iter;
}
}
return koalaVertices.end();
}
Further you check if you got end back. end indicates if the value was found or not.
auto iter = <fucntioncall> // lookUpVertexbyName
if (iter == <vector>.end() {
// abort or do what ever you want
}
To use the value you have to dereference the iterator. DON'T derefence the end-iterator, it will lead you to neverland -> undefined behavior.
std::string test = *iter;
Why not use std::find_if instead of reinventing the wheel. See this link.
struct equal
{
equal(const std::string& vertexName) : vertexName_(vertexName) { }
bool operator()(const koalaGraph::PVertex& pVertex) const
{
return pVertex->info.name == vertexName_;
}
private:
std::string vertexName_;
};
And then:
std::find_if(koalaVertices.begin(), koalaVertices.end(), eq(vertexName));
Regarding handling the errors in function as it has already been stated there are multiple approaches that one can take. Returning an iterator instead of object(you will avoid copying this way too) is one of them. end() iterator would then indicate that the name was not found.
There are three ways to exit a function:
Return a value
Throw a value
Call std::abort or std::exit (possibly indirectly)
(std::longjmp which you shouldn't use)
If you don't do any of the above, then behaviour will be undefined. If you don't want to do 1., then your options are 2. or 3. Abort and exit will terminate the process. A throw can be caught, but an uncaught throw will cause std::abort.
Note that just because you don't find a value, it's not necessarily impossible to return some value. What you can do is return a "sentinel" value that represents "not found". For example, std::string functions that return an index will return std::string::npos when there is no result. Functions returning a pointer might return null, and functions returning an iterator would return an iterator the the end of the range.
If there is no representation of your return type that could be reserved for a sentinel, there is a way to add such representation by wrapping the return type with additional state. The standard library has a generic wrapper for this: std::optional.
Another wrapper is the proposed std::expected (it's not accepted to the standard as far as I know, but there are plenty of non-standard implementations). It allows storing information about the reason for not returning a proper value which similar to what you can do with exceptions.
P.S. Your function appears to be nearly identical to std::find_if. Use standard algorithms when possible. Also consider a data structure that is more efficient for searching if the search space is large.

proper way to return a large object or indicate that it is not found

What is the idiomatic C++ way of doing this?
I have a method which looks like this:
LargeObject& lookupLargeObject(int id) {
return largeObjects[id];
}
This is wrong, because if you call this with a non-existent id it will create a new instance of large object and put it into the container. I don't want that. I don't want to throw an exception either. I want the return value to signal that object wasn't found (as it is a more or less normal situation).
So my options are either a pointer or an optional. Pointer I understand and like, but it feels like C++ doesn't want to me use pointers any more.
So on to optionals. I will return an optional and then the caller looks like this:
std::optional<LargeObject> oresult = lookupLargeObject(42);
LargeObject result;
if (oresult) {
result = *oresult;
} else {
// deal with it
}
Is this correct? It feels kind of crappy because it seems that I'm creating 2 copies of the LargeObject here? Once when returning the optional and once when extracting it from optional into result. Gotta be a better way?
Since you don't want to return a pointer, but also don't want to throw an exception, and you presumably want reference semantics, the easiest thing to do is to return a std::optional<std::reference_wrapper<LargeObject>>.
The code would look like this:
std::optional<std::reference_wrapper<LargeObject>> lookupLargeObject(int id) {
auto iter = largeObjects.find(id);
if (iter == largeObjects.end()) {
return std::nullopt;
} else {
return std::ref(iter->second);
}
}
With C++17 you can even declare the iter variable inside the if-condition.
Calling the lookup function and using the reference then looks like this (here with variable declaration inside if-condition):
if (auto const lookup_result = lookupLargeObject(42); lookup_result) {
auto& large_object = lookup_result.value().get();
// do something with large_obj
} else {
// deal with it
}
There are two approaches that do not require use of pointers - using a sentinel object, and receiving a reference, instead of returning it.
The first approach relies on designating a special instance of LargeObject an "invalid" one - say, by making a member function called isValid, and returning false for that object. lookupLargeObject would return that object to indicate that the real object was not found:
LargeObject& lookupLargeObject(int id) {
if (largeObjects.find(id) == largeObjects.end()) {
static LargeObject notFound(false);
return notFound;
}
return largeObjects[id];
}
The second approach passes a reference, rather than receiving it back:
bool lookupLargeObject(int id, LargeObject& res) {
if (largeObjects.find(id) == largeObjects.end()) {
return false;
}
res = largeObjects[id];
return true;
}
If default constructed LargeObject is unwanted from lookupLargeObject, regardless of whether it is expensive or it does not make semantic sense, you can use the std:map::at member function.
LargeObject& lookupLargeObject(int id) {
return largeObjects.at(id);
}
If you are willing to live with use of if-else blocks of code in the calling function, I would change the return type of the function to LargeObject*.
LargeObject* lookupLargeObject(int id) {
auto it = largeObjects.find(id);
if ( it == largeObjects.end() )
{
return nullptr;
}
return &(it->second);
}
Then, client code can be:
LargeObject* result = lookupLargeObject(42);
if (result) {
// Use result
} else {
// deal with it
}

Returning a pointer to a class within a class

this is the first time I've done something like this so I'm a little uncertain how I need to do this. I have a very simple class which contains some simple values and some getters:
class Nucleotide{
private:
char Base;
int Position;
int Polymorphic;
public:
Nucleotide(char ch, int pos);
int getPos();
char getBase();
int getPoly();
};
This class is present in another class that contains a vector of them:
class NucleotideSequence{
private:
std::string Name;
std::vector<Nucleotide> Sequence;
public:
NucleotideSequence(std::string name, std::vector<Nucleotide> seq);
std::string getName();
Nucleotide getBase(int pos1);
};
I want the method of the second class called getBase to be able to take a integer - say 1, and return the first Nucleotide object in the vector. What I've written is below:
Nucleotide NucleotideSequence::getBase(int pos1)
{
for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
{
if(pos1 == (*i).getPos())
{
return i; // Return a pointer to the correct base.
}
}
}
I've got Nucleotide as the return type but I was wondering really how I should change this - since if I return nucleotide because of pass by value would it not just return a copy of the object at that place in the vector? So I'd rather return a pointer/reference. I'm using an iterator in the loop so should I just return a pointer with the value of the iterator? How do I do this? In the function I return i but should I be returning i&? I'm uncertain about the specifics - presumably if I'm returning a pointer my return type needs to be Nucleotide* or perhaps Nucleotide& since & means address of? I've thought this through and read Cpp tuts but I'm still slightly unsure of the right answer.
Thanks,
Ben.
You have to return the Nucleotide by reference:
Nucleotide & NucleotideSequence::getBase(int pos1)
{
for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
{
if(pos1 == (*i).getPos())
{
return *i; // Notice the *i instead of i
}
}
}
A reference works very similarly to pointer (allows you to pass the actual object, not its copy), but cannot be null and cannot point to non-existing object, so it's a lot safer than pointer.
Note though, that if you don't find the desired Nucleotide, you don't return anything, what generally is not a good idea. In this case using pointers may actually be a better idea:
Nucleotide * NucleotideSequence::getBase(int pos1)
{
for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); i++)
{
if(pos1 == (*i).getPos())
{
return &(*i);
}
}
return nullptr;
}
You don't return a pointer, you attempt to return the iterator. And the function is declared to return an instance and not a pointer. Also, if you don't find the Nucleotide you don't return anything at all leading to undefined behavior if you try to use the "returned" value.
You could change the function to return a pointer, or a reference, or just a by value (copying like it's declared like not.
You can also change so that the function takes the Nucleotide as an argument instead, and then return a boolean indicator if it was found or not.
bool NucleotideSequence::getBase(int pos1, Nucleotide& n)
{
for (...)
{
if (...)
{
n = *i;
return true;
}
}
return false; // Not found
}
As far as your question is concerned, returning a reference (&) as suggested by others is the solution.
In order to improve your code, I would as well suggest a change:
Either go for the operator[], or use the at() present in std::vector.
Thus, you can directly say:
return Sequence[pos1]; or return Sequence.at(pos1);
Your code will benefit from some use of references for efficiency's sake. The getBase method signature should look like this:
const Nucleotide& NucleotideSequence::getBase(int pos1)
The NucleotideSequence constructor signature should look like this:
NucleotideSequence(const std::string& name, const std::vector<Nucleotide>& seq);
And the getName method like this:
const std::string& getName();
(Although return value optimisation might make that less important.)
As for the contents of getBase, it might help understanding to break down the code into:
const Nucleotide* NucleotideSequence::getBase(int pos1)
{
for(std::vector<Nucleotide>::iterator i = Sequence.begin(); i != Sequence.end(); ++i)
{
Nucleotide& ref = *i; //Get a reference to the object this iterator points to
if(pos1 == ref.getPos()) //compare its base to the argument
{
return &ref; // Return a pointer to the correct object.
}
}
return NULL; //or null if we didn't find the object we wanted
}

c++ - test a pointer

I am writting a method returning a double*. However, I would like to base another method behavior on output from this method. I would like to have
if (methodReturningArray()==0)
{
this_behavior();
}
else
{
this_other_behavior(methodReturningArray());
}
Is it appropriate then to have methodReturningArray() returning either the 'initialized' or 'build' double* and if this double* could not be appropriately be initialized or build, returning like that
double* new_array ;
return new_array ;
?
In other words, the double* output plays also role of a boolean to check whether some property is completed so that the double* output can be built.
Thanks and regards.
To indicate that something that you return by pointer has not been initialized, use return NULL. And check for it with if(double* d = method()) (or in any other fashion you like).
However, this is not your (or my) grandfathers C++ and you should only write something like this, when you absolutely have reason to do so. I would prefer to return either a std::array or std::vector by value wrapped and throw an exception if the behavior that leads to initialization failure is somehow exceptional. If failing to initialize is part of the idea I'd wrap the return value in a boost::optional. But probably I'd write something that takes an OutputIterator to not force any particular container on my client.
Notes on disaster: double* d; return d will leave your client with a pointer that points to random memory. There is no way for her to figure out if has to be deleted[] or if it is valid. Always initialize your pointers.
Code snippets:
// outputiterator
template<typename OutputIterator>
void myFunc(OutputIterator o) {
// fill stuff in
if(someThing) {
for(int i = 0; i < 5; ++i)
{
*o++ = 23;
}
} else {
// leave it empty
}
}
// client calls like:
std::vector<double> v;
myFunc(std::back_inserter(v));
if(!v.empty()) {
} else {
}
// exception
std::vector<double> myFunc() {
std::vector<double> v;
if(someThing) { v.push_back(23); return v; }
else throw std::runtime_error("Nargh!");
}
// client
try {
auto v = myFunc();
} catch(std::runtime_error err) {
}
// optional
boost::optional<std::vector<double>>
myFunc() {
std::vector<double> v;
if(someThing) { v.push_back(23); return v; }
else return boost::optional< std::vector<double> >();
}
//client
auto v = myFunc();
if(v) {
} else {
}
You got three ways, basically.
1) On error, return NULL. Then you can do boolean checks without issue, and it's sufficient in very most of cases.
2) Return boolean, and handle the double* output using a reference or a pointer argument like this:
bool methodReturningArray(double **out) { *out = ...; return true; }
double *out;
if (!methodReturningArray(&out)) this_other_behavior(out); else ....
3) Throw an exception - IMO kinda convoluted and unuseful.
Returning uninitialized pointer won't allow you to do boolean evaluation on it, and it's dangerous, as such pointer will be assumed dangling pointer afterwards.