I am declaring a structure as this

typedef struct Request
{
	deque<int> roomsList ;
}Request;

Then , in some other function I get a segmentation fault in the highlighted line :

int initRoomsList(Request *R)
{
	int roomSatisfying ;
	char roomName[20] ;
	int roomNum, capacity,wb,proj, lcd, sound, aud, vid , ac ;
	int count = 0;
	FILE *fp ;
	fp = fopen("rooms.txt","r") ;
	while(fscanf(fp,"%d %d %d %d %d %d %d %d %d %s", \
	&roomNum,&capacity, &wb,&proj,&lcd,&sound,&aud,&vid,&ac,roomName) != EOF )
	{
		if(R->capacity > capacity )
			continue ;
		if(R->wb > wb )
			continue ;
		if(R->proj > proj)
			continue ;
		if(R->lcd > lcd )
			continue ;
		if(R->sound > sound )
			continue ;
		if(R->aud > aud)
			continue ;
		if(R->vid > vid )
			continue ;
		if(R->ac > ac)
			continue ;
			
		// If control is here, that means this room fulfils all conditions .
		cout<<"hi "<<roomNum<<endl ;
		cout<<"hello "<<&(R->roomsList)<<endl ; 
		[B][I](R->roomsList).push_back(roomNum) ;[/I][/B]
		cout<<"bye "<<&(R->roomsList)<<endl ; 
		count++;
	}
	fclose(fp) ;
	if(count==0)
		return 0 ;	
	else
		return 1 ;
	
}

This is what gets printed on the terminal :

hi 0
hello 0x1849010
bye 0x1849010
hi 1
hello 0x1849010
Segmentation fault

Output of GDB :

hi 0
hello 0x60d010
bye 0x60d010
hi 1
hello 0x60d010

Program received signal SIGSEGV, Segmentation fault.
0x0000000000406f1c in __gnu_cxx::new_allocator<int>::construct(int*, int const&) ()
(gdb)

Please help me out with this!!

Build your code with debug symbols in. Run it again in gdb, and when the segFault happens, enter up until you're in one of your lines of code.

Enter print variableName for each variable in that line of code until you find the bad one. Almost certainly, one of your pointers is pointing at memory that isn't yours.

Since I don't see anything obvious wrong with your function itself, how are you allocating the Request* that you're passing into the function?

@Moschops : I am printing variable names just before and after the green highlighted statement. The statement before gets printed but the one after doesn't.When I run gdb , I get seg fault again at the same spot.

@raptr_dflo : This is how :

Request *R = (Request *)malloc(sizeof(Request)) ;

Then I initialize various fields of R and pass it on to initRoomsList

kodemaaster, don't print them as part of your code. That's no use. You need to get gdb to print them so you can see what values they have at the instant of crashing. gdb contains a print command that you can type in. For example,

print R->roomsList

Following Moschops input, the "where" command in GDB also shows you (briefly) the current function-call stack, which I've found useful if for no other reason than to get a quick idea of how many "up" commands I need to enter before I get back to my own code, and occasionally "Oops! How did i get into this function from THERE???"

If you can master where, up, down, and print you're in great shape. Then move on to "stop at", "stop in", "continue" and "step" (I believe, read the info or man page to get all the details straight...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.