Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: default value of uninitialized local variable and uniforms

  1. #1
    Newbie Newbie
    Join Date
    Feb 2016
    Posts
    3

    default value of uninitialized local variable and uniforms

    what is the default value of uninitialized local variables?
    Code :
    float myFloat;
    Is it zero?
    What is the value of an uniforms if it hasn't been set?
    Code :
    uniform float myFloat;

    Can't find anything in the spec.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    Quote Originally Posted by checkcheck View Post
    what is the default value of uninitialized local variables?
    Code :
    float myFloat;
    Is it zero?
    Depends. If you're talking about C/C++, and if it's a debug build your compiler may initialize it to something that's useful for detecting uninitialized variables in a debugger. If it's a release build you'll most likely get stack garbage in the variable, so it could be whatever was last on the stack, so - in theory - anything.

    If you're talking about uninitialized local variables in GLSL, section 5.9 of the GLSL spec (using version 4.50) has the answer:
    Reading a variable before writing (or initializing) it is legal, however the value is undefined.
    As a general rule, if you're writing code in any language that depends on uninitialized local variables having some value, don't. Stop. Just initialize the variable instead. You'll thank yourself for it later.

    Quote Originally Posted by checkcheck View Post
    What is the value of an uniforms if it hasn't been set?
    Code :
    uniform float myFloat;

    Can't find anything in the spec.
    Using the 4.50 spec, section 4.3.5 has the answer:
    All uniform variables are read-only and are initialized externally either at link time or through the API. The link-time initial value is either the value of the variable's initializer, if present, or 0 if no initializer is present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •