static-variables

Local variables set to nil? (Objective-C)

拜拜、爱过 提交于 2019-11-27 12:08:51
I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display it and a was displayed as zero. I was a little confused on that and I was wondering if someone could clarify it for me? With ARC enabled, your Objective-C object pointer variables will be set to nil regardless of where you create them. Without ARC, and for built in C types, your variables will not be initialized. Instance variables of Objective-C

Values obtained in case of a recursive function

非 Y 不嫁゛ 提交于 2019-11-27 09:31:37
Can anyone explain to me the reason behind the output of this program to be 0 0 0 0 0 ? Here we are using a static variable var whose values will not change due to function calls. The values of var will be 4, 3, 2, 1 during the recursive calls. When var becomes zero the recursion terminates and control moves on to the printf statement. Why is the output not 1,2,3,4 ? main(){ static int var=5; if(--var) main(); printf(" %d ",var); } Again if you use if condition var-- then program output will be -1 -1 -1 -1 -1 -1 ? In your recursion call printf() executes when main() returns. And because var is

Why does GCC not warn for unreachable code?

主宰稳场 提交于 2019-11-27 07:36:02
问题 I wonder why gcc (4.6.3) gives me no warning for the unreachable code in this example: #include <stdio.h> int status(void) { static int first_time = 1; if (first_time) { return 1; first_time = 0; /* never reached */ } else { return 0; } } int main(int argc, const char *argv[]) { printf("first call %d\n", status()); printf("second call %d\n", status()); return 0; } Note, the purpose of the faulty status() function was to maintain a status. I had expected to get a warning for this with -Wall .

Java Field Hiding

删除回忆录丶 提交于 2019-11-27 06:22:21
问题 I was wondering what it means to say a field is hidden between 2 java classes and what it means when running code in terms of resulting output? I have an abstract class with a protected static boolean field = false and a sub class which has a boolean field with the same name but is not static and set to true . If I had this code: Superclass d = new subclass(); what would be the value of the boolean field in the superclass and the boolean field in the subclass? Does subclass field stay as

Can I have different copies of a static variable for each different type of inheriting class

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:09:12
问题 I want to have the same static variable with a different value depending on the type of class. So I would have public class Entity { public static Bitmap sprite; public void draw(Canvas canvas, int x, int y) { canvas.drawBitmap(sprite, x, y, null); } } public class Marine extends Entity { } public class Genestealer extends Entity { } And then in my main program go: Marine.sprite = // Load sprite for all instances of Marine Genestealer.sprite = // Load sprite for all instances of Genestealer I

Why retain a static variable?

自作多情 提交于 2019-11-27 05:40:28
问题 Isn't it unnecessary to retain a static variable since it stays around for the duration of the program, no matter if you release it? See this code: https://github.com/magicalpanda/MagicalRecord/blob/master/Source/Categories/NSManagedObjectContext+MagicalRecord.m#L24-29 回答1: I'm assuming you mean a static object pointer, such as static NSString *foobar; . Such variables indeed have a lifetime as long as the application, but the variables we're talking about are pointers only . In Objective-C,

Google App Engine: Memcache or Static variable?

佐手、 提交于 2019-11-27 05:38:14
问题 Well, I think I have a very basic doubt here: I'm developing an app on GAE (Java) and performing a query to the datastore that returns a lot of entities, so I need to cache it. I was using memcache and it was working great, but if I keep the list of entities in a static variable, the whole request goes as twice as fast than using memcache. I think that's because I'm not deserializing the entities all the time. What would be the drawback of using a static variable instead on memcache? I don't

Access a global static variable from another file in C

旧城冷巷雨未停 提交于 2019-11-27 04:54:57
In C language, I want to access a global static variable outside the scope of the file. Let me know the best possible way to do it. One of the methods is to assign an extern global variable the value of static variable, In file a.c static int val = 10; globalvar = val; In file b.c extern globalvar; But in this case any changes in val(file a.c) will not be updated in globalvar in (file b.c). Please let me know how can I achieve the same. Thanks, Sikandar. Well, if you can modify file a.c then just make val non-static. If you can modify a.c but can't make val non-static (why?), then you can just

How to access a variable across two files

戏子无情 提交于 2019-11-27 02:19:57
问题 I have three files - global.php, test.php , test1.php Global.php $filename; $filename = "test"; test.php $filename = "myfile.jpg"; echo $filename; test1.php echo $filename; I can read this variable from both test and test1 files by include 'global.php'; Now i want to set the value of $filename in test.php and the same value i want to read in test1.php. I tried with session variables as well but due to two different files i am not able to capture the variable. How to achieve this........

Main Program and Shared Library initializes same static variable in __static_initialization_and_destruction_0

筅森魡賤 提交于 2019-11-27 01:46:38
问题 Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library re-initializes the main program's copy of the static variable and destructs it, causing a segfault when the main program attempts to destruct it. Is this a case of bad name mangling in the symbol table? 回答1: This is a case where the runtime linker only wants a