variable-declaration

Does moving variable declaration outside of a loop actually increase performance?

白昼怎懂夜的黑 提交于 2019-11-27 06:56:07
问题 I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains, no matter how small. I've heard opinions both ways on this subject. Is there any performance benefit at all to int smallPrime, spGen; for (int i = 0; i < numSmallPrimes; i++) { smallPrime = smallPrimes[i]; spGen = spHexGen[i]; [...] } over this? for (int i = 0; i < numSmallPrimes; i++) { int smallPrime = smallPrimes[i]; int spGen = spHexGen[i]; [...] } Does the compiler do this already? 回答1:

Declaring vs Initializing a variable?

♀尐吖头ヾ 提交于 2019-11-27 06:01:52
问题 I'm curious to know the difference between declaring a variable, and initializing a variable. e.g. var example; // this is declaring var example = "hi" // initializing? Or just "adding a value"? I don't think I'm right there, but what exactly is the definition of each? Or do they basically mean the same thing? 回答1: Edit: @ThisClark said something in the comments, and I went to prove him wrong, and upon reading the spec some more I learned something: Here's an informative except from the

dynamically declare/create lists in python [closed]

夙愿已清 提交于 2019-11-27 05:53:21
问题 I am a beginner in python and met with a requirement to declare/create some lists dynamically for in python script. I need something like to create 4 list objects like depth_1,depth_2,depth_3,depth_4 on giving an input of 4.Like for (i = 1; i <= depth; i++) { ArrayList depth_i = new ArrayList(); //or as depth_i=[] in python } so that it should dynamically create lists.Can you please provide me a solution to this? Thanking You in anticipation 回答1: You can do what you want using globals() or

Can I declare variables of different types in the initialization of a for loop? [duplicate]

亡梦爱人 提交于 2019-11-27 04:30:19
This question already has an answer here: Is it possible to declare two variables of different types in a for loop? 7 answers Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the for-loop initializer prohibited? If so, how can you work around it? Yes, that is prohibited. Just as otherwise you cannot declare variables of differing types in one declaration statement ( edit : modulo the declarator modifiers that

Use of “var” type in variable declaration

早过忘川 提交于 2019-11-27 03:02:12
问题 Our internal audit suggests us to use explicit variable type declaration instead of using the keyword var . They argue that using of var "may lead to unexpected results in some cases". I am not aware of any difference between explicit type declaration and using of var once the code is compiled to MSIL. The auditor is a respected professional so I cannot simply refuse such a suggestion. 回答1: How about this... double GetTheNumber() { // get the important number from somewhere } And then

Why does `int ;` compile fine in C, but not in C++?

梦想的初衷 提交于 2019-11-27 02:29:20
问题 Consider the following program (see live demo here). #include <stdio.h> int main(void) { int ; // Missing variable name puts("Surprise"); } My compiler, gcc 4.8.1, gives the below warning: [Warning] useless type name in empty declaration [enabled by default] Why does it compile fine? Shouldn't I get a compiler error? g++ 4.8.1 gives the following error when I compile it as a C++ program: [Error] declaration does not declare anything [-fpermissive] 回答1: The C standard says A declaration other

Why is initialization of a new variable by itself valid? [duplicate]

柔情痞子 提交于 2019-11-27 02:11:20
This question already has an answer here: What's the behavior of an uninitialized variable used as its own initializer? 3 answers Consider some code: #include <iostream> int main() { using std::cout; int a=3; cout << "a="<<a<<"\n"; { int a=a; cout << "new a = " << a << "\n"; a=5; cout << "a = " << a << "\n"; } cout << "old a = " << a << "\n"; } I'd expect it to print a=3 new a = 3 changed a = 5 old a = 3 But what I get actually appears to say new a = 0 in the second line. I thought that it would work like initialization list in a class' constructor, where one can write like C::C(int a) : a(a)

Is it possible to declare a public variable in vba and assign a default value?

≯℡__Kan透↙ 提交于 2019-11-27 02:01:54
问题 I want to do this but it won't compile: Public MyVariable as Integer = 123 What's the best way of achieving this? 回答1: .NET has spoiled us :) Your declaration is not valid for VBA. Only constants can be given a value upon application load. You declare them like so: Public Const APOSTROPHE_KEYCODE = 222 Here's a sample declaration from one of my vba projects: If you're looking for something where you declare a public variable and then want to initialize its value, you need to create a Workbook

setq and defvar in Lisp

橙三吉。 提交于 2019-11-27 00:19:22
I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable . Isn't it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq ? There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter *number-of-processes* 10) (defvar *world* (make-world)) ; the world is made only once. Notice that you likely never

Effect of declared and undeclared variables

落爺英雄遲暮 提交于 2019-11-26 22:29:26
What is the major difference between JavaScript declared and undeclared variables, since the delete operator doesn't work on declared variables? var y = 43; // declares a new variable x = 42; delete x; // returns true (x is a property of the global object and can be deleted) delete y; // returns false (delete doesn't affect variable names) Why does this happen? Variables declared globally are also the properties of the window object, so why can't it be deleted? Declared and undeclared global variables The mechanism for storing and accessing them is the same, but JavaScript treats them