Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Please tell me the difference b/w below three declaration-
private static int i=10;
private static final int j=10;
private final int k = 10;
C'mon. Just read what each keyword means:
static means "associated with a class rather than instances"
final is more complex - context dependent.
http://javamex.com/tutorials/synchronization_final.shtml
private static int i=10; // i is associated with class; mutable
private static final int j=10; // j is associated with class; immutable
private final int k = 10; // k is associated with instances; immutable
Short answer:
Final: you cannot change the var after the declaration.
Static: the variable is associated with a class not with instances
private static int i=10; // belongs to the class
private static final int j=10; // belongs to the class and it's unchangeable
private final int k = 10; // it's unchangeable
Long Answer:
Final: Define an entity once that cannot be changed nor derived from later. More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression. All methods in a final class are implicitly final.
Static: Used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. static also is used to define a method as a class method. Class methods are bound to the class instead of to a specific instance, and can only operate on class fields. (Classes and interfaces declared as static members of another class or interface are actually top-level classes and are not inner classes.)
It's sometimes helpful to think of "static" in a similar way to "global". So the differences are the following:
private static int i = 10; // this means that all instances of a class will share the same variable i and whatever its current value is. E.g., Thing A and Thing B are instances of class Thing. If A modifies the variable i, then it will be modified for B as well.
private static final int j = 10; // this means that all instances of the class will share the same value for j just like number 1. Additionally, you cannot change the value of j. It is a constant, immutable.
private final int k = 10; // this means it's a constant.
Related
I heard that statics and globals are stored in the same section. And if that's not true, like global variables static locals won't be removed from memory till code is unloaded or program exits.
Consider the following code:
void f1() {
static int i;
...
...
}
void f2() {
static int i;
...
...
}
if both i's are in same section, how will processor differentiate between them?
And how does processor load corresponding static locals when a function is called?
This question can be extended to multi-file global-static variables.
Each variable should have its own unique location and SCOPE! The scope is important and every variable has a scope associated with it. That is what keeps int i in f1 separate from int i in f2. So you can see since this is present in normal variables, the same applies to static variables.
Your question slightly confuses me but I think I answered it. If you're asking where the memory address is, there is no way to predict that.
I know that value classes don't have an default constructor as the compiler initializes all elements in this class with zero. But arrays are in a value class are not initialized:
value class c_LocationVal
{
public:
double x, y, z;
c_LocationVal(double i_x, double i_y, double i_z) {x = i_x; y = i_y; z = i_z;}
};
typedef cli::array<c_LocationVal> arrloc;
value class c_Managed
{
public:
arrloc^ m_alocTest;
//c_Managed() { m_alocTest = gcnew arrloc(3); } --> not permitted
double funcManaged ()
{
return m_alocTest[0].x; --> error: Object reference not set to an instance of an object
}
};
I just could cheat and use:
c_Managed(int i) { m_alocTest = gcnew arrloc(3); }
but there must be another solution.
Can someone please tell me how to solve this?
The CLR only supports code inside of methods. Compilers emulate the behavior of a member initialization expression by creating a constructor, if necessary, and moving the code for the expression into the constructor.
Which explains why this isn't permitted, your expression requires a parameterless constructor and that's not legal for a value type.
Sure, your trick will work. But in general, you need to de-tune C++ assumptions a bit when you write C++/CLI code. There are no practical differences between a struct and a class in C++. But that's definitely not the case in managed code. Only ever use a value class for very simple types. Requiring initialization heavily tips the choice to a ref class. As does a value type having an array, you'd normally need a deep copy to make that work without accidents. Never fear the heap in C++/CLI, it is very fast.
A value class is always initialized with "null/0". So a managed reference in a value class will also always be initialized to "null". If you want to have a special initialization, then you only have the solution, you were pointing out: You need to create a special constructor which has some parameters to "initialize" the value class correctly.
The question is: Do you really need a value class which contains a managed reference??? Normally this should also be a ref class.
Also, what happens, if the value class is copied? What should happen with the reference? It will also directly copied! Is this intended? The goal of a value class is to provide a "real" copy! In your case it will not "fully copied"...
Pleas re-think if a value class is the best solution for your data storage...
Switch the public field to a property, and do lazy initialization when the property is retrieved.
value class c_Managed
{
private:
arrloc^ m_alocTest;
public:
arrloc^ AlocTest
{
arrloc^ get()
{
if(m_alocTest == nullptr)
{
msclr::lock(c_Managed::typeid)
if(m_alocTest == nullptr)
m_alocTest = gcnew arrloc(3);
}
return m_alocTest;
}
}
double funcManaged ()
{
return AlocTest[0].x;
}
};
The lock for the lazy initialization isn't ideal, but it's just about the only thing to lock on: this is a value type, so locking would box it and the lock would be on the box, not on the object itself. Since it's a value type, providing any reference type as a field to lock on would give a null reference, just like the array, so there's no help there. The only thing I can think to lock on is the type object itself for this type.
I've started using Doxygen for a C project. I have set both EXTRACT_ALL and EXTRACT_STATIC to NO. Still, some of my file-level static variables show up in the documentation that Doxygen generates.
Of this block of definitions, fps_ypos and fps_height are included in the docs:
/* properties of the frames per second text */
static int fps_xpos, fps_ypos;
static int fps_length, fps_height;
static bool show_fps = FALSE;
bool is a typedef for unsigned char, if that matters. This is MSVC C, not C99.
Anyone know what can cause this or what I can do to fix it?
By the way, I'm using Doxygen 1.7.5.1 on Windows.
I'm unsure if it's intended behaviour or not, but as the two variables that are documented are second on the line, I'd suggest changing your code to this, i.e. splitting the declarations up to one per line, if you care enough about it:
/* properties of the frames per second text */
static int fps_xpos;
static int fps_ypos;
static int fps_length;
static int fps_height;
static bool show_fps = FALSE;
Content of X.c:
int i;
main ()
{
fun ();
}
Content of Y.c:
int i;
fun ()
{
}
Why does these two files compile with no error ? (using GCC)
But if i use int i = 10; it prints a multiple definition error.
You may be interested in this question and the answers. Keywords: "tentative definition".
Tentative definitions in C99 and linking
Assuming you really want an independent variable called i in each of these two files, you need to prefix them with static in order to give them internal linkage.
static int i = 10;
If you want i to be the same variable in both files, so changes in one affect the other, use the answers you were given 3 hours ago when you asked a variant of the question. If it is to be shared, you need to define the variable in one place.
As to why it didn't cause an error without the init, I think that's because you weren't using the variable until it needed initializing and so the compiler ignored it.
Because there is a difference between a declaration and a definition. int i; does nothing more than introducing a name. int i = 10; on the other hand defines i, hence, a place in the memory must be reserved to store the value it corresponds to. But it is impossible for the compiler to know which value corresponds to i as you want to associate two memory locations with the name i.
This is under the assumption that you link these files against eachother, which is not entirely clear from your explanation.
These are my errors:
error: static declaration of doct follows non-static declaration
error: previous declaration of doct was here.
And my code is:
int doct(int*); /* <- Second error points here */
private int doct(int *a)
{
static int a=0; /* First error points here */
a++;
*a=a;
return 0;
}
Any suggestions?
Your prototypes should match your actual functions. Yours do not:
int doct(int*);
private int doct (int *a)
Either change the prototype to:
private int doct(int*);
or change the function to:
int doct (int *a)
You should also bear in mind that private is not part of the C language, but people often use it to replace static. This can be made possible by the line:
#define private static
with the only proviso being that that macro must be active wherever you use the private name. If it's not working on your prototype, that's probably because it's not defined at that point. My advice would be to ditch private altogether and use static (if indeed that's how private is defined). People should learn the language, not adopt unnecessary crutches (in my opinion).
Other favourites which I also despise are:
#define global extern
#define begin {
#define end }
The private and global are used to mean local to this file and global to all files respectively. The begin and end are particularly nasty abominations from people who should go back to Pascal where they belong :-)
In addition to that problem, your line:
static int a = 0;
will actually hide the parameter that you're passing into the function (since it has the same name) and:
*a = a;
will cause an error (since it has a different type). It's rarely a good idea to do that. Rename one of them.
This error happens when a function was declared as non-static, then defined static, such as:
void foo(void);
static void foo(void) {}
Make static match on both, either by removing it from both or adding it to both. Make sure you understand what static does.
If your function is marked static, it is only visible in that translation unit. In your case, your declaration has no static meaning "this function will be available, non-statically.", but then you define it statically.
There are other errors. The a in your function will hide the a in the parameter list. You need to give them different names. *a = a won't work because, in that scope, a is an integer, not a pointer. Use a descriptive name like counter for the integer.