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.
> 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
>
> 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
Maybe, just put in
packet86 = malloc( sizeof( packet ));
"Sascha Morgenstern" <Sascha.Mo...@bitctrl.de> 写入消息新闻
:cct965$1bg$1...@inn.qnx.com...
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