local-variables

Local variable still exists after function returns

五迷三道 提交于 2020-04-06 03:21:15
问题 I thought that once a function returns, all the local variables declared within (barring those with static keyword) are garbage collected. But when I am trying out the following code, it still prints the value after the function has returned. Can anybody explain why? int *fun(); main() { int *p; p = fun(); printf("%d",*p); //shouldn't print 5, for the variable no longer exists at this address } int *fun() { int q; q = 5; return(&q); } 回答1: If you really want it to loose the value, perhaps

Parts of code of local script randomly stop working on respawn

混江龙づ霸主 提交于 2020-01-26 02:02:09
问题 I have a question concerning a Roblox game. The game works fine until a character dies. Then, random parts of the local script stop working. It's too long to explain so here is a link to the game: https://web.roblox.com/games/4586742374/Times-Tables-Turf-War If you are interested, walk around the board and look at how the columns and the rows light up. It all works. Then, let your character die. You can do this either by falling of the edge of the board or by standing on an enemy's platform

How do I value-initialize a Type* pointer using Type()-like syntax?

早过忘川 提交于 2020-01-23 04:31:08
问题 Variables of built-in types can be value-initialized like this: int var = int(); this way I get the default value of int without hardcoding the zero in my code. However if I try to do similar stuff for a pointer: int* ptr = int*(); the compiler (Visual C++ 10) refuses to compile that (says type int unexpected ). How do I value-initialize a pointer in similar manner? 回答1: How do I value-initialize a Type* pointer using Type()-like syntax? You cannot. The syntax T() is defined in 5.2.3/1,2 (C+

Local variable may not have been initialized

蹲街弑〆低调 提交于 2020-01-17 03:57:05
问题 public class DatabaseHandler extends SQLiteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "contextsManager"; // Locations table name private static final String TABLE_LOCATIONLABLES = "locationLables"; // LOCATIONLABLES Table Columns names private static final String KEY_LOCID = "loc_id"; private static final String KEY_LOCNAME = "loc_name"; public DatabaseHandler(Context

Java: arrays as reference types, in methods

人盡茶涼 提交于 2020-01-15 11:38:36
问题 I am moving from C++ to Java, and I have a problem understanding how, in Java, an array lasts outside the method in which it was created. Take a look at this simple code below: public static int[] arrayMethod(){ int[] tempArray = {1, 2, 3, 4}; return tempArray; } public static void main(String[] args){ int arr[] = arrayMethod(); for(int i : arr){ System.out.println(i); } } In C++, unless the array is dynamically allocated with the new operator, the array would not exist after the call,

Can i declare a static variable inside static member function in Java?

寵の児 提交于 2020-01-11 19:45:35
问题 private static int Fibonoci(int n) { static int first=0; static int second=1; static int sum; if(n>0) i am getting a error "Illegal Modifier" and if i remove static keyword there is no error and i need those variables to be static 回答1: You can not declare varibale as static inside a method. Inside method all variables are local variables that has no existance outside this method thats why they cann't be static. static int first=0; static int second=1; static int sum; private static int

Interpretation as a local variable overrides method name?

孤人 提交于 2020-01-09 11:12:52
问题 As in this question, when a local variable not defined is used within its own assignment, it is evaluated to nil . x = x # => nil But when the name of a local variable conflicts with an existing method name, it is more tricky. Why does the last example below return nil ? {}.instance_eval{a = keys} # => [] {}.instance_eval{keys = self.keys} # => [] {}.instance_eval{keys = keys} # => nil 回答1: In Ruby, because methods can be called without an explicit receiver and without parentheses, there is a

Interpretation as a local variable overrides method name?

℡╲_俬逩灬. 提交于 2020-01-09 11:12:48
问题 As in this question, when a local variable not defined is used within its own assignment, it is evaluated to nil . x = x # => nil But when the name of a local variable conflicts with an existing method name, it is more tricky. Why does the last example below return nil ? {}.instance_eval{a = keys} # => [] {}.instance_eval{keys = self.keys} # => [] {}.instance_eval{keys = keys} # => nil 回答1: In Ruby, because methods can be called without an explicit receiver and without parentheses, there is a

Pointer to local variable in C++ [duplicate]

眉间皱痕 提交于 2020-01-05 08:10:29
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can a local variable's memory be accessed outside its scope? I have the following code in C++ int* foo() { int myVar = 4; int* ptr = &myVar; return ptr; } int main() { printf("val= %d", *foo()); return 0; } The output i get is: val = 4 So my question is since myVar is a local variable, shouldn't it be gone after the function returns? and shouldn't the pointer to it be a null pointer as well? 回答1: So my question

Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

时光毁灭记忆、已成空白 提交于 2020-01-04 11:03:53
问题 Each slot of local variable array is of 4-bytes. So to store a character, short or byte variables one slot is used. Means all smaller data-types internally gets converted into int data type. My doubt is: 1). Is it not making smaller data types useless, if internally they are of 4-bytes?, If yes, Why not remove such data-types from the language? 2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte? 回答1: 1). Is it not making smaller data types