Operating System - Linux
1753768 Members
5264 Online
108799 Solutions
New Discussion юеВ

compiler OK, But execute it shows Error : Segmentation fault (core dumped)

 
j773303
Super Advisor

compiler OK, But execute it shows Error : Segmentation fault (core dumped)

Hi C program expert,
I've C problem , the part of code as below:
Could anybody knows why execute it would get the Error : Segmentation fault (core dumped) ?
Thanks in advanced.

=====================================
typedef struct APC_PNDN_Lot
{
char LotId[15];
int ImpWaferCount;
char ImpWaferList[400];
char Area[10];
struct APC_PNDN_Lot* Next;
} APC_PNDN_LOT;

APC_PNDN_LOT *ptr;
ptr = (void *)(malloc (sizeof (APC_PNDN_LOT)));

ptr->ImpWaferList[0]=0x00;
ptr->LotId[0]=0x00;
ptr->Area[0]=0x00;
===========================================

Used GDB got the below:
Program received signal SIGSEGV, Segmentation fault
si_code: 1 - SEGV_MAPERR - Address not mapped to object.
0x40000000000048a0:0 in main () at APC_PNDN.c:527
527 ptr->ImpWaferList[0]=0x00;
Hero
4 REPLIES 4
Venkatesh BL
Honored Contributor

Re: compiler OK, But execute it shows Error : Segmentation fault (core dumped)

Dennis Handly
Acclaimed Contributor

Re: compiler OK, But execute it shows Error: Segmentation fault

>ptr = (void*)malloc(sizeof(APC_PNDN_LOT));

This code isn't legal. The extra cast implies that there isn't any prototype for malloc.
On A.06.15 in 64 bit mode this is now a compiler error.
Make sure you include <stdlib.h>.

Dennis Handly
Acclaimed Contributor

Re: compiler OK, But execute it shows Error: Segmentation fault

You can check this by printing ptr. It probably has the upper 32 bits truncated.

Note the proper cast is (APC_PNDN_LOT*). malloc already returns a void*.

Dennis Handly
Acclaimed Contributor

Re: compiler OK, But execute it shows Error: Segmentation fault

We haven't heard any feedback on our suggestions. Have you solved your problem?
If our suggestions were helpful, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33