Visual Studio Time saver options? - c++

I was wondering how to customize my visual studio to code faster. I'm begginer, but saw few awesome things like autofilling for's or auto brackets when someone typed if/for/while etc.
Ex. When I'll type for it will fill it similar to this :
for(int i = size_T ; i< lengh ; i++)
{
}

Related

codeblocks auto-indent suddenly stops working (Windows 10)

All of a sudden, my codeblocks auto-indentation has stopped properly working.
This affects all previous programs, new programs - it is not code-specific.
I have tried all of the indentation settings on and off and in all sorts of combinations.
The actual error is that curly braces all line up with the beginning of the most indented code.
anyFunction()
{
variables;
for(int i(0); j<variable; i++){
for(int j(0); j < 100; j++){
code;
}
}
}
THIS IS NOT A MISSING ; OR ANY CODE-RELATED ISSUE.
This error now happens in all codes.
Again, this is strange because it happened in the middle of coding, and I wasn't in any kind of settings when it did happen.
Anybody have any ideas?

unordered_set Visual Studio C++ Slow

So I think there was a bad version of C++ where unordered_set was incredibly slow. This piece of code took one minute to execute:
int main() {
unordered_set<int> blah;
for (int i = 0; i < 1000000; ++i)
{
blah.insert(i);
}
cout << "done 2" << endl;
return 0;
}
It took about 40 seconds to get to the output statement, then took another 20 seconds to deallocate the object. It's C# counterpart with 10 times the insertions executes in about a second:
static void Main(string[] args)
{
HashSet<int> set = new HashSet<int>();
for (int i = 0; i < 10000000; ++i)
{
set.Add(i);
}
}
This caused my FacebookHackerCup solution to not run in time :(. How can this be fixed for someone using the visual studio C++ IDE? I don't know what version it runs via command line or how to upgrade it.
Run without debugging
Need to compile in Release mode
Potentially need to turn on optimizations by going to project properties -> Configuration Properties -> C/C++ -> Optimization -> Maximize Speed (/O2). Mine was already set for Release, not for Debug.
This makes the original code run in about a second, and the 10,000,000 variant to run in about 8 seconds (so still about ~4 times slower than C# in Debug mode with debugging). EDIT: It seems C#'s speed does not greatly change with or without debugging or building in Release mode for this particular program.

How to auto-vectorize range-based for loops?

A similar question was posted on SO for g++ that was rather vague, so I thought I'd post a specific example for VC++12 / VS2013 to which we can hopefully get an answer.
cross-link:
g++ , range based for and vectorization
MSDN gives the following as an example of a loop that can be vectorized:
for (int i=0; i<1000; ++i)
{
A[i] = A[i] + 1;
}
(http://msdn.microsoft.com/en-us/library/vstudio/jj658585.aspx)
Here is my version of a range-based analogue to the above, a c-style monstrosity, and a similar loop using std::for_each. I compiled with the /Qvec-report:2 flag and added the compiler messages as comments:
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec(1000, 1);
// simple range-based for loop
{
for (int& elem : vec)
{
elem = elem + 1;
}
} // info C5002 : loop not vectorized due to reason '1304'
// c-style iteration
{
int * begin = vec.data();
int * end = begin + vec.size();
for (int* it = begin; it != end; ++it)
{
*it = *it + 1;
}
} // info C5001: loop vectorized
// for_each iteration
{
std::for_each(vec.begin(), vec.end(), [](int& elem)
{
elem = elem + 1;
});
} // (no compiler message provided)
return 0;
}
Only the c-style loop gets vectorized. Reason 1304 is as follows as per the MSDN docs:
1304: Loop includes assignments that are of different sizes.
It gives the following as an example of code that would trigger a 1304 message:
void code_1304(int *A, short *B)
{
// Code 1304 is emitted when the compiler detects
// different sized statements in the loop body.
// In this case, there is an 32-bit statement and a
// 16-bit statement.
// In cases like this consider splitting the loop into loops to
// maximize vector register utilization.
for (int i=0; i<1000; ++i)
{
A[i] = A[i] + 1;
B[i] = B[i] + 1;
}
}
I'm no expert but I can't see the relationship. Is this just buggy reporting? I've noticed that none of my range-based loops are getting vectorized in my actual program. What gives?
(In case this is buggy behavior I'm running VS2013 Professional Version 12.0.21005.1 REL)
EDIT: Bug report posted: https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized
Posted bug report here:
https://connect.microsoft.com/VisualStudio/feedback/details/807826/range-based-for-loops-are-not-vectorized
Response:
Hi, thanks for the report.
Vectorizing range-based-for-loop-y code is something we are actively
making better. We'll address vectorizing this, plus enabling
auto-vectorization for other C++ language & library features in future
releases of the compiler.
The emission of reason code 1304 (on x64) and reason code 1301 (on
x86) are artifacts of compiler internals. The details of that, for
this particular code, is not important.
Thanks for the report! I am closing this MSConnect item. Feel free to
respond if you need anything else.
Eric Brumer Microsoft Visual C++ Team

C++, ASM, and cout

I'm using VC++ to disassemble a very simple program I've written:
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i < 11; i++)
{
cout << i << endl;
}
return 0;
}
I was hoping to shed some light on how cout works, but upon inspection, the resulting ASM points to an external source (I assume):
EXTRN __imp_?cout#std##3V?$basic_ostream#DU?$char_traits#D#std###1#A
Is there a way to identify, from the above line, where specifically this points to, and how to access it? Even still, how to read the above line?
You don't have to disassemble that. The MS sources of the streams are part of Visual Studio installation. See: "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src"
cout is provided by the C++ runtime. In the case of Visual C++ that would be MSVCPxxxx.dll (xxxx depending on the version and debug/release).
You can lookup that stuff by using something like "CFF Explorer" or "depedency walker" and look at the program import directory.

Segmentation fault in neural network

I have been writing a code for a neural network using back propagation algorithm and for propagating inputs I have written the following code,but just for two inputs,its displaying segmentation fault.Is there any wrong withe code.I wan not able to figure it out....
void propagateInput(int cur,int next)
{
cout<<"propagating input"<<cur<<" "<<next<<endl;
cout<<"Number of nerons : "<<neuronsInLayer[cur]<<" "<<neuronsInLayer[next]<<endl;
for(int i = 0;i < neuronsInLayer[next];i++)
{
neuron[next][i].output = 0;
for(int j = 0;j < neuronsInLayer[cur];j++)
{
cout<<neuron[cur][j].output<<" ";
cout<<neuron[next][i].weight[j]<<"\n";
neuron[next][i].output += neuron[next][i].weight[j] * neuron[cur][j].output;
}
cout<<"out["<<i<<"] = "<<neuron[next][i].output<<endl;
}
cout<<"completed propagating input.\n";
}
for(int i = 0;i < neuronsInLayer[next];i++)...
neuronsInLayer[next] is a pointer. perhaps if i knew the type of neuronsInLayer i could assist you more.
That is not anywhere near enough information to debug your code. No info about line numbers or how the structures are laid out in memory or which ones are valid, etc.
So let me tell you how you can find this yourself. If you're using a Unix/Mac then use the GDB debugger on your executable, a.out:
$ gdb a.out
> run
*segfault*
> where
Visual Studio has a great debugger as well, just run it in Debug mode and it'll tell you where the segfault is and let you inspect memory.