I just started using VSCode to program in C/C++.
Sometimes (e.g. after deleting a line break) I am in the following situation, where | represents the cursor:
x = 5;| y = 10;
If I press enter, then it becomes
x = 5;
| y = 10;
but I want the line y = 10; to be automatically indented to the correct place, namely
x = 5;
y = 10;
This is the expected behavior in Visual Studio, which I can't get in VSCode.
In the setting, Editor: Auto Indent is set to full. I also enabled Editor: Format On Type, if that matters.
How can I get the expected result?
Related
constexpr auto CHUNKS_X = 5, CHUNKS_Y = 5, CHUNKS_Z = 1;
std::array<std::bitset<CHUNKS_X>, CHUNKS_Y> ys;
std::array<decltype(ys), CHUNKS_Z> zs;
if (CHUNKS_Z > 1)
{
zs[0] = ys;
//zs.at(1) = ys; //this works
zs[1] = ys; //this doesn't work
for (auto &x : zs[1])
{
x.flip();
}
for (auto z = 2; z < CHUNKS_Z; z++)
{
zs[z] = zs[z - 2];
}
}
The line zs[1] = ys; gives me
error C4789: buffer 'zs' of size 20 bytes will be overrun; 20 bytes will be written starting at offset 20
But only when compiling in VS. Compiling on the command line gives me no such error, nor does using zs.at(1) = ys; instead. Another thing of note is that MSDN says that this should be a warning, not an error.
I realize this might be a subtle compiler flag issue but I haven't the slightest clue where to start looking.
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
std::array<decltype(ys), CHUNKS_Z> zs;
is equivalent to
std::array<decltype(ys), 1> zs;
meaning zs is an array with 1 element;
arrays in general are zero-based meaning the first element is at zs[0], and there are no more elements so zs[1] requires that the arrays will have a second element which it doesn't in your example.
Since you check if (CHUNKS_Z > 1) there should not be any problem since you can't reach the line zs[1] = ys; and you won't get any issues.
If I change CHUNKS_Z to 2 I don't get any errors in VS15
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
In Python, you can easily exchange the values of 2 variables with an expression like this:
x, y = y, x
In C++, on the other hand, if you want to exchange the values of 2 variables, you usually use a temporary variable to store the value of one of your variables, something like this:
int var1 = 100;
int var2 = 200;
int temp = var1;
var1 = var2;
var2 = temp;
This is easy, but you have to write a lot of code.
A professor that I have been following for his lectures on C++ has discovered a new compact way of exchange the values of 2 variables in a magic way:
int x = 200;
int y = 100;
x = y - x + (y = x);
It seems incredible, but it works both with the compiler he's using and mine Apple LLVM version 6.0 (clang-600.0.56).
It seems that the way that expression is interpreted is the following:
(y = x) // returns the value of x
-x + x = 0
x = y + 0
x = y
y = x // finally, y receives the initial value of x
If I try to exchange the values of some variables in a loop, it seems also to work:
for (int i = -10; i <= 10; i++) {
for (int j = 10; j >= -10; j--) {
int x = i, y = j;
x = y - x + (y = x);
std::cout << "x = " << x << "\ny = " << y << '\n';
}
}
We have seen that the exchange is done, but my compiler gives me this warning:
main.cpp:28:22: warning: unsequenced modification and access to 'y'
[-Wunsequenced]
If there's a warning, I suppose this is not a new standard way of exchanging the values of 2 variables, but was just an ingenious workaround of this professor.
Why this method is not standardised in C++, and why exactly does it work?
This is undefined behavior, and it's not guaranteed to work. The order of evaluation of the parameters in an expression is not specified. So it's allowed to access the value of y for the subtraction before or after it performs the y = x assignment.
The reason it's specified like this is to allow flexibility for optimizations.
It "works" because you were unlucky.
Yes, I said unlucky. The code has undefined behavior; the fact that it appears to "work" means that your compiler has let you get away with code that could fail next time you run it.
x = y - x + (y = x);
The object y is accessed twice, once to read its value and once to assign a value to it. The language does not define the order in which those accesses occur -- or that the occur in any order at all. As the warning message says, they're "unsequenced". The possible behaviors are not limited to the two possible orders; the behavior is entirely undefined.
Furthermore, the addition and/or subtraction could overflow, which is another potential source of undefined behavior.
This, `x = y - x + (y = x);, is undefined behavior in C - one undefined behavior listed by the C99 spec is:
Between two sequence points, an object is modified more than once, or
is modified and the prior value is read other than to determine the
value to be stored
The only sequence points in this expression are the beginning of the expression and the end of the complete expression.
I am using Visual studio 2010. I want to know is there any option in the VS debugger to step into an expression.
Example:
int a = 5, b = 255, c = 10;
int result;
result = a + b*c + a&b ;
To understand the operator precedence, I want to know which expression in the result = a + b*c + a&b ; is executed first and which expression next.
Is there any option to do that?
Thanks.
try to put open and close parenthesis. it will execute first
result = (a + (b*c)) + a&b ;
I am using Microsoft visual C++ express 2010
i have a variables:
int x, that represents the position of a video game character. (their is a Y of course)
The program loops and each time it changes X by a couple of places. but it must be within 0-800. and when it reaches the 0 (which is supposed to be the edge of the screen) its rewinds.
I have figured out how to change their value every time the program runs, but how do i make sure that it keeps its value in the 0-800 range, and rewind it when it reaches position 0?
and it has its very own function outside of Main entirely.
thank you.
x = (x + 800) % 800;
This will keep x within (0..799). If you really need (0..800), replace 800 with 801.
Make a direction variable...
int dir = -2;
for(;;) {
x += dir;
if( x < 0 || x >= 800 ) {
dir *= -1;
x += dir;
}
}
First, it's not quite clear exactly what you want. When you say "rewind", do you mean start over at the opposite side again, or turn around and move back in the direction it came from.
Assuming the first, the easy (but somewhat clumsy) way is to just do a comparison and when/if the value goes out of range, adjust as necessary:
x -= increment;
if (x < 0)
x = 800;
or:
x += increment;
if (x > 800)
x = 0;
You can also use the remainder operator, but it can be a little bit clumsy to get it entirely correct. When you're going in the positive direction, it's fairly direct and simple, but in the negative direction, it's not -- in this case a negative number is entirely possible, so simple tests like above are needed. If the value only ever goes in the positive direction, so you only care about it becoming greater than the limit, it works fine though.
I have the following code, m_edit is a MFC CEdit (I know I would never use MFC but project demanded it).
It's a simple loop, that gets the text from a text edit, converts it to integer after getting the first line, then stores it in m_y vector.
LPTSTR szTemp;
vector<int> m_y;
for(int i = 0; i < m_edit->GetLineCount(); i++){
szTemp = s_y.GetBuffer(0);
m_edit->GetLine(i, szTemp); // get line text store in szTemp
y = atoi(szTemp);
m_y.push_back(y);
szTemp = "";
y = 0;
}
IMPORTANT EXAMPLE: So let's say the CEdit has 6 numbers:
0
5
2
5
18
6
If you use Visual Studio's debugger you will notice an anomaly!!
Here's what it shows:
y = 0
y = 5
y = 2
y = 5
y = 18
y = 68
Do you see that? szTemp when inserted into atoi, it returns the number 6, but concatenates the 2nd digit of the last number!!! This is why I did szTemp = "";, but the problem persists. Also, let's say the last number was 17 (not 18), then this time debugger would say y = 67, so it is definitely this problem.
However, Visual Studio debugger, when you hover over szTemp during this iteration, it says '6' <--- not '68' inside szTemp. So somehow atoi is ruining it.
Am I suppose to concatenate a \0 into szTemp before putting it into atoi? How do I solve this easily?
From the MFC CEdit::GetLine documentation:
Remarks:
The copied line does not contain a null-termination character.
So you need to pay attention to GetLine's return value to determine how many bytes were copied into the buffer and then add your own NUL-terminator.
Also, I would recommend that you pass in the buffer size to make sure you don't have a potential buffer overflow.
I think you should specify a buffer size for GetBuffer
e.g.
szTemp = s_y.GetBuffer(MY_MAX_STRLEN);
having GetBuffer(0) allocates a 0 sized buffer since CString is (presumably) empty.