The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-2008
Registered User
 

Join Date: Feb 2008
Location: Mumbai
Posts: 11
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-17-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,464
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*/
}
foo() has to call malloc to create memory for both a struct Idev and the string q --- a_var.idev_ptr->q. Look for the problem to originate in foo.
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.
Reply With Quote
  #3 (permalink)  
Old 04-18-2008
Registered User
 

Join Date: Feb 2008
Location: Mumbai
Posts: 11
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.
Reply With Quote
  #4 (permalink)  
Old 04-18-2008
Registered User
 

Join Date: Oct 2003
Posts: 69
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.
Reply With Quote
  #5 (permalink)  
Old 04-18-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 223
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0