Operating System - Linux
1753787 Members
7543 Online
108799 Solutions
New Discussion юеВ

Re: Segmentation fault while C++ string concatenation

 
Sanjay Sutar
Frequent Advisor

Segmentation fault while c++ string concatenation...

I have a very small program to simulate my actual issue in a large program.

I build it on one HPUX box and test it on another, it gives me core dump whenever I do string operation or even cout gives me core dump.


I built and linked that using gcc 3.4.2

as

g++ -v -g -pthread -c test.cpp

g++ -v -o TestStaticString test.o -pthread -L /var/home/HPUXAGENT/lib -L/usr/local/lib -lpthread -lcrypt -lxnet -lnsl -lsec -lstdc++ -Xlinker +b -Xlinker ../lib:. -Xlinker +s -z



I have two HPUX machine;
Box1 : build machine
$ uname -a
HP-UX unknown B.11.11 U 9000/800 1925780423 unlimited-user license

Box2: Test machine
bash-2.04# uname -a
HP-UX kelp B.11.11 U 9000/785 2001873660 unlimited-user license

Attached file contains -
Sample Program
Sample program output
Core file analysis
program debugging with gdb
swlist on build machine
swlist on test machine
Building and linking
Build machine
Test machine

I have posted this on general forum and got much help , but I am posting this again here , beacause its more related to c++ language.

One last point , the same sample program works fine on a third HPUX box. The detail are given in the attached file

Any help regarding this is much appreciated.
Thanks in advance!


3 REPLIES 3
leonardo_20
Valued Contributor

Re: Segmentation fault while c++ string concatenation...

Try to modify method name as is

string getfullname()
{ return (name + surname); }
leonardo_20
Valued Contributor

Re: Segmentation fault while c++ string concatenation...

Also... I suggest you to use this declaration:

static1 s = new static1();

and call the methods as is

string fname = s.getfullname();

regards
Leonardo
Dennis Handly
Acclaimed Contributor

Re: Segmentation fault while C++ string concatenation

>Leonardo: Try to modify method name as is: { return (name + surname); }

Adding () won't help. The problem is that static1::name isn't initialized.

>I suggest you to use this declaration:
>static1 s = new static1();
>and call the methods as is
>string fname = s.getfullname();

s needs to be a reference or object for ".".

Also, this doesn't solve the problem with "name".