returning an address warning

G

Gary Wessle

Hi

I am getting a warning when I run this program, I am interested to
know why the compiler is warning.

thank you

**************** warning ****************
$ make clean; make
rm -rf *.o proj
g++ -c -o useful.o useful.cpp
useful.cpp: In member function 'int* count_rows_cols::get_counts()':
useful.cpp:31: warning: address of local variable 'x' returned
g++ -c -o useful_test.o useful_test.cpp
g++ -Wall -o proj useful.o useful_test.o


**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <string>

class count_rows_cols
{
int nRows, nCol;
std::string file_name;
void set_No_of_Rows_Cols();

public:

count_rows_cols(std::string const& fileName);
~count_rows_cols();

int* get_counts();

};


#endif


**************** useful_h.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "useful.h"

using namespace std;

count_rows_cols::count_rows_cols( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
set_No_of_Rows_Cols();
}
void count_rows_cols::set_No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

string word;
while(input >> word)
nCol++; // init'd by constructor

while (getline(in, line))
nRows++; // init'd by constructor
}
count_rows_cols::~count_rows_cols() {}

int* count_rows_cols::get_counts(){
int x[2];
x[0] = nRows;
x[1] = nCol;
return x;
}


/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/

**************** useful_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "useful.h"

using namespace std;

int main() {
string f = "address.txt";
count_rows_cols x( f ); // space delimited
int* vd = x.get_counts();
cout << vd[0] << "\t" << vd[1] <<"\n";

}
 
V

Victor Bazarov

Gary said:
I am getting a warning when I run this program, I am interested to
know why the compiler is warning.

Because it's a VERY BAD IDEA(tm) to return the address of a local
object.
thank you

**************** warning ****************
$ make clean; make
rm -rf *.o proj
g++ -c -o useful.o useful.cpp
useful.cpp: In member function 'int* count_rows_cols::get_counts()':
useful.cpp:31: warning: address of local variable 'x' returned
g++ -c -o useful_test.o useful_test.cpp
g++ -Wall -o proj useful.o useful_test.o


**************** useful.h ****************
#ifndef USEFUL_H
#define USEFUL_H
#include <string>

class count_rows_cols
{
int nRows, nCol;
std::string file_name;
void set_No_of_Rows_Cols();

public:

count_rows_cols(std::string const& fileName);
~count_rows_cols();

int* get_counts();

};


#endif


**************** useful_h.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "useful.h"

using namespace std;

count_rows_cols::count_rows_cols( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
set_No_of_Rows_Cols();
}
void count_rows_cols::set_No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

string word;
while(input >> word)
nCol++; // init'd by constructor

while (getline(in, line))
nRows++; // init'd by constructor
}
count_rows_cols::~count_rows_cols() {}

int* count_rows_cols::get_counts(){
int x[2];
x[0] = nRows;
x[1] = nCol;
return x;

By the time this function returns, 'x' doesn't exist any more.
Any attempt to dereference the pointer received from this function
results in undefined behaviour.
}


/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/

**************** useful_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "useful.h"

using namespace std;

int main() {
string f = "address.txt";
count_rows_cols x( f ); // space delimited
int* vd = x.get_counts();
cout << vd[0] << "\t" << vd[1] <<"\n";

}

V
 
T

Thomas J. Gritzan

Gary said:
Hi

I am getting a warning when I run this program, I am interested to
know why the compiler is warning. [...]
int* count_rows_cols::get_counts(){
int x[2];
x[0] = nRows;
x[1] = nCol;
return x;
}

You return the adress of a of a local object here. After the return, 'x'
is not valid any more.

You have to return by value. If you want to return more values, use a
std::pair<>, or a std::vector<>:

std::pair<int,int> count_rows_cols::get_counts()
{
return std::make_pair(nRows, nCol);
}
 
G

Gary Wessle

thanks
how do you cout the elements of the pair, I tried -> and [] and
first/second for no avail.
 
T

Thomas J. Gritzan

Gary said:
thanks
how do you cout the elements of the pair, I tried -> and [] and
first/second for no avail.

Please quote some context.
Please post what you have tried. first and second should work:

#include <iostream>
#include <utility>

int main()
{
std::pair<int,int> p = std::make_pair(10, 50);

std::cout << p.first << ", " << p.second << std::endl;
}
 
D

Default User

Gary said:
thanks
how do you cout the elements of the pair, I tried -> and [] and
first/second for no avail.


Please quote enough of the previous message for context.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top