- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- very simple code(5 lines only), why coredump?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 02:17 AM
12-27-2004 02:17 AM
it's ok under WINDOWS & LINUX, but coredump under HP-UNIX, why?
#include
#include
int main(int argc, char *argv[])
{
char s1[5],s2[8];
strcpy(s1,"WKF"); //if remove this line, no coredump
int *p=(int*)s2;
p[0]=1234; //this line cause coredump
printf("%d\n",p[0]);
}
HP-UX hpltest B.11.11 U 9000/800 (tc)
link with "aCC -g -o t t.cpp". why?
and how to avoid coredump without modify my code? thanks very much.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 09:57 AM
12-27-2004 09:57 AM
Re: very simple code(5 lines only), why coredump?
Try this:
cp tt.cpp tt.c
cc -g tt.c
When I run ./a.out, I get
1234
As a guess I'd say s1 is allocated 5 bytes on the stack, s2 is right behind it with 8 bytes. I'd be really surprised if the compiler didn't align s2 properly, but you never know. s2 may be byte aligned and an int is bigger, so the dereference (p[0]) may cause a bus error. (s2 needs to be on a word boundary.)
The command line option -o tt.cpp is telling aCC you want to create an object file, not an executable. Have you tried aCC -AA -g tt.cpp and running ./a.out ?
I see nothing wrong with the strcpy command, but you should always check the return value. Also, this is a C program, why are you using the C++ compiler?
Regards,
Dave
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 12:04 PM
12-27-2004 12:04 PM
Re: very simple code(5 lines only), why coredump?
> #include
> #include
> int main(int argc, char *argv[])
> {
> char s1[5],s2[8];
> strcpy(s1,"WKF"); //if remove this line, no coredump
> int *p=(int*)s2;
> p[0]=1234; //this line cause coredump
> printf("%d\n",p[0]);
> }
It dumps core because of incorrect alignment.
If you change the size of char s1[5] to s1[8],
the core dump will go away.
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 12:11 PM
12-27-2004 12:11 PM
Re: very simple code(5 lines only), why coredump?
Couple of more comments.
You are writing a C code with some C++
syntax and compiling in a C++ compiler.
You probably know that this is not really a
good idea in real life.
Another thing is using char s2[] to store
integer. That's not a good idea either.
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 02:34 PM
12-27-2004 02:34 PM
Re: very simple code(5 lines only), why coredump?
I wrote it in C++ because this code is just a demo of one of my another complex C++ program, which meet the same coredump. so do not compile it with "cc".
I store a number in a char[] is to store number in a big continuous memory in binary,in "sizeof(int)" bytes, the continuous memory also store other data.
I have tried to change the s1[] and s2[] size, but still coredump!
#include
#include
int main(int argc, char *argv[])
{
char s1[77],s2[88];
strcpy(s1,"WKF"); //if remove this line, no coredump
int *p=(int*)s2;
p[0]=1234; //this line cause coredump
printf("%d\n",p[0]);
}
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 03:43 PM
12-27-2004 03:43 PM
Solution> I have tried to change the s1[] and s2[] size, but still coredump!
I don't think you understood my reply; so let me be
more explicit. Make sure that the size of the array
s1[] is 8 byte alligned (i.e divisible by 8). In your new
code, you have used the s1[] size as 77. If you
change it to 80, then you will not encounter the bus
error.
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 05:53 PM
12-27-2004 05:53 PM
Re: very simple code(5 lines only), why coredump?
Biswajit Tripathy's reply is very helpful.
thanks. :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 06:33 PM
12-27-2004 06:33 PM
Re: very simple code(5 lines only), why coredump?
before you close the thread, I just want you
to understand the problem with this solution.
The solution is working solely because you are
assigning a suitable size to s1 to make sure that
address of s2 is 8 byte aligned. If someone
inserts another char array s3 between s1 and s2,
the program might again start to fail. To write a
program with better coding standard, you probably
need to use int arrays instead of char arrays or
check the compiler manual to see if the
c-preprocessor "pragma" directive can help you
align the data in a more predictable way.
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 06:33 PM
12-27-2004 06:33 PM
Re: very simple code(5 lines only), why coredump?
use strncpy instead to prevent possible security hacks of your code in the future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 07:23 PM
12-27-2004 07:23 PM
Re: very simple code(5 lines only), why coredump?
it's a good idea for me to use int arrays instead of char arrays in my program.
on the other hand, how to use "pragma" directive to align the data? would you please give me a simple example code?
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 07:38 PM
12-27-2004 07:38 PM
Re: very simple code(5 lines only), why coredump?
sorry, it's been a long time since I used pragma in
a user level code, so don't remember the exact
directives; but the following URL might help you:
http://docs.hp.com/en/B3901-90015/ch03.html
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2004 07:51 PM
12-27-2004 07:51 PM
Re: very simple code(5 lines only), why coredump?
Biswajit Tripathy's reply is always very helpful.
so I can close the thread now,
but maybe somebody else want to add comments,
so I'll close it later.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2005 11:29 PM
01-04-2005 11:29 PM