Core dumped when running a program

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 = 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 >> :sunglasses: | counter) << :sunglasses: |
0x00b7;

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

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

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

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

“Yijun Zou” <yjzou@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 ));


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

for (i = 0; i<39; ++i)
packet86->data > = 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 >> > :sunglasses: > | counter) << > :sunglasses: > |
0x00b7;

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

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

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


\

Thanks Robert and Sascha!

“Sascha Morgenstern” <Sascha.Morgenstern@bitctrl.de> дÈëÏûÏ¢ÐÂÎÅ
:cct965$1bg$1@inn.qnx.com

“Yijun Zou” <> yjzou@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 ));



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

for (i = 0; i<39; ++i)
packet86->data > = 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 >> > :sunglasses: > | counter) << > :sunglasses:
|
0x00b7;

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

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

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




\

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 > = 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 >> > :sunglasses: > | counter) << > :sunglasses: > |
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