unpredictable crash

S

sami.jan

Hi

I am using the xlC_r compiler on AIX 5.2 - this program crashes with a
segmentation fault (core dumped) - I compile with a command: xlC_r
filename.C - no switches or anything
------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>

char* myfunc()
{
char *temp;
temp = new char(100);
strcpy(temp, "Hello world man");
return temp;
}

int main()
{
cout << myfunc() << endl;
return 0;
}
---------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>

char* myfunc()
{
char *temp;
temp = new char(100);
strcpy(temp, "Hello world man");
return temp;
}

int main()
{
cout << myfunc() << endl;
return 0;
}
----------------------------------------------------------------------------------------
but this does'nt:

#include <iostream.h>
#include <string.h>

char* myfunc()
{
char *temp;
temp = new char(100);
cerr << "1" << endl;
strcpy(temp, "Hello world man");
return temp;
}

int main()
{
cout << myfunc() << endl;
return 0;
}
_----------------------_----------------------_----------------------_----------------------_----------------------

The only change is the

cerr << "1" << endl;

line

Any idea why this happens? And also, how do you read a core dump? Any
tools, information regarding this? I opened the core file in a hex
editor and there was too much information to make any sense

Thanks

Sami
 
M

mlimber

Hi

I am using the xlC_r compiler on AIX 5.2 - this program crashes with a
segmentation fault (core dumped) - I compile with a command: xlC_r
filename.C - no switches or anything
------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>

char* myfunc()
{
char *temp;
temp = new char(100);

You meant char[100] to create an array. The parentheses initializes
your single char to 100. Better yet, use std::string or perhaps
std::vector<char>. See this FAQ:

http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1

[snip]
Any idea why this happens? And also, how do you read a core dump? Any
tools, information regarding this? I opened the core file in a hex
editor and there was too much information to make any sense

Off topic. Ask in a group for your compiler or OS.

Cheers! --M
 
S

sami

btw, the program with the "cerr" line i.e.

har *temp;
temp = new char(100);
cerr << "1" << endl;

does not crash - I can see that I am not intitializing to a 100 char
length array but why does a cerr stop the program from crashing
 
M

mlimber

sami said:
btw, the program with the "cerr" line i.e.

har *temp;
temp = new char(100);
cerr << "1" << endl;

does not crash - I can see that I am not intitializing to a 100 char
length array but why does a cerr stop the program from crashing

First of all, please quote the message you are responding to. Not
everyone is using Google Groups, and it makes it easier for all to
follow the conversation. (To automatically quote in GG, click "show
options" and then "Reply" in the revealed header.)

By writing to memory that isn't allocated to you, you have invoked
dreaded undefined behavior (UB), which can be anything from erratic
program behavior to a crash to transubstantiating your lead into gold.
Adding the cerr line just made the program data and code different, and
so when you invoked the UB, it also did something different (for
instance, it might have overwritten the streambuffer underlying cerr
instead of writing to a protected area of memory). You could look at a
disassembly of the two programs and watch what happens to your code and
data after the UB if you really want to know.

Cheers! --M
 
C

cbmanica

#include <iostream.h>
#include <string.h>


char* myfunc()
{
char *temp;
temp = new char(100);
strcpy(temp, "Hello world man");
return temp;
}

You already know why this crashes. Do you also realize that myfunc()'s
caller is responsible for delete'ing temp?
 
S

sami

thanx mlimber, and sorry i did not quote - i am not a really news-savvy
guy

Nonstandard headers: they are <iostream> and <cstring> respectively.
Of course, your implementation may not be standard either...

Where can I get some detailed info regarding usage of standard vs
non-standard c++ headers?
You already know why this crashes. Do you also realize that myfunc()'s
caller is responsible for delete'ing temp?

yes, this is just a dummy/test program - the crash is really not
because the caller of myfunc() is responsible, mlimber wrote about it
how "By writing to memory that isn't allocated to you, you have invoked
dreaded undefined behavior (UB)"

thanx anyway guyz
 
R

red floyd

sami said:
thanx mlimber, and sorry i did not quote - i am not a really news-savvy
guy



Where can I get some detailed info regarding usage of standard vs
non-standard c++ headers?

The Standard -- ISO/IEC 14882:2003
 
R

red floyd

sami said:
thanx mlimber, and sorry i did not quote - i am not a really news-savvy
guy

I believe that technically, string.h *IS* standard. It's part of the
C89 library which is incorporated by reference into the Standard.
However, <cstring> is preferred.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top