Hammer, a question for you - var scope

This is a discussion on Hammer, a question for you - var scope within the C Programming forums, part of the General Programming Boards category; >but is it part of the C standard or the C++ standard? I'm not sure about the C++ standard, but ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but is it part of the C standard or the C++ standard?
    I'm not sure about the C++ standard, but I'm quite sure about C89 and most likely C99 as well. I would have to assume that it is supported in C++ for compatibility with C though.

    >print out a dissassembly and post it?
    Gladly, I had to modify the program so that it would actually compile, so the disassembly of the following code:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int a = 2;
      {
        int a = 0;
        a++;
      }
      printf ( "%d\n", a );
      return 0;
    }
    Which gives me one warning in Lint:
    Code:
    cTest.c: (in function main)
    cTest.c(7,9): Variable a shadows outer declaration
      An outer declaration is shadowed by the local declaration. (Use -shadow to
      inhibit warning)
       cTest.c(5,7): Previous definition of a: int
    Is as follows for MSVC++ 6:
    Code:
    1:    #include <stdio.h>
    2:
    3:    int main ( void )
    4:    {
    00401000   push        ebp
    00401001   mov         ebp,esp
    00401003   sub         esp,48h
    00401006   push        ebx
    00401007   push        esi
    00401008   push        edi
    00401009   lea         edi,[ebp-48h]
    0040100C   mov         ecx,12h
    00401011   mov         eax,0CCCCCCCCh
    00401016   rep stos    dword ptr [edi]
    5:      int a = 2;
    00401018   mov         dword ptr [ebp-4],2
    6:      {
    7:        int a = 0;
    0040101F   mov         dword ptr [a],0
    8:        a++;
    00401026   mov         eax,dword ptr [a]
    00401029   add         eax,1
    0040102C   mov         dword ptr [a],eax
    9:      }
    10:     printf ( "%d\n", a );
    0040102F   mov         ecx,dword ptr [ebp-4]
    00401032   push        ecx
    00401033   push        offset string "%d\n" (0040c01c)
    00401038   call        printf (00401060)
    0040103D   add         esp,8
    11:     return 0;
    00401040   xor         eax,eax
    12:   }
    00401042   pop         edi
    00401043   pop         esi
    00401044   pop         ebx
    00401045   add         esp,48h
    00401048   cmp         ebp,esp
    0040104A   call        __chkesp (004010e0)
    0040104F   mov         esp,ebp
    00401051   pop         ebp
    00401052   ret
    -Prelude
    My best code is written with the delete key.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Glad to see you've been having fun without me
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I think vc in debug mode will make anyone write assembly

  4. #19
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    void f();
    
    int main(void)
    {
    	f();
    	return 0;
    }
    
    void f()
    {
    	int A[10];
    
    	for (int i = 0; i < sizeof A / sizeof (int); ++i) {
    		A[i] = 2;
    		int B[10];
    		for (int j = 0; j < sizeof A / sizeof(int); ++j)
    			B[j] = 1;
    	}
    }
    And this is what borland's c++ compiler does
    Code:
       ;	
       ;	void f()
       ;	
    	push      ebp
    	mov       ebp,esp
    	add       esp,-80
    	lea       ecx,dword ptr [ebp-80]
       ;	
       ;	{
       ;		int A[10];
       ;	
       ;		for (int i = 0; i < sizeof A / sizeof (int); ++i) {
       ;	
    ?live16386@16: ; ECX = &B
    @4:
    @5:
    	xor       edx,edx
       ;	
       ;			A[i] = 2;
       ;	
    ?live16386@32: ; EDX = i, ECX = &B
    @6:
    	mov       dword ptr [ebp+4*edx-40],2
       ;	
       ;			int B[10];
       ;			for (int j = 0; j < sizeof A / sizeof(int); ++j)
       ;	
    @8:
    @9:
    	xor       eax,eax
       ;	
       ;				B[j] = 1;
       ;	
    ?live16386@64: ; EAX = j, EDX = i, ECX = &B
    @10:
    	mov       dword ptr [ecx+4*eax],1
    @12:
    	inc       eax
    	cmp       eax,10
    	jl        short @10
    ?live16386@96: ; EDX = i, ECX = &B
    @14:
    @15:
    	inc       edx
    	cmp       edx,10
    	jl        short @6
       ;	
       ;		}
       ;	}
       ;	
    ?live16386@112: ; 
    @17:
    	mov       esp,ebp
    	pop       ebp
    	ret 
    @@f$qv	endp
    @f$qv	ends
    _TEXT	ends
    So for both arrays it pushes the stack down 80 bytes.

  5. #20
    TK
    Guest
    Most of these things depend on what compiler you are using. VC++6 breaks a lot of rules, it doesn't meet the C Standard as well as Linux/Unix gcc.

  6. #21
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Seemingly, to create additional layers of scope as this change adopts can only mean that the stackframe has to be larger than it otherwise would be, because although the variable names could be the same inside and outside of braces, separate data spaces must be allocated for each. Kinda foolish, I think.<

    To re-iterate Salems point, scope is a language feature and wouldn't necessarily have to have a bearing on the asm that is produced, unless you're trying to produce asm from the C source code that follows scope rules. However, this may mean that something like -

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int* ptr;
    
    	{
    		int a=10;
    		ptr=&a;
    	}
    
    	printf("%d",*ptr);
    
    	return 0;
    }
    would always work, even though 'a' is out of scope.
    Last edited by Sorensen; 06-22-2002 at 05:58 PM.

  7. #22
    Guilty Spark 343
    Guest
    One moment while I access the archive... hmmm..hmmm.mmmmmm..dum..de.dum....hmmmm.hmm.hmmmmm .mmmmm.....

    Ah, here it is: Index 898127--

    In reference to the Reclaimer's statement:

    > scope is a language feature and wouldn't necessarily have to have a bearing on the asm

    In fact, a subtle influence will be observed in the assembly code itself, based on what register, register size, offset method, and addressing mode is used in the function in question. Scope is a 'logical' construct which controls the physical. A method of managing namespace.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question about scope resolution
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2002, 10:04 PM
  2. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  3. Question about C# scope rules
    By converge in forum C# Programming
    Replies: 3
    Last Post: 01-30-2002, 06:56 AM
  4. Scope question
    By mikebrewsj in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2002, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21