简体   繁体   中英

How to get the name of the pointer variable if the address is known?

When i run my project it loads up and starts running but crashes after sometime. The log that i get is this:

Attempt to free invalid pointer 0x2df2fc6b9000

Now this is chromium code and i don't know where in this code base is the program going wrong. So i wanted to know if it is possible to get the variable name give the address 0x2df2fc6b9000 . Thankyou.

You cannot.

For example:

int i = 10;
int* p1 = &i;
int* p2 = p1;

Given &i , there is no way to say the variable is p1 or p2 . For all we care, there may not be any p1 or p2 at all.

That error message is a strong indication that your code is using memory incorrectly. You'll have to try to reduce your code to a Minimal, Reproducible Example . You might find the source of the problem in the process of doing that.

There's no way to tell what variable is stored at a particular memory unless you print the memory addresses and variable names from your program.

With modern C++ you can use smart pointers: unique_ptr, shared_ptr and weak_ptr. They use RAII, so they will clear up the memory for you.

Instead of managing pointers yourself with new and delete try to use smart pointers as much as possible.

You might find the name of the variable if you run a debug version of your program with symbol information included, in a debugger. The debugger might display the list of symbols on the stack with the appropriate addresses at the point in program where the application crashes. In your case you would have to find a pointer variable with the value contained in the error.

The debugger should also display the line where the error occured. It might be easier to find the error by looking at the code arround this line than searching for the offending variable by content.

Without symbol information included in the program it is impossible to match memory locations to variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM