C++ Function Return Pointer to Struct Goes Weird

2019-08-11 16:10发布

问题:

I am working on an assignment which to create a program that can read a polynomial from a text file using linked list. Which when i tried to return the starting pointer of the linked list "poly_pointer" from the read_poly function things being weird.

The expected output should be -12

But what i got is -10

And if i add one single line of code right before return in read_poly

cout << curr_ptr->coef;

the output would suddenly turns to 2-12 May anyone provide some explanation on why and how to fix this problem?

Polynomial.h

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_

using namespace std;

typedef struct poly_node *poly_pointer;

typedef struct poly_node {
  int coef;
  int expon;
  poly_pointer link;
};

poly_pointer addNode(int coef, int expon);

#endif

Polynomial.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Polynomial.h"
using namespace std;

poly_pointer addNode(int coef, int expon)
{
    poly_node a;
    poly_pointer ptr = &a;
    a.coef = coef;
    a.expon = expon;
    return ptr;
}

poly_pointer read_poly(const char* fileName)
{
    poly_pointer start_ptr, curr_ptr;
    start_ptr = curr_ptr = addNode(-1, 6);
    curr_ptr = curr_ptr->link = addNode(2, 3);
    return start_ptr;
}

main.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main(void)
{
    poly_pointer a, b, d, e, f;
    a = read_poly("input1.txt");
    cout << a->coef;
    cout << a->link->coef;

    cout << "\n-eop-";
    cin.get();
    return 0;
}

回答1:

addNode is returning a pointer to a locally allocated poly_node.

The following is sloppy allocation of memory, but will work.

  poly_pointer a = new poly_pointer();
  a->coef = coef;
  a->expon = expon;
  return a;


回答2:

poly_node a;
poly_pointer ptr = &a;
a.coef = coef;
a.expon = expon;
return ptr;

Bad! You returned a pointer to a local variable. Using that pointer after the function exits triggers undefined behaviour. Use malloc or new instead if you want to return a value which lives on past the function's lifetime.



回答3:

Just return it by value. Fine for the struct you have.

poly_node addNode(int coef, int expon)
{
    poly_node a;
    a.coef = coef;
    a.expon = expon;
    return a;
}

If the type is large, then allocate memory on heap and return the pointer. The ownership of releasing the memory is on the caller of the method.

poly_pointer addNode(int coef, int expon)
{
    poly_pointer a = new poly_node();
    a->coef = coef;
    a->expon = expon;
    return a;
}

If you have compiler that supports std::unique_ptr or std::shared_pointer, then use them instead of raw pointers.