Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alexandre16

macrumors member
Original poster
Jan 21, 2011
72
0
Hi developers.

I am using xcode to build some simples command line programs, and now i want to check where is the value that i insert to a variable, i am writing in C language.

Exemple:

scanf("%d", &Var);

well, i run the program and insert the value, then i pause the program executation and go to Product -> Debug -> View memory and then i dont know what address i need to choose to see my value of the variable, anyone can give me some help with that?

Thanks people.
 
You should just set a breakpoint at that line, step over it, and in the debugger you will see the new value for 'Var'.

You could obviously printf out the variable, and its address (&Var) too.
 
You should just set a breakpoint at that line, step over it, and in the debugger you will see the new value for 'Var'.

You could obviously printf out the variable, and its address (&Var) too.

Yes i am using the breakpoint but my problem is to find the right address of the variable, because i only want to know in the memory, where is the value of my variable, and i will see it using the address, and then put this address in Product -> Debug -> View memory, i put the code and see some hexadecimals codes and the correspondent value of this hexadecimal codes, but i dont see the value that i put in my variable...maybe because the address that i use isnt the right address.

How can i get the right address of my variable?

Thanks for the answer
 
Is your variable on the variable list? If so, select it. Right click. One of the options is View Memory of "Var". It will bring up the memory viewer at the correct location.

Otherwise, you can print it out with printf.

Code:
printf("%p\n", &Var);

Put the value into the memory viewer.
 
Is your variable on the variable list? If so, select it. Right click. One of the options is View Memory of "Var". It will bring up the memory viewer at the correct location.

Otherwise, you can print it out with printf.

Code:
printf("%p\n", &Var);

Put the value into the memory viewer.

it is the problem, my var isnt in the list...

i am using this program...


Code:
#include <stdio.h>

int seconds_from_time(int h, int m, int d)
{
    return( h * 3600 + m * 60 + d);
}

void exercise_1_1 (void)
{
    int h;
    int m;
    int d;
    while (scanf ("%d%d%d", &h, &m, &d) != EOF)
    {
        int z = seconds_from_time (h, m, d);
        printf ("%d\n", z);
    }
}

int main()
{
    exercise_1_1();
    return 0;
}
 
Last edited by a moderator:
Yes i am using the breakpoint but my problem is to find the right address of the variable, because i only want to know in the memory, where is the value of my variable, and i will see it using the address, and then put this address in Product -> Debug -> View memory, i put the code and see some hexadecimals codes and the correspondent value of this hexadecimal codes, but i dont see the value that i put in my variable...maybe because the address that i use isnt the right address.

How can i get the right address of my variable?

Thanks for the answer

You are trying to make this very, very painful for yourself. Trying to use "View memory" to see the contents of a variable is just daft. Set a breakpoint where the variable is read. Run the program. When Xcode stops at the breakpoint, there is a view at the bottom of the Xcode window showing you all the variables of that function with their values.

"Pause" isn't very helpful because by the time you press "Pause" your program has been running on executing about 2 billion instructions per second, so you can probably see on the left side in the call stack that it is in the middle of the scanf function, waiting for your input. Use a breakpoint.
 
Which versions of Mac OS and Xcode are you running?

I am using the last version of OSX and Xcode.

i am using the lldb debugger.

i am using the breakpoint at the line of scanf, using the code that i posted above, when i run the the code and it stop at the breakpoint i have 3 things in the debugger navigation, exercise_1_1, main and start.

What i am doing wrong?
 
That's the "stack trace" pane off to the left side of the "source" pane.

So that's a big "no" on my question.

At the top right of the window above the "source" pane is a collection of buttons. One of which is called "View". Select the center button. A "debug" pane should appear "BELOW" your projects source.

All the above is accurate assuming you haven't done any custom setup.
 
To find the address of variable foo, you can just type:

Code:
print &foo
in Xcode's debug console, after hitting some breakpoint or stopping the app with that variable in scope. Then you can use:

Code:
me r -b <start_address> <end_address>
to dump out hex bytes at that (or any other nearby) address.
 
I am using the last version of OSX and Xcode.

i am using the lldb debugger.

i am using the breakpoint at the line of scanf, using the code that i posted above, when i run the the code and it stop at the breakpoint i have 3 things in the debugger navigation, exercise_1_1, main and start.

What i am doing wrong?

What you are doing wrong is that you haven't learned what all the little clickety icons on the screen are there for and what all the menu items are doing.

Can you tell us what the "Show Toolbar" or "Hide Toolbar" menu does? That's the first step. Next, what do the three icons with "View" under them do? That's the next step. What do the three little icons to the right of the "Clear" button do?
 
What you are doing wrong is that you haven't learned what all the little clickety icons on the screen are there for and what all the menu items are doing.

Can you tell us what the "Show Toolbar" or "Hide Toolbar" menu does? That's the first step. Next, what do the three icons with "View" under them do? That's the next step. What do the three little icons to the right of the "Clear" button do?

i am not an expert in xcode because i start using it maybe 1 month ago, but until this problem everything is ok, anyone can talk with me by skype? it will resolve the problem in 1 minute because i can show you some pictures....

Thanks
 
Why Skype when there a native solutions already a part of your "last version of OSX" - Messages.

What you haven't clearly made apparent yet is if you've got it figured out yet or not!
 
Why Skype when there a native solutions already a part of your "last version of OSX" - Messages.

What you haven't clearly made apparent yet is if you've got it figured out yet or not!

Yeh i can use messages, anyone can help?
 
About printing the variable address, other than printf you can do it with gdb: "print &Var".
 
Are we both speaking english or is it just me.

I see your variables "below" the "source pane" in that image.

Perhaps you could say explicitly what it is you need to see that isn't in that image.

If you're looking for the addresses of 'argc', 'argv' and 'retVal' then 'argc' and 'argv' will most likely be in 'registers', which don't have an address and 'retVal' may've been mapped directly to the 'register' in which function results are returned bypassing the stack altogether.
 
Are we both speaking english or is it just me.

I see your variables "below" the "source pane" in that image.

Perhaps you could say explicitly what it is you need to see that isn't in that image.

If you're looking for the addresses of 'argc', 'argv' and 'retVal' then 'argc' and 'argv' will most likely be in 'registers', which don't have an address and 'retVal' may've been mapped directly to the 'register' in which function results are returned bypassing the stack altogether.

this image is not mine, i said if i have this image it will be easy to find the values of variables in the stack strace, but i dont have this.

I posted a simple code above, and i want to share a simple image to show you, what i have in my xcode.

Can i send you a image via email, or other way?
 
The picture helps.

In the "debug pane" you're only displaying the "console".

See the "clear" button in the "debug pane"? Their's a group of three buttons next to it. Press the center button and your variables sub pane will display what you're wanting to see.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.