Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:38:20+00:00 2026-05-31T16:38:20+00:00

I am writting a method returning a double* . However, I would like to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:38:21+00:00Added an answer on May 31, 2026 at 4:38 pm

    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 {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm writting junit test how can I test this method .. this is
I have this class with an unbound method and a static class inside: class
Basically I would like to do something like this at the top of my
I have a doubt about orkut's new URL Writing method. Earlier the home page
I'm writing a method to wrap another method as part of a class. It
I writing a simple method which should delete an entity from the database if
One of my base repository classes contains a method: public abstract class RepositoryBase<T, TDb>
I'm writting custom handler interceptor in order to have multilangual url addresses. For example,
So in writing a C++ template class, I have defined a method that returns
I'm writing a method to return an 'asset' row from the database. It contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.