Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Core dumped when running a program

317 views
Skip to first unread message

Yijun Zou

unread,
Jul 11, 2004, 6:52:24 AM7/11/04
to
Hi all,

I wrote a short program and gcc it in QNX6.21. However when I attempted to
execut the program, following message appears but the program doesn't run at
all.

Memory fault (core dumped)

What's the problem?

Yijun

The program list is:
/**************** alignment.c *****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct
{
unsigned short head;
unsigned short frame_type;
unsigned short command;
unsigned short data[39];
unsigned short crc;
} packet;

int main (int argc, char *argv[])
{


unsigned short counter;
int i;
packet *packet86;

/* packet86 initialization */
packet86->head = 0x55ff;
packet86->frame_type = 0x00b7;
packet86->command = 0xbb00;

for (i = 0; i<39; ++i)
packet86->data[i] = 0;

packet86->crc = 0;

printf("\nsize of struct is %d\n",sizeof(packet86));

for (counter = 0; counter < 101; ++counter)
{
packet86->frame_type = (((packet86->frame_type >> 8) | counter) << 8) |
0x00b7;

printf("\npacket86->frame_type = %4x\n",packet86->frame_type);

sleep (2);
}
return (0);
}
/*************************************/

I used #gcc alignment.c -o alignment to compile it.

Robert Rutherford

unread,
Jul 11, 2004, 8:52:22 AM7/11/04
to
On Sun, 11 Jul 2004 17:49:31 +0800, Yijun Zou wrote:

> Hi all,
>
> I wrote a short program and gcc it in QNX6.21. However when I attempted to
> execut the program, following message appears but the program doesn't run at
> all.
>
> Memory fault (core dumped)
>
> What's the problem?

This message means your program tried to access an invalid memory address -
one outside the allocated range(s) of memory for your program. When this
happens the operating system catches the error and raises a special signal
(SIGSEGV) on your program, which (unless you take other action) causes it
to abort with the error message above.

The actual problem is your attempt to use the pointer variable packet86
without first initialising it to point to valid memory.

If you don't understand this (or how to fix it) I suggest you get yourself
a good textbook on C programming in a Unix environment.

Hope this helps,

Rob Rutherford
Ruzz Technology

Michael

unread,
Jul 12, 2004, 2:34:04 AM7/12/04
to
Hi,
as Robert pointed out, you try to use memory,
which is not owned by your programm:

>
> The program list is:
[...]


>
> unsigned short counter;
> int i;
> packet *packet86;
>
> /* packet86 initialization */
> packet86->head = 0x55ff;
> packet86->frame_type = 0x00b7;
> packet86->command = 0xbb00;

I guess thats the error: packet86 is just a pointer,
no memory is allocated. Try:
packet packet_b[N];
packet *packet86 = &(packet[0]);
where N is the size of your array or add s.th. like
packet86 = malloc ( N * sizeof (*packet86));
to get memory at runtime.
regards
Michael

Sascha Morgenstern

unread,
Jul 12, 2004, 3:52:28 AM7/12/04
to

"Yijun Zou" <yj...@sina.com> schrieb im Newsbeitrag
news:ccr06n$c45$1...@inn.qnx.com...

> Hi all,
>
> I wrote a short program and gcc it in QNX6.21. However when I attempted to
> execut the program, following message appears but the program doesn't run
at
> all.
>
> Memory fault (core dumped)
>
> What's the problem?
>
> Yijun
>
> The program list is:
> /**************** alignment.c *****************************/
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> typedef struct
> {
> unsigned short head;
> unsigned short frame_type;
> unsigned short command;
> unsigned short data[39];
> unsigned short crc;
> } packet;
>
> int main (int argc, char *argv[])
> {
>
>
> unsigned short counter;
> int i;
> packet *packet86;


Maybe, just put in
packet86 = malloc( sizeof( packet ));

Yijun Zou

unread,
Jul 12, 2004, 7:52:35 AM7/12/04
to
Thanks Robert and Sascha!

"Sascha Morgenstern" <Sascha.Mo...@bitctrl.de> 写入消息新闻
:cct965$1bg$1...@inn.qnx.com...

Marc Haas

unread,
Jul 12, 2004, 8:52:32 AM7/12/04
to
Hi,
just two things to do...

Yijun Zou schrieb:
[...]


> The program list is:
> /**************** alignment.c *****************************/
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> typedef struct
> {
> unsigned short head;
> unsigned short frame_type;
> unsigned short command;
> unsigned short data[39];
> unsigned short crc;
> } packet;
>
> int main (int argc, char *argv[])
> {
>
>
> unsigned short counter;
> int i;
> packet *packet86;

packet86 = (packet*)malloc(sizeof(packet));

>
> /* packet86 initialization */
> packet86->head = 0x55ff;
> packet86->frame_type = 0x00b7;
> packet86->command = 0xbb00;
>
> for (i = 0; i<39; ++i)
> packet86->data[i] = 0;
>
> packet86->crc = 0;
>
> printf("\nsize of struct is %d\n",sizeof(packet86));

ATTENTION!!
This is not the size of the struct, this is the size of the pointer!

use:
printf("\nsize of struct is %d\n",sizeof(*packet86));
||
printf("\nsize of struct is %d\n",sizeof(packet));

>
> for (counter = 0; counter < 101; ++counter)
> {
> packet86->frame_type = (((packet86->frame_type >> 8) | counter) << 8) |
> 0x00b7;
>
> printf("\npacket86->frame_type = %4x\n",packet86->frame_type);
>
> sleep (2);
> }
> return (0);
> }
> /*************************************/
>
> I used #gcc alignment.c -o alignment to compile it.
>

Regards,
Marc

0 new messages