Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Monday, February 13, 2017

What is the difference between const char *p, char * const p and const char * const p?

const char *p - This is a pointer to a constant character. You cannot change the value pointed by p, but you can change the pointer p itself.

*p = 'S' is illegal.
p = "Test" is legal.

Note - char const *p is the same.


const * char p - This is a constant pointer to non-constant character. You cannot change the pointer p, but can change the value pointed by p.

*p = 'A' is legal.
p = "Hello" is illegal.


const char * const p - This is a constant pointer to constant character. You cannot change the value pointed by p nor the pointer p.

*p = 'A' is illegal.
p = "Hello" is also illegal.

Tuesday, February 7, 2017

Thread Sample

/* Sample program for Thread. */

int main()
{
    ExecuteOnThread();
}

void * PrintHello(void *usr_info)
{
    //Uncomment below lines to reproduce Defect #43238
    am_audio_source_t source;

    if (NULL != static_cast<TestCTC *>(usr_info))
        static_cast<TestCTC *>(usr_info)->GetActiveSource(AST_Mutex, source);

    DEBUG_PRINT("TestCTC, Active Source: %d\n", source);

   return NULL;
}

int TestCTC::ExecuteOnThread()
{
    pthread_t tid;
    int rc = pthread_create(&tid, NULL, PrintHello, this);
    return 0;
}

Sunday, April 7, 2013

How to check MACRO is defined or not in C

#warning is preprocessor directives to print messages in preprocessing.

#ifdef MAX_RES_720P
#warning "Macro is defined, Value of  Macro = " MAX_RES_720P
#else
#warning "Macro is not defined"
#endif

In above example,
If macro MAX_RES_720P  is defined, #warning will print "Macro is defined..."
Otherwise, it will print "Macro is not defined"

Tuesday, February 12, 2013

What is output of following code?

int main()
{
    int key = 2;
    switch(key)
    {
        default :
            printf("In default\n");

        case 1:
            printf("In Case: 1\n");
    }
    return 0;
}

O/P:
In default
In Case: 1

Saturday, February 9, 2013

What is difference between reference and pointer?

1) A pointer can be re-assigned:

int x = 5;
int y = 6;
int *p;
p =  &x;
p = &y;
*p = 10;
assert(x == 5);
assert(y == 10);

A reference cannot, and must be assigned at initialization:

int x = 5;
int y = 6;
int &r = x;

2) A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable.

Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you.

int x = 0;
int &r = x;
int *p = &x;
int *p2 = &r;
assert(p == p2);

3) You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.

int x = 0;
int y = 0;
int *p = &x;
int *q = &y;
int **pp = &p;
pp = &q; //*pp = q
**pp = 4;
assert(y == 4);
assert(x == 0);

4) Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain NULL.

int *p = NULL;
int &r = NULL; <--- compiling error

5) Pointers can iterate over an array, you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.

There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

6) A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a ..

7) A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.

8) References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)

9) Const references can be bound to temporaries. Pointers cannot (not without some indirection):

const int &x = int(12); //legal C++
int *y = &int(12); //illegal to dereference a temporary.
This makes const& safer for use in argument lists and so forth.


Where to use reference and pointer:
  1. Use references in function parameters and return types to define attractive interfaces.
  2. Use pointers to implement algorithms and data structures.

Friday, February 8, 2013

Dec to Hex Conversion

e.g. DEC 300 = HEX 12C
Let us try to convert DEC 10,000 to HEX.
DivisionQuotientRemainder
10,000/166250
625/16391
39/1627
2/1602
So our answer for DEC 10,000 = HEX 2710

How to convert from hex to decimal

A regular decimal number is the sum of the digits multiplied with its power of 10.

Example #1

137 in base 10 is equal to each digit multiplied with its corresponding power of 10:
13710 = 1×102+3×101+7×100 = 100+30+7
Hex numbers are read the same way, but each digit counts the power of 16 instead of power of 10.
Multiply each digit of the hex number with its corresponding power of 16.

Example #2

3B in base 16 is equal to each digit multiplied with its corresponding power of 16:
3B16 = 3×161+11×160 = 48+11 = 59

Example #3

E7A9 in base 16 is equal to each digit multiplied with its corresponding power of 16:
E7A916 = 14×163+7×162+10×161+9×160 = 57344+1792+160+9 =59305

Output of printf("%d")

What is the output of printf("%d") ?


1. When we write printf("%d",x); this means compiler will print the value of x. But as here, there is nothing after %d so compiler will show in output window garbage value.

2. When we use %d the compiler internally uses it to access the argument in the stack (argument stack). Ideally compiler determines the offset of the data variable depending on the format specification string. Now when we write printf("%d",a) then compiler first accesses the top most element in the argument stack of the printf which is %d and depending on the format string it calculated to offset to the actual data variable in the memory which is to be printed. Now when only %d will be present in the printf then compiler will calculate the correct offset (which will be the offset to access the integer variable) but as the actual data object is to be printed is not present at that memory location so it will print what ever will be the contents of that memory location.

3. Some compilers check the format string and will generate an error without the proper number and type of arguments for things like printf(...) and scanf(...). malloc()

What is typedef

What is typedef in C?
 
The purpose of typedef is to assign alternative names to existing types.
typedef is compile time entity, C is a static language, you cannot create a type during runtime, the type needs to be resolved at compile/link time.

eg. typedef struct MyStruct newtype;

output of following code

What is output of following code?
int main()
{
   int a = 5, b = 6, c;
   c = a+++b;
   printf("%d", c);

   return 0;
}


O/P: 11 
Explanation: "++" operator has high precedence than "+" operator  
So, it is solved as c = (a++) + b;
     Not as c = a + (++b) 
See Operator Precedence here

swap nibbles in integer

How to swap nibbles in integer?

int value = 420;
value = ((value >> 4) & 0x0F0F0F0F) | ((value & 0x0F0F0F0F) << 4);

Thursday, February 7, 2013

Virtual memory

Virtual Memory = Part in physical memory + Part on disk

The part in physical memory is RSS. So, VSS should be greater than RSS.

Vss = virtual set size
Rss = resident set size

RSS represents the physical memory usage and
VSS represents the virtual memory usage.