Problem with input whenever debugging C++ (Xcode 10.1) - c++

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;

Related

Debugger problem regarding to how to see all the value in the dynamic array in c++

I need to understand the other people's code by debugging through all the code, in the meantime, I can't change or add any code. The one major problem comes up. That is When I want to see the all the value in a dynamic array by adding it to Watch, it would not give me what I expect to. I am only able to see first value of the them. Such as:
In addition, when I add it to Watch, it would show no operator "[]" matches these operands.
How can i address this problem?
you can follow this link, it shows you how to use feature called the “size specifier”to display the array elements:
https://blogs.msdn.microsoft.com/habibh/2009/06/05/size-specifier-how-to-display-a-cc-pointer-as-an-array-in-the-visual-studio-debugger/

Debugging with gdb - (gdb) "x/s pointer" output

I am trying to debug using gdb. I got that if you want output in string you have to use "x/s Ptr". It works fine some time. But many times I am getting either Null value i.e. " " or some random numeric values. My file has 10000 lines of codes. :-p Please find some gdb output. For e.g.
krb5_get_credentials_for_user (context=0x59c00eb0, options=4, ccache=0x5a001d40, in_creds=0x5ab022a8, subject_cert=0x0,
out_creds=0x5ab02378) at test_abc.c:696
(gdb) x/s 0x59c00eb0
0x59c00eb0: "$\247\016\227"
(gdb) x/s 0x5ab022a8
0x5ab022a8: ""
Could someone please tell me how I can solve this prob? Thanks in advance!
But many times I am getting either Null value i.e. " " or some random numeric values.
There is nothing wrong with what you show. It's just that the memory location you are examining isn't pointing to a string (0x59c00eb0) or is pointing to an empty string (0x5ab022a8).
You didn't present any evidence that these locations should be pointing to a string, and in fact, as (now deleted) comment showed context points to struct _krb5_context, which contains magic number as the first member. Therefore, you should use x/w to examine it.
... fine some times. But many times I am getting either Null value i.e. " "
or some random numeric...
Been there, done that. Allow me to encourage you to be creative.
I sometimes create a function (call it foo? bar? show? dump?), that is not used by the program being debugged. The function is often c-style (because gdb seems to understand that better, and simpler to invoke), global scope, simple. The temporary install of this function close (in the same file?) to what you want to improve the visibility of sometimes helps.
I can then invoke this function using the gdb p command, such as
gdb> p foo
it is possible to pass parameters to foo, but if I'm touching the code to debug something, I usually make foo more capable ... when no parameters it does one thing. Or perhaps use an int parameter (bar(7)) that switches to show more or differently.
Experiment.
This is not typical, and I suspect better knowledge of gdb might be worth the effort, if I could remember it to the next time I need it. Sometimes gdb just doesn't understand, and I can't figure out why. Other times, I get away with adding a pointer and trying to print that:
gdb> p *foobar

String array unusable after setting it to 0 using memset

I have a class property which is an array of strings (std::string command[10]). When I assign some string value to it, it stop the program execution. As you can see below I've a string variable tempCommandStr which I assign to my property. I don't know what the error could be, but I've the print statement after assignment which is never executed, while the one preceding it is.
//Declared in class header
std::string command[10];
// Part of function which is causing problem.
string tempCommandStr(commandCharArray);
printf("%s\n", tempCommandStr.c_str()); // Prints fine.
this->command[i] = tempCommandStr; // Something goes wrong here. i is set to some correct value, i.e. not out of range.
printf("%s\n", this->command[i].c_str()); // Never prints. Also program stops responding.
// I noticed that getting any value from the array also stops the execution.
// Just the following statement would stop the program too.
printf("%s\n", this->command[i].c_str());
It's not just this property, I also have another array which has the same problem. What could be causing this? What's actually going wrong (look at edit)? Is there another better way to do this?
I'm running the program on an MBED so I've limited debugging options.
EDIT:
I found the problem, I was cleaning the array before using to remove any previous values by memset(command, 0, sizeof(command));. This is was causing the problem. Now I use the clear function on each item in array as following. This fixed the execution problem.
for (int i = 0; i < sizeof(command)/sizeof(*command); i++){
command[i].clear();
}
Question: Why does setting the string array to 0 using memset makes it unusable?
Why does setting the string array to 0 using memset makes it unusable?
Because you're obliterating the values held in the string class, overwriting them all with 0s. A std::string has pointers to the memory where it's storing the string, character count information, etc. If you memset() all that to 0, it's not going to work.
You're coming from the wrong default position. Types where 'zeroing out' the memory is a meaningful (or even useful) operation are special; you should not expect any good to come from doing such a thing unless the type was specifically designed to work with that.

How do I know when a variable is accessed within my code?

I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.

Remember my choice for display as array in Eclipse C/C++

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.