Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C4703: potentially uninitialized local pointer variable 'pNamesPtr' used

Tags:

c++

I'm working on a crypter project and ran into the following error when attempting to compile the program.

main.cpp(520): error C4703: potentially uninitialized local pointer variable 'pNamesPtr' used
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

        DLLNAMES[i].UsedAlready = 0;
    }


    *dwOutSize = (DWORD)pNamesPtr - (DWORD)pBuffer;//*<----is line 520 of error
    *dwImportsSize = *dwOutSize - *dwIATSize;    
    return pBuffer;
}
#pragma pack(1)
typedef struct

Can someone help me with this error? Do you need more code in order to have a good answer?

like image 860
user3191413 Avatar asked Feb 27 '14 05:02

user3191413


2 Answers

This warning isn't always a bug, sometimes its just the result of an optimization. Since it is in your code and you don't know what is this it might actually be a bug.

For example if I write:

int i;

if (this_and_that)
    i = 5;

if (whatever)
    printf("%d\n", i);  // <--- this may give a potential blahblah warning

If you are optimizing then you may know that the value of whatever is always true when the value of this_and_that is true so if printf gets called then i is already guaranteed to be initialized but the compiler often can not figure out the relation between this_and_that and whatever, this is why you get the warning. One possible quick fix to this warning is initializing the variable to a default value right where you declare it. In my opinion sparing with initialization is a bad practice and a source of a lot of bugs.

like image 65
pasztorpisti Avatar answered Nov 11 '22 18:11

pasztorpisti


It means that

  • you don't initialise pNamesPtr when you declare it, so it starts with an invalid value; and
  • the compiler can't be sure that you will have assigned a valid value to it before using it.

Check all the code paths from the declaration to the point of use. Do they all assign something sensible to the variable? If not, fix it so they do.

If they do, and you're convinced that you're assigning it correctly, could you simplify the code so that it's obvious to the compiler that it is?

If all else fails, then you could silence the compiler by initialising it to nullptr or some other default value in the initialisation. But only do that if you're really sure your code is correct - compilers are usually good at spotting mistakes like this.

like image 20
Mike Seymour Avatar answered Nov 11 '22 20:11

Mike Seymour