Why is this failing?

B

Billy Patton

If I comment out all down to the first fprintf , 6 lines. no problem
I uncommented one at a time until just the std::string* sp3 was commented out
and still everything went ok.
When I uncomment that line I get a seg fault.

Here's the core bt:
Core was generated by `Linux/32/test'.
Program terminated with signal 11, Segmentation fault.
#0 0x0811bb55 in _int_malloc ()
(gdb) bt
#0 0x0811bb55 in _int_malloc ()
#1 0x0811b58a in malloc ()
#2 0x080efd9e in operator new (sz=137509408)
at /apps/local-source/gcc/gcc-3.4.1/libstdc++-v3/libsupc++/new_op.cc:48
#3 0x080662e2 in t_String () at test.cxx:1971
#4 0x0804836d in main (argc=1, argv=0xbfff84f4) at test.cxx:87
(gdb) q


Notice #2 sz=137509408

/********************************************************************
************************* TEST OPERATOR != *************************
*******************************************************************/
{
String s1("ABC");
String s2("abc");
String* sp1 = new String("abc");
String* sp2 = new String("ABC");
std::string s3("abc");
std::string* sp3 = new std::string("abc");
fprintf(stderr,"%d\n",__LINE__);
std::string* sp4 = new std::string("ABC");
fprintf(stderr,"%d\n",__LINE__);
OK(s1 != s1);
NOK(s1 != s2);
NOK(s1 != sp1);
OK(s1 != sp2);
NOK(s1 != s3);
NOK(s1 != sp3);
OK(s1 != sp4);
delete sp1;
delete sp2;
delete sp3;
delete sp3;
s2 = 1;
NOK(s1 != (short)1);
OK(s2 != (short)1);
NOK(s1 != (long)1);
OK(s2 != (long)1);
NOK(s1 != (int)1);
OK(s2 != (int)1);

}
LEAVE;
}

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
R

REH

Billy Patton said:
If I comment out all down to the first fprintf , 6 lines. no problem
I uncommented one at a time until just the std::string* sp3 was commented out
and still everything went ok.
When I uncomment that line I get a seg fault.

Here's the core bt:
Core was generated by `Linux/32/test'.
Program terminated with signal 11, Segmentation fault.
#0 0x0811bb55 in _int_malloc ()
(gdb) bt
#0 0x0811bb55 in _int_malloc ()
#1 0x0811b58a in malloc ()
#2 0x080efd9e in operator new (sz=137509408)
at /apps/local-source/gcc/gcc-3.4.1/libstdc++-v3/libsupc++/new_op.cc:48
#3 0x080662e2 in t_String () at test.cxx:1971
#4 0x0804836d in main (argc=1, argv=0xbfff84f4) at test.cxx:87
(gdb) q


Notice #2 sz=137509408

/********************************************************************
************************* TEST OPERATOR != *************************
*******************************************************************/
{
String s1("ABC");
String s2("abc");
String* sp1 = new String("abc");
String* sp2 = new String("ABC");
std::string s3("abc");
std::string* sp3 = new std::string("abc");
fprintf(stderr,"%d\n",__LINE__);
std::string* sp4 = new std::string("ABC");
fprintf(stderr,"%d\n",__LINE__);
OK(s1 != s1);
NOK(s1 != s2);
NOK(s1 != sp1);
OK(s1 != sp2);
NOK(s1 != s3);
NOK(s1 != sp3);
OK(s1 != sp4);
delete sp1;
delete sp2;
delete sp3;
delete sp3;
s2 = 1;
NOK(s1 != (short)1);
OK(s2 != (short)1);
NOK(s1 != (long)1);
OK(s2 != (long)1);
NOK(s1 != (int)1);
OK(s2 != (int)1);

}
LEAVE;
}
You are deleting sp3 twice.

Your macros make reading your code confusing.

REH
 
V

Victor Bazarov

Billy said:
If I comment out all down to the first fprintf , 6 lines. no problem
I uncommented one at a time until just the std::string* sp3 was
commented out and still everything went ok.
When I uncomment that line I get a seg fault.

Here's the core bt:
Core was generated by `Linux/32/test'.
Program terminated with signal 11, Segmentation fault.
#0 0x0811bb55 in _int_malloc ()
(gdb) bt
#0 0x0811bb55 in _int_malloc ()
#1 0x0811b58a in malloc ()
#2 0x080efd9e in operator new (sz=137509408)
at /apps/local-source/gcc/gcc-3.4.1/libstdc++-v3/libsupc++/new_op.cc:48
#3 0x080662e2 in t_String () at test.cxx:1971
#4 0x0804836d in main (argc=1, argv=0xbfff84f4) at test.cxx:87
(gdb) q


Notice #2 sz=137509408

/********************************************************************
************************* TEST OPERATOR != *************************
*******************************************************************/
{
String s1("ABC");
String s2("abc");
String* sp1 = new String("abc");
String* sp2 = new String("ABC");
std::string s3("abc");
std::string* sp3 = new std::string("abc");
fprintf(stderr,"%d\n",__LINE__);
std::string* sp4 = new std::string("ABC");
fprintf(stderr,"%d\n",__LINE__);
OK(s1 != s1);
NOK(s1 != s2);
NOK(s1 != sp1);
OK(s1 != sp2);
NOK(s1 != s3);
NOK(s1 != sp3);
OK(s1 != sp4);
delete sp1;
delete sp2;
delete sp3;
delete sp3;

Why are you deleting 'sp3' twice? Copy-paste error?
 
B

Billy Patton

You are deleting sp3 twice.

Your macros make reading your code confusing.

REH

It makes no difference about deleting sp3 twice. It doesn't get that far.
It will not even get to the second fprintf.

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
L

Lionel B

Billy Patton said:
If I comment out all down to the first fprintf , 6 lines. no problem
I uncommented one at a time until just the std::string* sp3 was commented out
and still everything went ok.
When I uncomment that line I get a seg fault.

[...]

String s1("ABC");

What is "String" ? Perhaps the problem is there (just guessing, as we can't see the code for it).
 
T

Thomas Matthews

Billy said:
If I comment out all down to the first fprintf , 6 lines. no problem
I uncommented one at a time until just the std::string* sp3 was
commented out and still everything went ok.
When I uncomment that line I get a seg fault.

Here's the core bt:
Core was generated by `Linux/32/test'.
Program terminated with signal 11, Segmentation fault.
#0 0x0811bb55 in _int_malloc ()
(gdb) bt
#0 0x0811bb55 in _int_malloc ()
#1 0x0811b58a in malloc ()
#2 0x080efd9e in operator new (sz=137509408)
at /apps/local-source/gcc/gcc-3.4.1/libstdc++-v3/libsupc++/new_op.cc:48
#3 0x080662e2 in t_String () at test.cxx:1971
#4 0x0804836d in main (argc=1, argv=0xbfff84f4) at test.cxx:87
(gdb) q


Notice #2 sz=137509408

/********************************************************************
************************* TEST OPERATOR != *************************
*******************************************************************/
{
String s1("ABC");
String s2("abc");
String* sp1 = new String("abc");
String* sp2 = new String("ABC");
std::string s3("abc");
Try this:
std::string * sp3 = new std::string;
*sp3 = "abc";
std::string * sp4 = new std::string;
*sp4 = "ABC";
std::string* sp3 = new std::string("abc");
fprintf(stderr,"%d\n",__LINE__);
std::string* sp4 = new std::string("ABC");
fprintf(stderr,"%d\n",__LINE__);
OK(s1 != s1);

By the way, does your String class define the
!= operator for std::string?

Why do you have sp1 as a pointer to a dynamically
allocated string (and same for the others)?
Did you want the following:
String * sp1 = &s1;

Generally, what you are doing is testing the
dynamically allocated memory (via new) versus
the local memory.

When testing equivalence operators, the location
of the objects is irrevalent. You are testing
the comparison of the value of one String versus
another. I don't see the point of using the
dynamic memory allocation.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top