const variable...

This is a discussion on const variable... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { const int a; printf("%d\n\n\n", *a); return 0; } when i compile and execute the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Question const variable...

    Code:
    #include <stdio.h>
    
    int main(void) {
    	const int a;
    	printf("%d\n\n\n", *a);
    	return 0;
    }
    when i compile and execute the above code on
    a) Windows, Visual Studio (compiled as C code): it gives one warning on compilation and crashes on execution
    b) Linux, and compiled using gcc and flags -Wall: there are no warnings and prints a garbage value on execution
    c) Windows, Visual Studio (compiled as C++ code): does not compile and throws an error

    what is the expected behaviour in this case??

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    What do you expect when dereferencing int?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Code:
    #include<iostream>
    using namespace std;
    
    int main() {
    	const int a;
    	cout<<a;
        return 0;
    }
    in case of compilation as C++, i used this code

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    i just wanted to know the behavior ...
    so i wrote this code..thus i was not expecting anything specific

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    printf("&#37;d",a);
    should be
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    If your compiler honest and truly compiled that without problems then yours is broken.

    Anything similar is just as incorrect because a is not a pointer type.
    Code:
    int bar ( const int a ) 
    {
       return *a; 
    }
    
    int main ( void )
    {
        const int foo;
        bar( foo );
    
        return 0;
    }
    
    bar.c
    bar.c(5) : error C2100: illegal indirection

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    what is broken???

    and u can compile the above code to confirm what i have stated...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf("%d\n\n\n", *a);
    This line is broken, as it's dereferencing an integer a as a pointer - and the compiler should not allow that.

    My gcc 3.4.x gives this:
    Code:
    t.c: In function `main':
    t.c:5: error: invalid type argument of `unary *'
    This is not a clear error message, but it says that you have a "*" that doesn't belong there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As to the initialization of the variable, in gcc, the flow analysis isn't being done unless you run at least -O2, so it has no way to determine if the code initializes a variable or not - it is perfectly valid to do:
    Code:
    const int a;
    ...
    // Stuff that doesn't use a here. 
    a = 3;
    ... 
    // Stuff that uses a here.
    It is of course better to do it in one line.

    I'm not sure about MS compilers, but I believe that too is only doing flow analysis in the higher optimization levels.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    I agree with Mats...same result on SuSE 10 with gcc 4.1.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 03:22 AM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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