Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


How deep can you nest functions in c++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 LAURENT*   Members   -  Reputation: 266

Like
0Likes
Like

Posted 27 August 2014 - 09:24 AM

I nested a lot of functions, while I'm not lost with what I did, I am confused about why the last nested function is giving me problems with the function arguments. Here is some code from the second nested function to the last in spefic order.

 

bool touches_wall( SDL_Rect box, SDL_Rect ys, Tile *tiles[], SideTile *walls[] )

 

2#

In this function I call  check_collision_region

 bool touches_wall( SDL_Rect box, SDL_Rect ys, Tile *tiles[], SideTile *walls[] ) 
 { 
	int count = 0;
	//Go through the tiles 
	for( int t = 0; t < TOTAL_TILES; t++ ) 
	{ 
		//If the tile is a wall type tile 
		if( ( tiles[ t ]->get_type() >= TILE_CENTER ) && ( tiles[ t ]->get_type() <= TILE_TOPLEFT ) ) 
		{ 
			//If the collision box touches the wall tile 
			if( check_collision( box, tiles[ t ]->get_box() ) == true ) 
			{ 
				 return true; 
			}  
		} 
	} 

	for( int s = 0; s < TOTAL_TILES_WALL; s++ ) 
	{ 
		//If the tile is a wall type tile 
		if(  walls[ s ]->get_type() == TILE_RED_WALLs ) 
		{ 
			//If the collision box touches the wall tile 
			if( check_collision_region( box, walls[s]->get_base_box(), ys, walls[s]->get_box() ) == true ) 
			{ 	
					return true;
			}  
		} 
	} 

 //If no wall tiles were touched 
 return false; 
 }

3# in this function I call check_collision_topside

bool check_collision_region(SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D )
 {
	 //The sides of the rectangles 
	int leftA, leftB, leftC, leftD; 
	int rightA, rightB, rightC, rightD; 
	int topA, topB, topC, topD; 
	int bottomA, bottomB, bottomC, bottomD; 

	bool regionTop;
	bool regionBottom;
	bool regionNone;

	//Calculate the sides of rect A 
	leftA = A.x; 
	rightA = A.x + A.w; 
	topA = A.y; 
	bottomA = A.y + A.h; 

	//Calculate the sides of rect B 
	leftB = B.x; 
	rightB = B.x + B.w; 
	topB = B.y; 
	bottomB = B.y + B.x;

	//Calculate the sides of rect C 
	leftC = C.x; 
	rightC = C.x + C.w; 
	topC = C.y; 
	bottomC = C.y + C.h; 

	//Calculate the sides of rect D 
	leftD = D.x; 
	rightD = D.x + D.w; 
	topD = D.y; 
	bottomD = D.y + D.x;
	

	//Test
	 //If any of the sides from A are inside of B 
	if(( bottomA >= topB ) && ( bottomA <= topB + TILE_WALLs_HEIGHT/2 ) && (rightA >= leftB) &&(leftA <= rightB)) 
	{ 
				
		if (check_collision_topside(  SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D) == true) 
		{
			return true;
		}
		
	} 

	//Second function 
/*	if(( bottomA <= bottomB) && (bottomA >= bottomB - TILE_WALLs_HEIGHT/2 ) )
	{ 
		if((rightA >= leftB) &&(leftA <= rightB))
		{		
			//regionBottom = true; 
			if (check_collision_bottomside( SDL_Rect A, SDL_RECT B, ,SDL_Rect C, SDL_Rect D) == true)
			{
				return true
			}
		}
	} */





	return false;
 }

4# I haven't really worked on the forth yet it's kinda generic but I will fine tune it if I can get the 3# to work.

bool check_collision_topside( SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D) 
 { 
	//The sides of the rectangles 
	int leftA, leftB, leftC, leftD; 
	int rightA, rightB, rightC, rightD; 
	int topA, topB, topC, topD; 
	int bottomA, bottomB, bottomC, bottomD; 


	int top;


	//Calculate the sides of rect A 
	leftA = A.x; 
	rightA = A.x + A.w; 
	topA = A.y; 
	bottomA = A.y + A.h; 

	//Calculate the sides of rect B 
	leftB = B.x; 
	rightB = B.x + B.w; 
	topB = B.y; 
	bottomB = B.y + B.h; 

	//Calculate the sides of rect C 
	leftC = C.x; 
	rightC = C.x + C.w; 
	topC = C.y; 
	bottomC = C.y + C.h; 

	//Calculate the sides of rect D 
	leftD = D.x; 
	rightD = D.x + D.w; 
	topD = D.y; 
	bottomD = D.y + D.x;


	//the top collision only
	top =  bottomA - topB; 

    //If any of the sides from A are outside of B 
	if( bottomC <= topB + top  ) 
	{ 
	
		return false; 
	} 

	if( bottomC >= topB + TILE_WALLs_HEIGHT/2 ) 
	{ 
		return false; 
	} 

	if( rightA <= leftB ) 
	{ 
		return false; 
	} 

	if( leftA >= rightB ) 
	{ 
		return false; 
	} 

	//If none of the sides from A are outside B 
	return true; 
 } 

Edited by LAURENT*, 27 August 2014 - 09:38 AM.


Sponsor:

#2 KulSeran   Members   -  Reputation: 2662

Like
9Likes
Like

Posted 27 August 2014 - 10:31 AM

1) Please be specific about "problems".  Compile errors? wrong answers?

    Looks like your issue is actually a compile error, notice how you are attempting to call the function in check_collision_topside(), if that's the inputs you really wanted, remove all the SDL_Rect from that line to get "check_collision_topside(A,B,C,D);"

 

2) If it's wrong answers, use your debugger.  When run in debug mode it's possible to step through your code and see exactly where inputs aren't doing what you thought they should. 

 

3) You can nest calls in C++ until you run out of space on your stack.  The nesting itself isn't the issue.



#3 DiegoSLTS   Members   -  Reputation: 2048

Like
0Likes
Like

Posted 27 August 2014 - 11:06 AM

KulSeran is right, you're having compilation errors. But apart from that, why are you using 4 rects in each function? Collision detection is usually done betwen 2 objects. It looks like you don't use D, and you only use of C looks really weird:

//If any of the sides from A are outside of B 
	if( bottomC <= topB + top  )

What does the condition have to do with the comment?

 

Also, you do:

//the top collision only
	top =  bottomA - topB; 

and immediatelly after it you use "top" this way:

    //If any of the sides from A are outside of B 
	if( bottomC <= topB + top  ) 

That condition is the same as "bottomC <= bottomA" (topB + top = topB + (bottomA - topB) = bottomA).

 

So, even with nested functions not being a problem here, are you sure you need 3 nested functions to do a collision check?



#4 aregee   Members   -  Reputation: 1037

Like
1Likes
Like

Posted 28 August 2014 - 07:59 AM

I tried to provoke Xcode, just for fun.  Note that this is a limit imposed by the compiler, not necessarily the standard itself.

 

The limit is 256 levels, 255 selected ones plus the one used for the method itself:

 

nestinglevel.png

 

Edit:

 

Seems like there even is a command line option to allow for more brackets, so it is definitively not a restriction in the standard:

 

-fbracket-depth=N
Sets the limit for nested parentheses, brackets, and braces to N. The default is 256.

Edited by aregee, 28 August 2014 - 08:14 AM.


#5 DiegoSLTS   Members   -  Reputation: 2048

Like
1Likes
Like

Posted 28 August 2014 - 08:36 AM

Right, there was something I wanted to say in my last post and you made me remember it.

 

Nesting is what aregee did, blocks inside blocks (like chained "if" statements). Calling functions from another functions (what the OP did) is not nesting and it's not limited by the bracket depth, otherwise you'll be limited in your app by the call hierarchy of any external library you're using, or using recursive functions won't work well.


Edited by DiegoSLTS, 28 August 2014 - 08:37 AM.


#6 LennyLen   GDNet+   -  Reputation: 4319

Like
1Likes
Like

Posted 28 August 2014 - 09:16 AM

Nesting is what aregee did, blocks inside blocks (like chained "if" statements). Calling functions from another functions (what the OP did) is not nesting and it's not limited by the bracket depth, otherwise you'll be limited in your app by the call hierarchy of any external library you're using, or using recursive functions won't work well.

 
It's also worth noting that nesting within a function is not the same thing as a nested function.  Neither standard C or C++ allow nested functions (though gcc does have an extension to allow nested functions).
 
edit: I should give an example.
 
This is nesting within a function:
 



int foo() {
 
  int x = 5;
  {
    x++;
    {
      x++;
    }
  }
  x++;
 
 return x;
 
}

 
This is a nested function:
 



int foo() {
 
  int bar() {
    int x = 5;
    x++;
 
    return x;
  }
 
  return bar();
 
}

Edited by LennyLen, 28 August 2014 - 09:22 AM.


#7 Bregma   Crossbones+   -  Reputation: 5840

Like
0Likes
Like

Posted 28 August 2014 - 10:21 AM

Well, the standard does not impose any limits to either nested lexical blocks or runtime function calls.  Any such limits are implementation details.  Consult your compiler and/or OS documentation.


Stephen M. Webb
Professional Free Software Developer

#8 LAURENT*   Members   -  Reputation: 266

Like
0Likes
Like

Posted 28 August 2014 - 10:30 AM

Alright thank you all I see my error with the function argument.

 

 

Also for any questions regarding the collision detection. It's is somewhat unique but not a real complex concept. To summarize it my collision detection require one extra step before collision is detected.

 

 

Edited: Now that I knew there wasn't a problem with nesting I turned my attention to other potential problems which allowed me to figure out what I was doing wrong. Thanks guys


Edited by LAURENT*, 28 August 2014 - 11:04 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS