'core dumped ' error 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

Memory fault (core dumped)

What’s the problem?

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) |

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

sleep (2);
return (0);

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

Meanwhile, when I use
#gdb alignment core
to debug it, the gdb info also reminds me “/home/core, no such file or directory”
What’s the reason?


In the line:

packet *packet86;

You are creating a pointer. Then in the following:
/* packet86 initialization */
packet86->head = 0x55ff;
packet86->frame_type = 0x00b7;
packet86->command = 0xbb00;

you are dereferencing a pointer you never allocated. Sure looks
suspicious to me.


thanks, I added the following line to allocate mem for the struct first and the problem fixed.

packet86 = malloc( sizeof( packet ));

As far as I know you also should change this

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

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

if you intend to set field 0 - 39 to 0 and not 1 - 40 :slight_smile:

Err, forgot what i did say.

I had this problem quite some years ago.

But i tested it right when i got on the QNX-Machine and i saw it did work.

So no matter of ++i or i++ in the for-loop-header …


The usage of pointer often confuses me so much.
In this example, is it OK not to free the pointer?
Or if in my programs, pointers are initialized but never freed, will my programs work well?

A pointers just points to something (that’s why they are called pointer). If what the pointer points that you may need to free.

char array[100];
char *arraypt;

arraypt = array;

Here b doesn’t need to be freed because the object or memory it points to is the variable array.

However for:

char *arraypt;

arraypt = malloc( 100 );

The object pointed to need to be freed because it was obtain with malloc.

It’s important to remember that it’s not the pointer that is freed (or not) it what the pointer points to.

Hi johnsonyoung,

No, it is not OK to not free pointers. Depending on what you do in your program, you may be just fine without freeing, but you will be wasting/leaking
memory … and eventually you will run out of memory and probably your program will crash if you don’t check the value returned by malloc …

It’s good practice to always free allocated memory once you don’t need it anymore.


thx a lot!
In fact, when I use any types of pointer, I have to initialize them like this:
p=malloc(numbersizeof(type)); and free them as free(p);
If I try to initialize a string like this:
char s="";//Isn’t s initialized? I’m always confused by this usage
and then try to assign it:
sprintf(s,“this is a test”);
The program will be compiled right but run to memory fault?
if I change the code to:
char s[100]; or
then the program works well.

my understanding is
char *s=“something”;
will initialize the s with the length of “something”. this means you CAN"T do
sprintf(s,“somethingelse”) where “somethingelse” is longer than “something”.


char *s1 = “something”;
char *s2 = “something”;
sprintf(s1, “anything”);

the change of s1 will also change s2, because compiler will consider “something” as an unnamed global constant character array variable
it is not good idea to change global constants like this