error C4700: an uninitialized local variable used

struct BITMAPFILEHEADER { }; struct BITMAPINFOHEADER { }; struct RGBQUAD { }; void firstaction(BITMAPFILEHEADER, BITMAPINFOHEADER, RGBQUAD, FILE); int main() { BITMAPFILEHEADER header; BITMAPINFOHEADER bmiHeader; RGBQUAD *pixels; firstaction(header, bmiHeader, *pixels, *pFile); // тут ошибка на первые 3 аргумента } void firstaction(BITMAPFILEHEADER header, BITMAPINFOHEADER bmiHeader, RGBQUAD *pixels, FILE *pFile) { } 

Who can tell what I was wrong?

  • one
    There is no initialization of these variables in your code. And by default, the compiler won't do it for you with ++. - KoVadim
  • BITMAPFILEHEADER header; BITMAPINFOHEADER bmiHeader; RGBQUAD * pixels; Isn't that initialization? - N191119
  • For header and bmiHeader I'm not sure what the rules say about the default constructor. But with pixels problem is absolutely certain: RGBQUAD * pixels code; only declares a pointer to RGBQUAD , but its value is arbitrary. You need to first allocate memory for a variable of type RGBQUAD , and initialize the pointer with the address of this variable. - VladD
  • In addition to the lack of initialization, I see here such a problem as the transfer of structures by value. By the way, all listed structures refer to standard WinAPI structures. And these are structured structures, so there’s no question even about any constructor. - insolor 4:34 pm

2 answers 2

  1. At you swears on a variable-pointer *pixels , which you dereference, without initializing.
  2. You have two overloaded transaction functions. One accepts structures in the argument (it is simply defined), the second - 2 structures, 2 pointers to the structure. Judging by this function call function

    firstaction (header, bmiHeader, * pixels, * pFile);

    You call the first implementation of the transaction function, this one

    void firstaction (BITMAPFILEHEADER, BITMAPINFOHEADER, RGBQUAD, FILE);

    But it is not defined for you (only declared), in connection with this the linker will swear because the implementation of the function has not been found.

  3. The variable pFile not declared anywhere in your example. Where does it come from?

If your project is written in VisualStudio 2013, then to disable the error about variable initialization, run

In the project properties - configuration properties - С / С ++ - code generation - security check - Disable security check (/ GS-)

Then point 1 will disappear, but the problem of point 2 will appear.

     void firstaction(BITMAPFILEHEADER, BITMAPINFOHEADER, **`RGBQUAD`**, FILE); void firstaction(BITMAPFILEHEADER header, BITMAPINFOHEADER bmiHeader, **`RGBQUAD *`**pixels, FILE *pFile) 
    • one
      I also wanted to clarify, the C4700 is generally a warning, not a mistake. Initializing variables before passing to a function is of course your business :) You most likely have a link error, since I see that you have declared a function with 4 structure arguments, and in the implementation of the function two structures and two pointers. Here the linker swears that he cannot find the implementation of the declared function. - vinnie
    • And what is your answer, sorry? By the way, the use of uninitialized variables is UB, so this is not an error, but a gross error. - VladD
    • 2
      > Why is this "using uninitialized variables a blunder"? This is usually a warning because it is a really gross mistake. There are not only compilation errors. There are also runtime errors and logic errors. Both those and others are much worse than compilation errors, because they can destroy the program. - DreamChild
    • one
      @Gimka: You are mistaken in the belief that initialization does not affect the operation of the program: the compiler does not work line by line. An uninitialized variable according to the standard allows the compiler to remove the code containing it, or to rewrite it arbitrarily. - VladD
    • one
      @Gimka:> he still doesn’t change the logic. Wrong. Here is the correct code: int x = 12345; int y = x - x; And this: int x; int y = x - x; There is a UB and has the right to format your hard drive. Think about it. - VladD