local-variables

Can a local variable's memory be accessed outside its scope?

心已入冬 提交于 2019-12-25 04:56:09
问题 I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The output was 58 How can it be? Isn't the memory of a local variable inaccessible outside its function? 回答1: How can it be? Isn't the memory of a local variable inaccessible outside its function? You rent a hotel room. You put a book in the top drawer of the bedside table and go

Local variable not used

我们两清 提交于 2019-12-25 02:51:58
问题 I am trying to make a test of whether an inputted word is a palindrome or not (the same spelled forward and backward). From what I can see it should work but Eclipse says "The value of the local variable isPalindrome is not used" , but it is used. The problem is that even if the word is not a palindrome it says it is. import java.util.Scanner; public class Palindrome { public static void main(String[] args) { String phrase; char[] phraseLetters; int endChar; boolean isPalindrome; Scanner

Why would I be getting a Null pointer exception with Gui input and listener methods?

℡╲_俬逩灬. 提交于 2019-12-24 05:22:05
问题 I am new to Java and I am trying to allow a user to enter an employees first and last name via the Gui and when they press the submit button it activates the listener methods and allows the values entered to be gathered and put in the systems memory My issue is that when I enter the first name it works perfectly but when I enter the last name it does not work at all I press the submit button and the the whole thing goes nuts the error is null pointer exception "AWT event queue". And I can see

local variables overwrite in function run multiple times in a loop

孤人 提交于 2019-12-23 19:24:56
问题 Hey I'm having small issue here. Question: how can I create unique variables for a function so when called multiple times, the variables will hold what they should (not swap) keep in mind I have to keep it asynchronous as the loop will be large and not running it async would hit the performance very hard I have a function which draws items on canvas. Then I run this function in for loop to draw few canvases depending on some data in array. so simplified version: function renderCanvas(canvas,

Make A javascript variable only avaliable to functions on the same .js file

喜夏-厌秋 提交于 2019-12-23 12:25:11
问题 I somewhat new to Javascript and I'm stuck on this one item. Can someone please show me how to make a javascript variable only usable on the .js file that it is on. EXAMPLE: Say I have two .js files, PG1 & PG2. PG1 contains var channel = Channel01; PG2 contains a variable with the same name, but a different entered Variable (the part after the equals sign) (var channel = Channel02) I don't want function1 on PG1 to use the variable on PG2 or for function2 on PG2 to use the variable on PG1. I

What is the idea behind using a stack for local variables?

流过昼夜 提交于 2019-12-23 11:59:07
问题 In C as many of you know, the stack is where all local variables reside. The stack being a first in last out data structure means you can only access what has been most recently pushed onto it. So given the following code: int k = 5; int j = 3; short int i; if (k > j) i = 1; Obviously this is useless code which has no real meaning but I'm trying to wrap my head around something. For the short int i declaration I'm assuming 2 bytes get allocated on the stack. For int k and int j for both 4

PHP performance: $this->variable versus local $variable (manipulating)

北慕城南 提交于 2019-12-23 09:27:20
问题 I had a section in a class that I decided to split into a new one. When I had ported the code section into a new class I noticed it was considerably slower at executing one of the foreach loops. I managed to track down part of the problem to be how I decided to save the final result array. I think it'll be easier to understand if you see a shortened version of my code: The original ported code: http://pastebin.com/2iBuqmgn More optimized ported code: http://pastebin.com/TYU1rHwU You'll see

Why are local variables not declared final in most open source java projects?

二次信任 提交于 2019-12-23 09:09:08
问题 If I look at the java source source code in the OpenJDK or Hibernate or Apache I have yet to see any local variables declared final. This suggests that the developers of some of the most widely used java software libraries: do not believe the final keyword improves readablity. do not believe it significantly improves performance. Why do the majority of contrbuters on stackoverflow believe it it should be used (based on the highest voted responses)? 回答1: do not believe the final keyword

Use of unassigned local variable on finally block

雨燕双飞 提交于 2019-12-23 08:50:13
问题 When could i in this example be unassigned? int i; try { i = 2; } catch { i = 3; } finally { string a = i.ToString(); } 回答1: You could get a ThreadAbortException before i=2 runs, for example. Anyway, the C# compiler is not exceptionally smart, so it's easy to fool with contrived examples like the above one. It doesn't have to recognize every situation, and if it's not sure it is assigned, even if you are sure, it will complain. EDIT: I was a bit quick on my first assumption. So to refine it,

What is the difference between local, instance, and class variables? [duplicate]

醉酒当歌 提交于 2019-12-23 02:07:22
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Difference between class variables and class instance variables? While reading a Ruby book, I seem to have missed the variables chapter. Now I can't seem to understand the following things: What is an instance variable? What is a class instance variable? What is the difference between a variable , @instance_var and @class_instance_var ? I tried to read some posts in different blogs, but I still do not understand