Need Help with a warning.. 'C'

Hello.. I get this warning when I try to compile:

1430 Warning: 'fptr2' may be used uninitialized in this function
[-Wmaybe uninitialized]

The problem is that this warning comes up when I try to compile in release mod
If I compile in Debug mod I have 0 warnings and 0 errors.

How could this be possible ?

This is my function that says 'fptr2' is uninitialized:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void arrangeGameScore(FILE *file1, FILE *file2)
{
    char fname[MAX] = "score.txt";
    char temp[100][Length] = {};
    char tmp[100][Length] = {};
    char user[Length] = {};
    int score[100] = {};
    int lin;

    file1 = fopen(fname, "r");

    for(lin = 0; fscanf(file1, "%9s %i", user, &score[lin]) != EOF; ++lin)
    {
        strcpy(temp[lin], user);

        for(int i = 0; i < lin + 1; ++i)
        {
            for(int j = 0; j < i; ++j)
            {
                if(score[i] > score[j])
                {
                    int a = score[i];
                    score[i] = score[j];
                    score[j] = a;

                    strcpy(tmp[0], temp[i]);
                    strcpy(temp[i], temp[j]);
                    strcpy(temp[j], tmp[0]);
                }
            }
        }
    }

    file2 = fopen("temp.txt", "w");

    for(int line = 0; line < lin; ++line)
    {
        fprintf(file2, "%-6s        %i\n", temp[line], score[line]);
    }

    fclose(file1);
    fclose(file2);

    remove(fname);
    rename("temp.txt", "score.txt");
}


This function is actually in a .dll and I call it from my main project..
it doesn't make sens, why this warning comes ?!?..
Last edited on
Hello Mif,

Your warning says: 1430 Warning: 'fptr2' may be used uninitialized in this function. Yet you provide code that does not define "fptr2" or even use this variable.

The warning message does not look complete. I would expect it should say what function the problem is in.

The solution would be to ALWAYS initialize your variables when defined. Better to have a good value rather than a garbage value.

Andy
Hello Andy.. Yes I did understood what the warning meant.. and I did search to see if forgot to declare this .... but as I did mentioned this WARNING comes only when I compile in "RELEASE" mode otherwise in DEBUG all goes fine..
However the function has a declaration about the fptr2 variable and it's even used 9 times in this other function here is a piece of the code because it has like 550~ lines I will provide the beginning where the variables are declared and where the the region where the warning was found:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
void logic(struct INFO* info, char **tmp2, enum eDirection* dir)
{
    FILE *fptr1, *fptr2, *fptr3;
    char score[MAX] = "score.txt";
    char temp[MAX];
    int lin;

    char go[] = {"Game  Over"};
    int c = 10;

    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;

    for(int i = 1; i < nTail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }

    switch(*dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }

    if(x == fruitX && y == fruitY)
    {
        srand(time(NULL));
        PlaySound(MAKEINTRESOURCE(IDW_EAT), NULL, SND_RESOURCE | SND_ASYNC);
        Score += 1;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }

    if(!mod)
    {
        if(Map == 1)
        {
            if(x >= width)
            {
                x = 0;
            }
            else if(x < 0 )
            {
                x = width - 1;
            }
            if(y >= height)
            {
                y = 0;
            }
            else if(y < 0 )
            {
                y = height - 1;
            }
        }
....}
..
...
..
..
        for(int i = 0; i < nTail; i++)
        {
            if(tailX[i] == x && tailY[i] == y)
            {
                alSourceStop(uiSources[0]);
                alDeleteSources(MAX_NUM, &uiSources[0]);
                alDeleteBuffers(MAX_NUM, &uiBuffer);
                play = FALSE;

                PlaySound(MAKEINTRESOURCE(IDW_DEAD), NULL, SND_RESOURCE | SND_ASYNC);
                gotoxy(36, 34);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
                printf("You've touched your tail with the had");

                fptr1 = fopen(score, "a+");

                rewind(fptr1);
                for(lin = 1; fscanf(fptr1, "%19s %i", info->user, &info->score) == 2 && fgets(score, 999, fptr1) != NULL; ++lin)
                {
                    if(strcmp(*tmp2, info->user) == 0 && Score > info->score)
                    {
                        fptr3 = fopen("temp1.txt", "a+");
                        fprintf(fptr3, "%-6s         %i\n", *tmp2, Score);
                        rewind(fptr3);
                        fgets(temp, 999, fptr3);

                        fptr2 = fopen("temp.txt", "w");

                        rewind(fptr1);
                        for(int lineCtr = 1; fgets(score, 999, fptr1) != NULL; ++lineCtr)
                        {
                            fprintf(fptr2, "%s", lineCtr != lin ? score : temp);
                        }

                        fclose(fptr1);
                        fclose(fptr2);
                        fclose(fptr3);

                        remove("temp1.txt");
                        remove("score.txt");
                        rename("temp.txt", "score.txt");

                        arrangeGameScore(fptr1, fptr2); // I use it even here I don't get any Warnings

                        system("attrib +h +s score.txt");
..
..
..
            fseek(fptr1, 0, SEEK_END);
            fprintf(fptr1, "%-6s        %i\n", *tmp2, Score);
            fclose(fptr1);

            arrangeGameScore(fptr1, fptr2); // <- Here I got the Warning !?

            system("attrib +h +s score.txt");
Last edited on
Sorry for the
code
, I just pickup here and there without closing the brackets.. but to show you that the variables are all declared at the start of the function..
It is a bit hard to tell but it looks like there may be a path that bypasses the fopen() on line 107 that would initialise the pointer and uses the pointer on line 133.
The line you indicate is outside a block where that variable is initialized. It is possible for that block to never execute, so it's possible that it won't be initialized.

Your basic problem is that you are passing fptr1 and fptr2 into arrangeGameScore for no reason whatsoever. Inside that function you are using them like local variables. Just remove them from the declaration and don't pass them. They should just be local.

Hello Mif,

I do not compile programs in "release" mode very often, but when I do they have been compiled and tested in "Debug" mode making sure all warnings and errors have been taken care of first.

Also if you get in the habit of initializing your variables when defined you will eliminate any potential problem right from the off.

In this case I am thinking: FILE *fptr1 = NULL, *fptr2 = NULL, *fptr3 = NULL;. Unless you can use: FILE *fptr1{}, *fptr2{}, *fptr3{nullptr}; with "fptr3" being another option.

The other part I find strange is that the "fptr" variables are given a value before they are used.

Something that just occurred to me. I do not recall if you ever mentioned what IDE you are using or if I just forgot.

In debug mode the input files are in the proper current working directory and everything is fine because you do not check if the file is open and ready to use.

But in "release" mode the ".exe" file may be in a different directory, so when the program runs the current working directory is different and with out a proper path to your files they can not be opened. The exception might be that a file opened for output may be created where it needs to be.

By not checking that the files are open the program continues as if nothing is wrong and you may not get any results from reading and writing the files until you eventually realize that it did not work.

Andy
The pointer at line 107 is initialized but at 116 is closed. I always close it after I worked with it.. but yes I'm searching it since what I provide is a mess.. I mean it has no sense, cose it's taken from here and there, but I still wonder if I'm having this warning in RELEASE mod why does in DEBUG don't appear?
TBH this function I call from the auxiliar.dll, is not even used in the main function is rather called in game.dll ... so even if I could provide the code, cause I don't really care is just a stupid game.. but the space here wouldn't allow me to paste it.. :\
Last edited on
Okay Andy I got it.. I just initialize the pointers right away... and there are no warnings no more.. saying that I just realize that everytime I did initialize the fptr2 = NULL in front of my function another warning was few lines ahead and so on... until I did go up to the beginning and NULL all pointers :)
I use Code:Blocks still 17.12 I didn't upgrade it to 20 but I think you're right since me either I don't compile often in release mode I only use debug until I finish the project, but yes still the release has a different directory, maybe that could be the cause.

Thank you for the help.. Cheers !!
Hello dutch, and thank you for help, but I did solved my problem ... meaning Andy was opened my eyes and I did not initialize my pointers at the beginning how it should be. To answer you at what you said .. well I did mentioned I just provided code from a function is not the entire cause is to much to paste in here.. so I pick up some pieces,.. really they don't have any meaning reading it like that and I apologies. I am passing those pointers in that function cause that function needs only those pointers and plus if I write the function in to the other one will become more and more bigger. Well i do not want that .. I want my function to become smaller to be readable. Cheers !!!
The pointer at line 107 is initialized but at 116 is closed.

Line 107 isn't initialization, it's assignment. There's a difference.

More importantly, there is a path through the code that would lead to line 107 never being executed, which would lead to the pointer never having any value assigned to it before line 133 attempts to use it, causing undefined behaviour. This is what your compiler is warning you about.

I still wonder if I'm having this warning in RELEASE mod why does in DEBUG don't appear?

I think I'm right in saying that, in DEBUG mode, Visual C++ initialises values to 0 if they're not initialized, whereas in RELEASE mode, it doesn't initialize them, leading to undefined behaviour.

This has always seemed to me to be completely the wrong way round, but Microsoft have chosen to make it this way for some reason.

EDIT: And dutch is absolutely correct about your arrangeGameScore() function. You don't do anything at all with the values you pass into the function, so you shouldn't be passing them in at all.
Last edited on
Ahh okay
MikeyBoy
.. now I understand.. sorry about the early replay,,, I was thinking in other places.
Now I understand why .. if nothing is needed inside the arrangeGameScore() function, then why to pass the pointers.. Yeah right good question. :)

Thanks for the advice and Sorry about that
dutch
now I did understand what you meant.
Thanks again guys.
Last edited on
No problem - glad it helped!
Vindication!
(Thanks MikeyBoy!)
Registered users can post here. Sign in or register to post.