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

  • Home
  • SEARCH
  • 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 6767763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:59:25+00:00 2026-05-26T14:59:25+00:00

So, I have a function which returns a pointer to a dynamically allocated double,

  • 0

So, I have a function which returns a pointer to a dynamically allocated double, and I need to print the value to which it points (silly I know).

double * ans1p = dot(v1, v2);
    double ans1 = *ans1p;
    cout << "Answer to problem 1:" << endl;
    cout << ans1 << endl;
    cout << *ans1p << endl;

The first cout statement returns the expected value.

The second returns something random and unexpected. What’s the difference? It seems to be that in both cases what’s being printed is the value pointed to by ans1p.

I’d rather eliminate ans1 and its cout statement entirely.

OK, here is the dot function:

double * dot(Vector &v1, Vector &v2) {
if (getLength(v1) != getLength(v2) ) {
    std::cout << "Error: cannot perform dot product. Vectors must be " <<
        "equal length." << std::endl;
    return 0;
}
double result = 0;
for (unsigned int i = 1; i <= getLength(v1); ++i) {
    result += (v1.elements[i] * v2.elements[i]);
}
double * resultPtr = new double;
resultPtr = &result;
return resultPtr;
}

For the record, I wouldn’t attempt to do it this way, but it’s part of an assignment. And I’m supposed to return the 0 pointer in the event of mismatched sizes.

OK, looking at it, I can see that result is out of scope when the function returns, and the pointer I’m returning just points there (right?). But then, I can’t think of how to do what I want to do.

I suppose I could declare a double pointer outside the function, pass the pointer as an argument, and set it inside the function. But I’d kind of like to figure out how to do it the way I planned. Although, thinking about it, it might be better to do it this way once I understand what’s going on with the old way, right?

  • 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-26T14:59:25+00:00Added an answer on May 26, 2026 at 2:59 pm

    There’s your problem right there:

    double * resultPtr = new double;
    resultPtr = &result;
    return resultPtr;
    

    You allocate a new double on the heap… then assign the address of a local variable to resultPtr. You get a memory leak (what was allocated with new double is lost), and you then return the address of said local variable. That’s undefined behavior. Which means anything can happen.

    To fix this, you must change the assignment to actually use the memory you just allocated:

    double * resultPtr = new double;
    *resultPtr = result; // assign the value to the value pointed to resultPtr
    return resultPtr;
    

    Or, better yet:

    return new double(result);
    

    Considering the OP has to use a pointer, then this is irrelevant. Left as a bonus (and because I wrote this anyway, deleting it would be a waste).

    Or you could also drop the whole memory allocation and return result. doubles are pretty light, and I really don’t see why you absolutely must return a pointer rather than the value itself:

    double dot(Vector &v1, Vector &v2) {
        // check here and throw an exception if needed instead of returning 0
    
        double result = 0.0;
        for (unsigned int i = 1; i <= getLength(v1); ++i) {
            result += (v1.elements[i] * v2.elements[i]);
        }
        return result; // return the value
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a c function which returns a long double . i'd like to
I have a function address_of , which returns a Pointer (encapsulating a shared_ptr )
I have a function which returns a one-sided intersection of values between two input
I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
If I have a function which returns a reference cursor for a query, how
Say, i have a function which returns a reference and i want to make
i have a php function which returns a random json encoded color <?php function
If you have a C function which returns an integer, you could write a
I have around 8-9 parameters to pass in a function which returns an array.

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.