0

A mysterious function Function '_ZNSsaSERKSs' appears in my gcov report which i have absolutely no idea what it is , can someone can explain to me what is happening

Thanks

Header file

#ifndef CASHIER_H
#define CASHIER_H
#include <string>
using namespace std;


class cashier 
{
public:

        void setID(string);
    string getID();

    void setPassword(string);
    string getPassword();

    void settries(int);
    int gettries();
        void increase_tries();

private:
    string ID;
    string Password;
    int tries;



};

#endif  /* CASHIER_H */

Implementation file

#include "cashier.h"




void cashier::setID(string value)
{
    this->ID = value;
}

void cashier::setPassword(string value)
{

    this->Password = value;

}

string cashier::getID()
{
    return this->ID;
}

string cashier::getPassword()
{
    return this->Password;
}

void cashier::settries(int value)
{
    this->tries=value;
}
int cashier::gettries()
{
    return this->tries;
}
void cashier::increase_tries()
{
    this->tries = this->tries + 1 ;

}

I type in the following command

gcov -f cashier.gnco

I got the following results B

Function '_ZN7cashier8settriesEi' 
Lines executed:100.00% of 3       


Function '_ZN7cashier8gettriesEv'
Lines executed:100.00% of 2

Function '_ZN7cashier14increase_triesEv'
Lines executed:100.00% of 3

Function '_ZN7cashier11getPasswordEv'
Lines executed:100.00% of 2

Function '_ZN7cashier5getIDEv'
Lines executed:100.00% of 2

Function '_ZNSsaSERKSs' //mysterious function
Lines executed:0.00% of 2

Function '_ZN7cashier11setPasswordESs'
Lines executed:100.00% of 3

Function '_ZN7cashier5setIDESs'
Lines executed:100.00% of 3

File 'cashier.cpp'
Lines executed:100.00% of 18
cashier.cpp:creating 'cashier.cpp.gcov'

File '/usr/include/c++/4.4/bits/basic_string.h'
Lines executed:0.00% of 2
/usr/include/c++/4.4/bits/basic_string.h:creating 'basic_string.h.gcov'





 File '/usr/include/c++/4.4/bits/basic_string.h'
    Lines executed:0.00% of 2
    No branches
    Calls executed:0.00% of 1
    /usr/include/c++/4.4/bits/basic_string.h:creating 'basic_string.h.gcov

EDIT

Computernerd
  • 7,378
  • 18
  • 66
  • 95

2 Answers2

3

Using c++filt to demangle the name gives

std::string::operator=(std::string const&)

the copy-assignment operator for std::string.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • sorry i am very new to using gcov , how do i use c++filt to demangle the names given – Computernerd Jan 20 '14 at 03:47
  • @Computernerd: `c++filt _ZNSsaSERKSs` to demangle one symbol; or `gcov -f cashier.gnco | c++filt` to demangle the output of `gcov`; or `man c++filt` to learn all about it. – Mike Seymour Jan 20 '14 at 03:49
  • I have done as instructed and i get the following output: Function 'std::basic_string, std::allocator >::operator=(std::basic_string, std::allocator > const&)' – Computernerd Jan 20 '14 at 03:52
  • @Computernerd: That's right. `std::basic_string, std::allocator >` is the full name of `std::string`. – Mike Seymour Jan 20 '14 at 03:54
  • Why does the full name of std::string appear as a function in my report and why is 0 percent of the lines executed – Computernerd Jan 20 '14 at 03:56
  • @Computernerd: The operator appears in the report because it's called from `setID` and `setPassword`. I've no idea why it reports 0%. – Mike Seymour Jan 20 '14 at 03:58
  • Perhaps a kind of copy-elision?? cf. http://stackoverflow.com/questions/13624819/gcov-reporting-that-return-statement-is-not-hit – nodakai Jan 20 '14 at 04:21
  • @MikeSeymour the answer explains it ?? or it is a kind of copy elision?? @ nodakai What does copy-elision mean ?? – Computernerd Jan 20 '14 at 14:53
  • @Computernerd: Copy elision is an optimisation that sometimes allows unnecessary object copies to be avoided. It's got nothing to do with this situation. – Mike Seymour Jan 20 '14 at 15:09
1

As described by others, you need to use c++filt to translate the names from 'mangled' form into human-readable. c++filt takes standard input, detects mangled C++ strings and writes to output with the names corrected; so, e.g. gcov -f filename | c++filt

The source of your problem is taking std::string by value in a header file and putting the function body into an implementation, this prevents the compiler from doing clever optimizations and forces it to construct temporary std::strings.

void setID(string);
void setPassword(string);

These functions are trivial enough that you ought to put their implementations in the header file so that the compiler can inline them if need be. But ultimately, what you need to do is accept references.

void setID(const std::string& id)
{
    this->m_id = id;
}

void setPassword(const std::string& password)
{
    m_password = password;
}

(The 'm_' prefix for member variables is one of the more widely used methods for distinguishing member names from variable names).

This saves the compiler the need to create an intermediate temporary and copy the text from the source string. If your performance is still an issue, you may want to look at C++11s rvalues:

void setPassword(std::string&& password);
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • what do you mean by taking std::string by value in header file and putting the usage into an implementation , which part of the code does that?? – Computernerd Jan 20 '14 at 11:29
  • In the header file, you have "void setID(string);", that takes a std::string by value, by "usage" - a poor choice of words, edited - I meant the use of that copied value. What you've done forces the compiler to always construct a temporary, copy of the source std::string, which means allocating new memory for the text and memcpying the source string into it. – kfsone Jan 20 '14 at 21:39