Thread: goto

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    goto

    Code:
    #include <iostream.h>
    void main(){
      gaurav:
      int i = 0;
      cout<<"here "<<++i;
      goto gaurav;
    }
    /*
    can someone please tell me what happens to the previous i , each time goto is executed. I know goto s are not good to use. Just asking for information. What is the memory behavior in this case ??
    */

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    The i is reset to 0 on each loop. Then because you are precincreminting the i it will be equal to 1 when you display it. If you were to post increment it it would display as 0.

    A better, equavilant program

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	while (1)
    	{
    	int i = 0;
    	cout<<"here "<<++i;
    	}
    }
    and sorry for my spelling today

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    thank for your post , but i know what happens when the program is executed. I wanted to know how memory allocation etc occur. Will it encounter a memory full ?? , Is the memory of previous i locked ??

    How does the stack behave ??

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    there is only one instance of i. All auto variables are allocated on the stack immediately on program entry, not when encountered in the program.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    u mean to say when this function is entered atleast four int memory spaces will be allocated on the stack??

    Code:
    void func(int a){
    if(a){
    int p,q;
    // do anything
    }else{
    int r,s;
    //do anything
    }
    }

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A variable is initializied when its scope is activated I thought.

    For sure though, there will only be one instance of "i" since it is all in the same scope.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by agarwaga
    u mean to say when this function is entered atleast four int memory spaces will be allocated on the stack??

    Code:
    void func(int a){
    if(a){
    int p,q;
    // do anything
    }else{
    int r,s;
    //do anything
    }
    }
    Yes -- the compiler allocates all memory for the function in one big block, and it is not deallocated until the function returns. So, in your example, if sizeof(int) == 4, then the compiler will allocate 4*4= 16 bytes for the 4 variables. It may allocate more variables than you requested, the additonal ones are reserved for the compiler's use for temporary storage.

    Code:
    void func(int a){
    {
      int a,b;
      for( ... )
      {
         int c;
       }
       if( ... )
       {
          int c;
       }
    }
    above will allocate all thee variables (a, b, and c) at beginnnig of the function. Normally there is only one integer allocated for variable c because of scoping.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Wraithan
    A variable is initializied when its scope is activated I thought.
    You are correct -- it is initialized when the variable comes into scope but no additional memory is allocated for it. The same applies to C++ classes -- initialized (constructor called) only when they come into scope but actual allocation on the stack occurs upon function entry.

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by agarwaga
    u mean to say when this function is entered atleast four int memory spaces will be allocated on the stack??
    Maybe? It depends on what your compiler does. Compilers can do whatever they want. Your function here could use no extra memory on the stack, keeping everything inside the processor, depending on the code involved. I would be disappointed if the compiler allocated 4*4 bytes for p, q, r, s, because only two are ever needed at any one time. If your code has some complicated arithmetic expression in it, the compiler might need to use extra stack space to store temporary results. If you have any function calls, stack space will be used to pass arguments to the function.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    By the same token, it could optimise them out of existence altogether - there is no general way of looking at a bit of code and deciding how much stack space a function needs.

    Additionally, if you have a mix of local variable types, then there may be padding between them which cannot be detected from simply looking at the code. char arrays for example are often rounded up to multiples of 4 or 8 to preserve alignment of other data on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM