ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If I compile a simple "int main(int argc, char *argv[])" program with -g, and debug with gdb, I'm getting an error when I break at the start and try to print argc or argv.
Cannot access memory at address 0x0
This is with SUSE 10.1, which is gcc 4.1.0. I happen to have some binaries of the *same* source that were compiled with gcc from 10.0 (which is gcc 4.0.2 I think) and these debug correctly.
The programs themselves work fine. It's just the debugger that has a problem.
Is this a known problem?
To reproduce, write the classic "hello world" main, compile with "gcc -o main -g main.c" or "g++ -o main -g main.cpp" (C and C++ give the same problem). Then "gdb main", "break main" and run to the breakpoint. "p argc" or "p argv" gives the error. If you (can) repeat this under SUSE 10.0 it may not show the problem.
For me if the program doesn't uses argc/argv the values are optimized out (even if specifying O0). Is this your problem also ?
Ah! I didn't think of that. It would mean it's something that's changed recently (my 10.0 builds are OK), but it would explain it very neatly. I'll try later tonight and let you know.
Are all function arguments that aren't used removed from the debug symbol list, or is it just main's?
I only noticed it because the Eclipse C++ debug environment gives very nasty looking errors when it can't read the variables.
If you use argc/argv, then their values are reports as normal. Curiously, you have to actually step into main before they can be read - ie. when the program stops at the "break main" you can't see them, but after a "next" you can. It only applies to main; other functions show their parameters, even if they're not used in the body.
This almost fixes the problem with Eclipse. It still reports errors when you break at main, but it will let you view the pair once you've stepped into main. It also cures a problem where breakpoints in main weren't effective - my guess is the errors trying to read the locals confused it so much it couldn't successfully set the breakpoint either.
I'm told this doesn't happen with the 64-bit compiler in SUSE 10.1, incidentally.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.