Thread: I need to see a function return address.

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    I need to see a function return address.

    OK to cut a long story short I have a program which crashes when it returns from a function call.


    Hence I need to be able to examine the return address to see when it changes.

    Bit of a weird problem, this program used to work, I have made a few changes but not the sort that would cause this problem (I think).

    The function has been called several hundred times before and returned OK (with pretty much the same parameters passed to it).

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK I have decided to not pass any parameters to it and make them global, will see what happens!!!!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    One possibility is that the function is returning the address of a local variable - which can eventually cause the caller to crash when it dereferences (uses) the object at that address. It doesn't matter if your function has been called millions of times before - those previous calls might have done something that wasn't detected by the operating system.

    However, the more likely possibility is some other code is the cause. If some other code executed before your function is tromping memory used by your function, it will eventually cause your function to crash.

    Either way, the problem might appear to go away if you make parameters global - but that does not mean the problem has been eliminated. Restructuring memory used by your program (which is what you will be doing if you make things global) changes the symptom, not the cause.

    Without seeing a sample of code that illustrates your problem, it is impossible to speculate further.

    Try providing a SMALL, COMPLETE, and COMPILABLE sample of code that illustrates your problem. In the process of producing that sample, you might have an "aha!" moment, and find the problem. If not, other people will have a chance of helping you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Have you tried running the program with the aid of a debugger? I don't see how changing a function to depend on global variables could really be the cause of the problem. Depending on global state should be avoided if possible.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK I have made the values global and the problem is still there.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It could be a few things

    How are you examining the return address?
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    This was a program I kind of hacked together from another very similar program.
    But it was working for a good while.
    I kind of agree with what grumpy says, some memory is being over written.
    It a fairly big program to list here.

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    This is some debug code of the fail.

    Code:
    george_shrub posts big blind [$0.10 USD]. VALUE<0.10 USD].>
    betval <0.100000> string <0.10 USD].> <-1>
     KKJ3421ff player no <4> 
     KKJ342ggw 
     KKJ342ggjj Exiting due to signal SIGSEGV
    General Protection Fault at eip=000089ac
    eax=e36c38b0 ebx=000193ec ecx=000193ec edx=e36c38b0 esi=02feb414 edi=000193f7
    ebp=02feaa58 esp=02feaa54 program=Exiting due to signal SIGSEGV
    This is the end of the function, note I have debug code in there.

    Code:
    	printf("\n KKJ342ggw ");
    	fflush(stdout);
    		raiseflag=0;
    	printf("\n KKJ342ggjj ");
    	fflush(stdout);
    }

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK This is the whole of the function


    Code:
    totalit()
     {
    	float tempval;
    	int tmp;
    	pos++;
        //     printf("\n         PID valtemp vals %f start !!",   pname[cpid[pid_no]].tempstack          );
    
    
    	// for(tmp=pos; ((var1[tmp]>='0' && var1[tmp]<='9') || var1[tmp]==',');tmp++){
    		// if (var1[tmp]==',') strcpy(&var1[tmp],&var1[tmp+1]);
    	// }
    
    sscanf(&var1[pos],"%f",&betval);
     printf("\n\n%s VALUE<%s>",var1,    &var1[pos]);	fflush(stdout);
     printf("\nbetval <%f> string <%s> <%d>", betval, &var1[pos], signeg);fflush(stdout);
    
    	csitoutid[player_no]=0;
    	if (strcmp( pname[cpid[player_no]].name, PID)==0) flastard=1; else flastard=0;
    
    	if (anteflag==0) {
    		if (cfid[player_no]==0) { /*first time*/
    		
    			cfid[player_no]=1;
    			if (cbid[player_no]==0) /*not blind */
    			{
    		
    				pname[cpid[player_no]].tookpart++;	
    				if (table_size>=5)
    					pname[cpid[player_no]].tookpart5++;	
    				if (table_size>=8)
    					pname[cpid[player_no]].tookpart8++;	
    				if (table_size>=3)
    					pname[cpid[player_no]].tookpart3++;		
    			}
    		}
    	}
    	anteflag=0; //clear anteflag every time
    
    		tempval=betval;
    
    		if (raiseflag) 
    		{
    		/*	
    			betval-=tempstack2;
    			*/
    // if (pid_no==cpid[player_no]){
    
               printf("\nRaise vals %f %f !!", betval,  pname[cpid[player_no]].tempstack          );
    // }
    
    			betval+=pname[cpid[player_no]].tempstack;
    		
    			printf("\nRaise flag set <%f>",pname[cpid[player_no]].tempstack);
    	
    		}
    
    
    			if (signeg) {
    // printf("\n Total -%f",betval);
    	//			printf("\n signev");
    
    		printf("\n KKJ3421ff player no <%d> ", player_no  );
    	fflush(stdout);
    			 	pname[cpid[player_no]].stack-=betval;
    
    				pname[cpid[player_no]].tempstack-=betval;
    				t_in+=betval;
    				st_in+=betval;
    
    				if (flastard)	p_in-=betval;
    			}
    			else 	{
    			//	printf("\n sigpos %f",betval);
    // printf("\n Total +%f",betval);
    		printf("\n KKJ3421ww ");
    	fflush(stdout);
    				pname[cpid[player_no]].stack+=betval;
    	printf("\n KKJ3421wxxzw ");
    	fflush(stdout);
    				pname[cpid[player_no]].tempstack+=betval;
    				t_out+=betval;
    				st_out+=betval;
    		printf("\n KKJ3421wccw ");
    	fflush(stdout);
    
    				if (flastard)	p_in+=betval;
    			}
    		//}
    		
    		/*
    		if (var1[pos]=='$') {
    		
    			if (signneg) {
    				pname[cpid[player_no]].dolstack-=betval;
    				pname[cpid[player_no]].tempval-=betval;
    				if (flastard) d_in-=betval;
    			}
    			else	{
    				pname[cpid[player_no]].dolstack+=betval;
    				pname[cpid[player_no]].tempval+=betval;
    				if (flastard) d_in+=betval;
    			}
    			
    		}
    		*/
    		
    		
    		/*
    		printf("\nPlayer 5b %f\n",pname[cpid[player_no]].stack);
    		*/
    	//	          printf("\n         PID valtemp vals %f start !!",   pname[cpid[pid_no]].tempstack          );
    	printf("\n KKJ342ggw ");
    	fflush(stdout);
    		raiseflag=0;
    	printf("\n KKJ342ggjj ");
    	fflush(stdout);
    }

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    this is the call to the function, just posting it might help me think lol.

    Code:
    printf("\n debug21 <%s>",&var1[19+namelen]);
    						sscanf(&var1[19+namelen],"%f%[^\n]\n",&betval,var3);
    	
     pos=18+namelen; signeg=MINUS;
    				
    						totalit();
    						break;
    					}

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    So basically I think something is overwriting stuff elsewhere.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    How are you global variables declared?
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Click_here View Post
    How are you global variables declared?
    Code:
    	int pos,signeg;

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Pretty sure the problem lies else where, I think I have noticed an "issue"

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    ... That doesn't account for all the variables that you have used

    I'd say that you are going out of bounds on one of those arrays

    Pretty sure the problem lies else where, I think I have noticed an "issue"
    No worries.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function calls and return address
    By acpower in forum C Programming
    Replies: 2
    Last Post: 06-05-2012, 05:58 AM
  2. Returning Address with Return Statement?
    By Waleed Mujeeb in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2012, 05:47 AM
  3. how to return address of a pointer?
    By spotty in forum C Programming
    Replies: 1
    Last Post: 02-11-2010, 08:11 PM
  4. modifying a return address of a function
    By jay1313 in forum C Programming
    Replies: 3
    Last Post: 09-18-2008, 09:09 AM
  5. Replies: 2
    Last Post: 12-07-2004, 02:31 AM