Showing posts with label fundamental. Show all posts
Showing posts with label fundamental. Show all posts

Aug 3, 2009

char str1[]="Hello";//

char str1[]="Hello";//
char *str2=”Hello”;// const
they are different, str1 is on the local stack, str2 is also on the const data segment

static variable

Static variable
Which is destroyed when program terminates
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
static int number_of_times = 0;
number_of_times++;
}
}
or
class user
{
private:
int id;
static int next_id;

public:
static int next_user_id()
{
next_id++;
return next_id;
}
/* More stuff for the class user */
user()
{
id = user::next_id++; //or, id = user.next_user_id();
}
};
int user::next_id = 0;

Jul 8, 2009

a pipeline consists of a chain of processing elements (processes, threads, coroutines, etc.), arranged so that the output of each element is the input

a pipeline consists of a chain of processing elements (processes, threads, coroutines, etc.), arranged so that the output of each element is the input of the next.

Feb 12, 2009

memory address queation

#include
main()
{
char a[]="Hello";
char b[]="Hello";

char *p="hi";
char *q="hi";

if(a==b)
printf("Both are equal\n");
else
printf("Not equal\n");

if(p==q)
printf("Both are equal %c",*p);
else
printf("Not equal\n");
}
Not equal for the first. The two arrays are allocated separately, and the equality operator tests for whether the pointer points to the same address in memory. Two arrays in different places means that is a no.
The second, however, does produce equal pointers on my default version of gcc (I suspect there are some compilers and/or optimization settings for which this would NOT be true). The reason is because the compiler has optimized the common string literal in both places by storing it at one place in memory. That place is incidentally within the program's code segment, which means if you try touching it BAM segfault for you.

Feb 1, 2009

Regular expression to finite machine

Regular expression to finite machine
http://swtch.com/~rsc/regexp/regexp1.html

Destructor only used in static memory allocation, which is on the stack. When the object is out of the scope, the compiler will call destructor to rel

Destructor only used in static memory allocation, which is on the stack. When the object is out of the scope, the compiler will call destructor to release this object.
You should manually release dynamical memory allocation in destructor; destructor itself by default can do nothing for this.

Jan 31, 2009

When you use the new operator, there are three steps: allocate a portion of memory where the new class is going to be instantiated, instantiate the cl

When you use the new operator, there are three steps: allocate a portion of memory where the new class is going to be instantiated, instantiate the class on that memory, and assign that memory location to a variable.
Class* p = new Class;
New can throw an exception (out of memory). In this case, no memory is allocated. The variable p is not assigned a new value.
New calls the constructor of the class. The constructor can throw an exception. If class has any base classes or member objects, the constructors for each are called before the constructor for class.
If an exception is thrown in the body of Class's constructor, destructors are called for all those base and derived classes for which the constructor has already been called (cleaning those objects up). The destructor for Class is NOT called (as indicated by blank above).
Finally, memory allocated by new is freed and p is not assigned a value.
What this means in practical terms, is that classes should use smart pointers to wrap resources in classes to ensure proper cleanup.

Jan 20, 2009

Order the functions in order of their asymptotic performance

Order the functions in order of their asymptotic performance
1) 2^n
2) n^100
3) n!
4) n^n
n^100 < 2^n < n! < n^n

Jan 18, 2009

class foo { public: static int func(const char*& p) const; }; Why is this fragment incorrect ? The const function modifier is supposed to claim that

class foo {
public:
static int func(const char*& p) const;
};

Why is this fragment incorrect ?
The const function modifier is supposed to claim that the function will not modify any member variables (not a "mutator" if you want to).
But a static function, by definition, cannot access the member variables. So this function declaration doesn't make sense.

Create a class structure that helps to store multidimensional matrix with n-dimension, the constructor should take dimension and size of matrix as inp

Create a class structure that helps to store multidimensional matrix with n-dimension, the constructor should take dimension and size of matrix as inputs.
void * alloc_n_dim(int dim, int size)
{
int i;
void **p;
void * ptr;
if(dim < 1)
return NULL;
if(dim == 1)
{
ptr = malloc(sizeof(int) * size);
return (ptr);
}
p = malloc(sizeof(void *) * size); //second level pointer
for(i = 0; i < size; i++)
{
p[i] = alloc_n_dim(dim - 1, size);//p[i]==*(p+i) first level pointer
}
return (p);
}

Keyword Volatile

Keyword Volatile
Usually the compiler will put the variable in the cache for fast access, Volatile make the compiler not do optimization on variable, so the volatile variable is on the main memory.
In the multithreading, if the variable is executed in atomic read (not read modify write), you can state the variable as volatile, multi threads can get the only copy of the variable from main memory. So if you do not have mutex and lock, just use volatile to do synchronization, although it is spinlock…
Basically, Volatile can only replace mutex
http://www.ddj.com/cpp/184403766
http://blogs.sun.com/sin/entry/why_to_use_volatile_in1

Int a; Int b; Cout<< (int)&b-(int)&a << endl;//this is wrong, undefined Int a[2] Cout<< (int)&a[1]-(int)&a[0]<<endl; //this is right

Int a;
Int b;
Cout<< (int)&b-(int)&a<< endl;//this is wrong, undefined
Int a[2]
Cout << (int)&a[1]-(int)&a[0]<< endl; //this is right

int* p=0;// the value of the p is 0, not the value of *p is 0

int* p=0;// the value of the p is 0, not the value of *p is 0

How does Java achieve synchronization? Given an class with Synchronized method A and B, a normal method C, there are 2 threads, one instance, can thes

How does Java achieve synchronization? Given an class with Synchronized method A and B, a normal method C, there are 2 threads, one instance, can these two threads call A at the same time? call A and B at the same time? A & C at the same time?
A&C yes, others no, java create instance on the heap which is shared by all threads, A and B can access the same shared resource, so at one time only one function can be executed. One instance is shared resource

Consider the following c++ program #include using namespace std; int main() { const int a=10; int* b=(int*)&a; *b=20; cout<<

Consider the following c++ program
#include
using namespace std;
int main()
{
const int a=10;
int* b=(int*)&a;
*b=20;
cout<<&a<<'\n'<<&b<<'\n';
cout<<a<<'\n'<<*(&a)<<'\n'<<*b;
return 0;
}

This is what I got as output
0x22ff54
0x22ff54
10
10
20
Can anyone explain how I got different values as output when it is the same memory location I'm accessing.
BTW I was trying to override the constness of 'a'.
&a and b gave the same address as output but it gave different values for a and *b. I find it strange ( I'm perhaps missing something).

A is declared const therefore the compiler is allowed to treat it as constant. It optimized the print of a known constant at compile time. In the case of b you find it actually set the value. Putting your prints in a function may be enough to fool it into a= 20 b =20;
If you are making a class and need nonconst elements in a const instance use mutable. const_cast is the preferred way to make constness go away in C++. Though it still wouldn't save you in this case because something that is declared constant is assumed to be constant and optimized accordingly. Const casting is just there to provide wiggle room to users of libraries that are not const correct.

copy constructor and assignment operator

class A
{
public:
int i;
A(int ii) { i = ii; }
A(const A& a) { i = a.i; i++; }
A& operator=(const A& a) { this->i = a.i; i--; return *this}
};

int main(void)
{
A a(4);
A b = a; //this calls copy constructor not assignment operator
//A b(4); b=a; this calls assignment operator
cout << b.i << endl;//the result is 5
return 0;
}

How Java is platform independent

How Java is platform independent
Java has build JVM for each type of platform and JAVA compiler converts its code into bytecodes, which is uniquely interpreted by each JVM, so that makes JAVA platform independent.
JVM is java virtual machine....that is used to interpret bytecodes that are generated via java compiler by "javac xyz.java" command......

What is difference between String and StringBuffer classes

What is difference between String and StringBuffer classes
Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.
The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.

try-catch examples

try-catch examples
http://www.java2s.com/Tutorial/Cpp/0120__Exceptions/Asimpleexceptionhandlingexample.htm

Suppose there are two functions, Function First allocates a memory and then call Function Second..... But in Function Second Exception occurs!!! How d

Suppose there are two functions, Function First allocates a memory and then call Function Second..... But in Function Second Exception occurs!!! How do you notify Function First about this in order to deallocate the memory allocated by First???
Use the try catch block the function second
or
Try finally which is in java (c++ extended by Microsoft in c++)
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Executed when the program finished, because it is a keyword
finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method. Executed when the object is deleted, because it is a class member function