I'm working in a personal project of open-source technologies developing an application build it in C. I'm using the lldb debugger tool.
My question is simple: How can I display or show the values of an element when I'm debugging.
For example:
#include <iostream.h>
int main(){
char phrase[1024];
int i=0;
for(i=0;i<1024;i++){
printf("%c",phrase[i]);
}
return 0;
}
In the lldb prompt, I can see the values for specific character of the array:
lldb>b 6
lldb>frame variable phrase[0];
When I want to execute:
lldb>frame variable phrase[i]
I got an error: "unable to find any variable expression path that matches 'phrase[i]'"
You need to use
(lldb) expr phrase[i]
or equivalently
(lldb) p phrase[i]
for that
frame variable supports constant indexes (i.e. plain ol’ numbers), but if you need to use a variable or anything BUT a number, you need to use the expression command
As a caveat, the behavior of frame var vs. expression might be different in some cases when doing array-like access. This won’t affect your example (but it would if you were using an std::vector, for instance).
Related
I am newbie to Xcode. I try to create a first problem with C++ (image below) and set some breakpoints to test debugging. I get a problem with input in console window. Particularly, whenever I get input line, I type number and there is nothing i can see in console until i change between target output and all output (i.e. if i am on "target output" and type anything, i have to change into "all output" to see the number i typed, vice versa). And there is one more problem: I can delete the number i typed, i.e. if i type 3, it only allow me to add postfix number such as 31 or anything like it. Cannot change the number.
Does anyone know this problem? Please help me. Thank a lot.
This is my code
in the old way you were allocating an empty array you should give its size which is 'n' here
int *a=new int(n);
your old code had some undefined behavior due to the out of bound access you were trying to do accessing without allocating
at the end of your code you should deallocate the dynamic allocated memory like this :
delete[] a;
Is there a way to take a string as an input argument to a c++ function and evaluate it as an internal argument e.g. the name of a structure or other variable?
For example (written in pseudo code)
int myFunction(string nameStructure){
nameStructure.field = 1234
}
The "take away" point is converting the input string as a variable within the code.
Mark
This type of question is often a symptom of a XY problem so consider other options first. That being said, there's no such default mechanism in C++ but there is a simple workaround I can think of - use a dictionary (std::map / std::unordered_map) to store all your objects:
std::map<std::string, MyAwesomeObject> objects;
...
int myFunction(std::string nameStructure)
{
objects[nameStructure].field = 1234
}
The names of local variables are just artifacts of the human-readable code and have no meaning in the compiled binary. Your int myIntVar's and char* myCharP's get turned into instructions like "four bytes starting at the location of the base pointer minus eight bytes, interpreted as a four-byte integer". They no longer have names as such.
If you export symbols from your binary, you can at runtime to look into export table according to your binary format and find the variable you want. But i bet you want something like access to local variable and that is not possible.
If you really need this funcionality, take a look at more dynamic interpreted languages as php
http://php.net/manual/en/language.variables.variable.php
I'm trying to get value from .cc. In my file ,
AgentCPU.h and AgentCPU.cc
there is a integer called "npkts_" and a function "recv" for receive Packets
when I finally finished a packet , I will increase the "npkts_" 1
In further , in my tcl script I want to access the "npkts_"
my code is like this
set cpu [new Agent\AgentCPU]
puts [format "%d" "$cpu set npkts_"]
However , ther value is not correct
it is same to the value when I construct my AgentCPU like this
AgentCPU::AgentCPU(): Agent(PT_TASK)
{
...
npkts_=199;
...}
the value will be 199,
and in the "recv" function , I use "printf" to check if there is any problem
...
npkts_++;
printf("npkts is %d\n",npkts);
...
and the value here is correct,every time I receive Packet will increase the "npkts"
Is there any code wrong??
On the other hand, I use another way to debug
In my "recv" function
...
npkts_++;
Tcl& tcl = Tcl:;instance();
tcl.evalf("puts \" npkts is %d""",npkts_);
..
In this way the message will be 1, and stop to print
Can sombody give me a hand?
How to get the correct value from .cc file?
Any suggestion will be very thankful!!
In Tcl, every script evaluation produces a result value (which could be the empty value, or could be something else, depending on what happened). That result value may be retrieved from the interpreter context with Tcl_GetStringResult or Tcl_GetObjResult; the former produces (effectively) a const char * and the latter a Tcl_Obj * (i.e., a Tcl value reference; the name Tcl_Obj is for historical reasons). That value is there until the next piece of script is evaluated. Do not modify the Tcl_Obj* returned (except via Tcl's API) unless you know exactly what you're doing (i.e., are writing code to extend Tcl itself). Integers may be efficiently retrieved from a Tcl_Obj* using Tcl_GetIntFromObj (assuming that the value is an integer), floating point numbers with Tcl_GetDoubleFromObj, and a const char * can be retrieved from a Tcl_Obj* using Tcl_GetString or Tcl_GetStringFromObj (slightly different API).
There are also variables in Tcl. You can read those directly with Tcl_GetVar — which returns another const char * — or Tcl_GetVar2Ex (with an ugly name but convenient API) that returns another Tcl_Obj *.
These are all C API functions and all take a Tcl_Interp * (which is a reference to a Tcl evaluation context, which is always bound to a single thread). How they have been wrapped in the C++ API… I can't tell you that.
You are using some weird C++ library you are not giving any information about. However, when using Tcl from a C/C++ program when you evaluate some Tcl code the result is available attached to the interpreter object. You access this using Tcl_GetObjResult and then use an appropriate Tcl_GetIntFromObj or Tcl_GetDoubleFromObj or whatever to read the Tcl_Obj value into some C++ form.
In debug mode of Eclipse for C/C++, you can right-click on any watched expression that is a pointer and select "display as array". Then you're prompted for the array bounds and for the rest of this debug run, the watched expression is being displayed as an array according to these bounds.
When I terminate the process and start debugging again, it remembers my watched expressions, but pointers that were previously displayed as arrays will now be mere pointers again and hence, I have to re-cast all pointers in each debug run. In a recent project, this has become very tiresome.
Is there a way to make Eclipse remember the "display as array" choices for watched expressions?
You have to be able to encode the fact that you want to look at the pointer as an array in the expression string itself.
Say you have an array as int* and you want to look at (most at) its first 4 elements.
In the Expressions tab, use one of the two following syntaxes supported by GDB:
(*arr # 4)
((int[4])*arr)
The surrounding (...) parens above are important.
You can do this in the Expressions tabs (watches), but not in the Variables tab.
I need to append a number at the end of the word flare depending on how many I have. That part works great. My problem is when I go to print it to the screen. I would like the program to output the value of what (Camera::Table[Id]->sensor->flare1) sensor is pointing at, in this case, flare one. If the program were to continue it would output the value pointing at flare2, flare3, flare4,... until the limit is reached.
What I get as the output is the following:
lens_pos1=¨Camera::Table[Id]->sensor->flare1¨
lens_pos2=¨Camera::Table[Id]->sensor->flare2¨ .......
How can I output the value of flare1 instead of pasting the string?
What I want is the following:
lens_pos1=¨10.3¨ lens_pos2=¨12.4¨.....
Where the values 10.3, 12.4 would be those of flare1 and flare2 respectively taken from a seperate C file.
for(int i = 1; i <= nbflares; i++)
{
char integer_string[32];
sprintf(integer_string, "%d", i);
char flare[100] = "Camera::Table[Id]->sensor->flare";
strcat(flare,integer_string);
fprintf(yyout, "lens_pos%d=\"%s\" ",i,flare);
}
You can't access a variable like that in C/C++. You have to redesign the "sensor" structure to contain an array instead of individual flares, and access the array by index: Camera::Table[Id]->sensor->flare[1], Camera::Table[Id]->sensor->flare[2], etc.
That can't be done in C++. Some interpreted languages might allow such things because the text of the source code still exists while the program is running. But in C++, when you compile a program all the names of classes and variables and such are essentially lost. When it gets to the point of a running executable, the actual machine instructions are just working with offsets and memory addresses.
So you need re-design how the data is stored.
Is there a way to access them without the use of arrays?
Technically, yes. But only by using a more complicated scheme that would involve a more complicated data structure (such as a linked list or map).
Why would you want to avoid arrays anyway? Right now you have a series of variables of the same type that you want to distinguish by the number are the end of their names. And an array is a series of variables of the same type that are distinguished by their index in the array. It's pretty much a perfect match.
For example, if you had a flares array, then you could simply do something like:
for(int i = 0; i < nbflares; i++)
{
fprintf(yyout, "lens_pos%d=\"%f\" ", i, Camera::Table[Id]->sensor->flares[i]);
}