Visit Our UNIX and Linux User Community


Structure element accessing issue...


 
Thread Tools Search this Thread
Top Forums Programming Structure element accessing issue...
# 1  
Old 04-17-2008
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 07:32 AM..
# 2  
Old 04-17-2008
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.
# 3  
Old 04-18-2008
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.
# 4  
Old 04-18-2008
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.
# 5  
Old 04-18-2008
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 02:41 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #980
Difficulty: Easy
Releases of Mac OS X from 1999 to 2005 ran on the Intel-based Macs of that period.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Solaris OS having issue accessing LUN from Qlogic swich

Hi Community, I am facing one issue related storage accessing from solaris 10 machine(M5000). it was working fine then we shifted to another site. the only change on that site is FC switch. The issue i am facing is from solaris machine i can able to see the LUN, it;s only a test lun of 30... (17 Replies)
Discussion started by: bentech4u
17 Replies

2. Shell Programming and Scripting

Issue with accessing value inside while loop, outside it

Hi, GetName() { if then echo " Please enter the name: " read Name tempvar=0 while read line do if then tempvar=`expr $tempvar + 1` echo $tempvar ... (10 Replies)
Discussion started by: rituparna_gupta
10 Replies

3. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

4. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

5. Shell Programming and Scripting

Issue faced while accessing data from files on different servers.

I have two log files of same name and structure which resides on different servers with ssh-keygen installed. I want to search for a list of strings available in an array on last 15 minutes logs of both files and calculate the total count of occurence of each string. Sample format of both file... (0 Replies)
Discussion started by: poweroflinux
0 Replies

6. Web Development

Accessing a Perl CGI script, security issue

Hi Everybody, I was wondering if it was possible for someone to gain access to my Perl CGI scripts before they are interpreted by Perl (mod_perl on apache2) i.e. getting a hold of my raw scripts and not the html output? Let's say I use the DBI module where I have the hostname, user and... (2 Replies)
Discussion started by: z1dane
2 Replies

7. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

8. UNIX for Advanced & Expert Users

concurrency issue while Accessing Mail Box from shell script

Hi Bros, I am in a serious trouble with concurrency issue while using mailx. I have a shell script which reads all the emails of a unix user account and create a copy (another mbox for processing and archive purpose). This script works fine for 99.99% of the time but sometime it start creating... (2 Replies)
Discussion started by: Sumit_Fundoo
2 Replies

9. Shell Programming and Scripting

accessing my first element of array

Hello everyonel, I have an array set like so num=4 read name arr=name I go through while loop to assign different values to different array element from 1 to 4. when I try to access the FIRST element of the array I get the last one first. Like if I say ${arr} it will show the last element... (4 Replies)
Discussion started by: afadaghi
4 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

Featured Tech Videos