8

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?

2
  • 1
    There's no way we can tell if you initialized it or not.
    – chris
    Feb 27, 2014 at 5:03
  • How you have declared pNamesPtr. Add the code for the same..
    – HadeS
    Feb 27, 2014 at 5:04

4 Answers 4

9

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.

4
  • This happens to me a lot when I'm not careful - it's also true if you have an if-else if-else kind of setup, whereby you know the value of a variable/pointer will be set, since you have an else, but since they're all only sometimes called, the compiler will likely complain
    – chrisb2244
    Feb 27, 2014 at 5:16
  • 1
    @chrisb2244 yes, if we wanted to generalize my statement: This warning happens if you try to use a variable that isn't initialized on all possible code execution pathways between variable initialization and variable usage. So any control flow statement that generates code with "jump/goto" can cause this, for example switch could do the same but a more exotic control flow example: break-ing out from a loop may cause this if the initialzation of the variable is inside the loop after your break statement... Feb 27, 2014 at 5:26
  • correcting my previous comment: between variable declaration and variable usage Jun 27, 2015 at 21:37
  • else is equal to goto.
    – Johnny
    Feb 26 at 16:15
3

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.

0

This error mean that compier don't know if used variable have assigned value. For example:

int i;
if( someBool )
    i= 123;
someFunction();
if( someBool )
    printf("%d", i );

If someFunction() change value of someBool then you may end up calling printf with i argument while i doesn't have any value assigned.

If you are sure that your code is correct you may disbale that error in Visual Studio by putting at the top of your cpp file line:

#pragma warning (disable: 4703)

Or you may disable that check for whole project by going to Properties / C/C++ / General / SDL checks and set it to No (/sdl-).

Alternatively if you don't care about performance you may just assign anything to pNamesPtr where it's defined.

-1

Put this in your code:

xtype *pNamesPtr = NULL
1
  • 2
    I think this answer could be improved, it's not clear where it should be put and you're not highlighting the code with the proper formatting.
    – phaberest
    Feb 15, 2016 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.