![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Search attributes in one structure using the values from another structure | dhanamurthy | High Level Programming | 3 | 03-26-2008 11:37 PM |
| concurrency issue while Accessing Mail Box from shell script | Sumit_Fundoo | UNIX for Advanced & Expert Users | 2 | 02-25-2007 10:23 AM |
| MV files from one directory structure(multiple level) to other directory structure | srmadab | UNIX for Advanced & Expert Users | 4 | 09-13-2006 01:01 PM |
| accessing my first element of array | afadaghi | Shell Programming and Scripting | 4 | 09-29-2005 01:43 PM |
| Copying a Directory Structure to a new structure | jhansrod | UNIX for Dummies Questions & Answers | 8 | 07-27-2005 03:24 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Structure element accessing issue...
Hi all,
Can someone advice, why I'm getting a segmentation fault on a Linux system for a program which looks like the below one--: typedef struct idev{ int p, char q[10]; } Idev; typedef struct abc{ int i; Idev *idev_ptr; } ABC; int main(){ ABC a_var; char str[10]; int i_var = 0; /* I received 'a_var' populated by some function call as follows---*/ int result = foo(&a_var); /*Received result == SUCESS*/ /*initializing str[] to all zero.*/ bzero(str, sizeof(str)); if(a_var.idev_ptr != NULL) { /*Access to this is pretty fine*/ i_var = a_var.idev_ptr->p; /*But accessing array element gives the segmentation fault*/ strncpy(str, a_var.idev_ptr->q, 10); } if(a_var.id_q != NULL) { free(a_var.i_d); /*freed the memory*/ } return 0; /*end of the program*/ } Now please advice me, why I get the segmentation fault by just accessing a_var.idev_ptr->q ??? Last edited by Praveen_218; 04-17-2008 at 03:32 AM. |
| Forum Sponsor | ||
|
|
|
|||
|
Code:
typedef struct idev
{
int p,
char q[10];
} Idev;
typedef struct abc
{
int i;
Idev *idev_ptr;
} ABC;
int main()
{
ABC a_var={0,NULL};
char str[10]={0x0};
int i_var = 0;
/* I received 'a_var' populated by some function call as follows---*/
int result = foo(&a_var);
/*Received result == SUCESS*/
/*initializing str[] to all zero.*/
bzero(str, sizeof(str));
if(a_var.idev_ptr != NULL)
{
/*Access to this is pretty fine*/
i_var = a_var.idev_ptr->p;
/*But accessing array element gives the segmentation fault*/
strncpy(str, a_var.idev_ptr->q, 10);
}
if(a_var.id_q != NULL)
{
free(a_var.i_d); /*freed the memory*/
}
return 0; /*end of the program*/
}
Segmentation faults are caused by trying to reference a memory address that is not allocated to your process.... I put your code inside code tags, otherwise it is very hard to read. |
|
|||
|
Thanks Jim for the reply ....
I got the Seg-11 even after conditional checking for NULL before actually accessing the struct object --:
if(a_var.idev_ptr != NULL) { /*Access to this is pretty fine*/ i_var = a_var.idev_ptr->p; /*But accessing array element gives the segmentation fault*/ strncpy(str, a_var.idev_ptr->q, 10); } This is perticularly strange to me. The actual code for which foo() is a dummy is written in a .so file and the library is supposed to provide me the *idev_ptr populated via some malloc() and user is supposed to free(idev_ptr). Its starange. |
|
|||
|
Have you insured that the definitions for the structure are identical between the library where foo resides and whoever is calling it? You may try recompiling both with the same header if both sources are under your control just to be sure.
|
|
|||
|
Make sure that a_var.idev_ptr->q != NULL.
<edit: nevermind on the sizeof stuff regarding str -- statically alloc'd array -- sizeof works fine.> Also your free has a syntax error with variable name. Last edited by ramen_noodle; 04-18-2008 at 10:41 AM. |
|||
| Google UNIX.COM |