Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 14, 2012, 10:02 AM   #1
alexandre16
macrumors member
 
Join Date: Jan 2011
Check in xcode where is the value of a variable in memory

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.
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 10:17 AM   #2
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
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.
jared_kipe is offline   0 Reply With Quote
Old Nov 14, 2012, 02:51 PM   #3
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by jared_kipe View Post
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
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 03:32 PM   #4
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: San Diego, CA USA
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.
mfram is offline   0 Reply With Quote
Old Nov 14, 2012, 04:09 PM   #5
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by mfram View Post
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 grapes911; Nov 14, 2012 at 06:26 PM. Reason: added code tags to make it more readable
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 04:15 PM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by alexandre16 View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Nov 14, 2012, 04:21 PM   #7
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Which versions of Mac OS and Xcode are you running?
lloyddean is offline   0 Reply With Quote
Old Nov 14, 2012, 04:37 PM   #8
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
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?
alexandre16 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to check log files to date/time system RAM was changed? Any way to check this? asterix2k10 OS X 1 Mar 7, 2014 08:19 PM
How to check video memory usage ngeil OS X 10.8 Mountain Lion 2 Sep 12, 2013 12:45 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Variable into Method how ? mngoose iPhone/iPad Programming 9 Oct 10, 2012 03:59 PM
The I may go to bed, and just check back in tomorrow to check my order status thread lsutigerfan1976 iPhone 0 Sep 14, 2012 10:50 PM

Forum Jump

All times are GMT -5. The time now is 08:21 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC