This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: c++/9872: temporary destructor not called?


Aha, I've forgatten the attachments, here they come.

(Not tgz as the are small though as I can see it looks ugly on the bug report 
site. Not sure about the std. ways so sending anuway)

Sandor
/**
 * Compile:
 *  - to eat up memory:
 *   g++ -c bug.cpp
 *   g++ bug.o -o bug
 *  - work properly:
 *   g++ -fno-elide-constructors -c bug.cpp
 *   g++ bug.o -o bug
 */
 
#define ALLOC(N) (char*) new char[sizeof(char)*(N)]
#define DELETE(P) delete[] ((char*)(P))

class String {
public:
    String() {
        data = 0;
    }
    String(const char *str) {
        data = ALLOC(strlen(str) + 1);
        strcpy(data, str);
    }
    String(const String &str){
        data = 0;
        operator=(str.data);
    }
    ~String() {
        if (data) DELETE(data);
    }
    String &operator=(const String &str) {
        return operator=(str.data);
    }
    String &operator=(const char *str) {
        if (data) DELETE(data);
        if (str) {
            data = ALLOC(strlen(str) + 1);
            strcpy(data, str);
        } else data = 0;
        return *this;
    }
    String &operator+=(const String &str);
    String &operator+=(const char *str);
    int length() {
        return strlen(data);
    }
    static int strlen(const char *str);
    static void strcpy(char *dest, const char *src);
protected:
    char *data;
};

String &String::operator+=(const String &str) {
    return operator+=(str.data);
}

String &String::operator+=(const char *str) {    
    int len1 = length(), len2 = strlen(str);
    if (len2) {        
        char *newdata = ALLOC(len1 + len2 + 1);
        if (data) strcpy(newdata, data);
        strcpy(newdata + len1, str);
        if (data) DELETE(data);
        data = newdata;        
    } 
    return *this;
}
    
inline const String operator+(const String &s1, const char *s2) {
    String tmp(s1);
    tmp += s2;
    return tmp;
}

inline const String operator+(const char *s1, const String &s2) {
    String tmp(s1);
    tmp += s2;
    return tmp;
}

int String::strlen(const char *str) {
    if (!str) return 0;
    const char *org = str;
    while (*str) str++;
    return str - org;
}

void String::strcpy(char *dest, const char *src) {
    while ((*dest++ = *src++));
}

void tester(String str) {
}

int main(int argc, char** argv) {
    String str("bug");
    while (true) tester("ops: " + str);
}
# 1 "bug.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "bug.cpp"
# 14 "bug.cpp"
class String {
public:
    String() {
        data = 0;
    }
    String(const char *str) {
        data = (char*) new char[sizeof(char)*(strlen(str) + 1)];
        strcpy(data, str);
    }
    String(const String &str){
        data = 0;
        operator=(str.data);
    }
    ~String() {
        if (data) delete[] ((char*)(data));
    }
    String &operator=(const String &str) {
        return operator=(str.data);
    }
    String &operator=(const char *str) {
        if (data) delete[] ((char*)(data));
        if (str) {
            data = (char*) new char[sizeof(char)*(strlen(str) + 1)];
            strcpy(data, str);
        } else data = 0;
        return *this;
    }
    String &operator+=(const String &str);
    String &operator+=(const char *str);
    int length() {
        return strlen(data);
    }
    static int strlen(const char *str);
    static void strcpy(char *dest, const char *src);
protected:
    char *data;
};

String &String::operator+=(const String &str) {
    return operator+=(str.data);
}

String &String::operator+=(const char *str) {
    int len1 = length(), len2 = strlen(str);
    if (len2) {
        char *newdata = (char*) new char[sizeof(char)*(len1 + len2 + 1)];
        if (data) strcpy(newdata, data);
        strcpy(newdata + len1, str);
        if (data) delete[] ((char*)(data));
        data = newdata;
    }
    return *this;
}

inline const String operator+(const String &s1, const char *s2) {
    String tmp(s1);
    tmp += s2;
    return tmp;
}

inline const String operator+(const char *s1, const String &s2) {
    String tmp(s1);
    tmp += s2;
    return tmp;
}

int String::strlen(const char *str) {
    if (!str) return 0;
    const char *org = str;
    while (*str) str++;
    return str - org;
}

void String::strcpy(char *dest, const char *src) {
    while ((*dest++ = *src++));
}

void tester(String str) {
}

int main(int argc, char** argv) {
    String str("bug");
    while (true) tester("ops: " + str);
}

Attachment: bug.with-felide.s
Description: Binary data

Attachment: bug.without-felide.s
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]