Wednesday, November 30, 2011

memset

1) What is the output of this program?

#include <stdio.h>

int main () {

   char string[10];
   char *p;
   int i=0;
 
   strcpy(string, "HELLOWORLD");

   printf("\nBefore memset: ");
   for (i=0; i<10;i++) {
      printf("%c", string[i]);
   }

   p = string;
   memset(p, '\0', sizeof(p));
 
   printf("\nAfter memset: ");

   for (i=0;i<10;i++)  {
      printf("%c", string[i]);
   }

   printf("\n");
}


2) The second question is: In the above program what would be the output if the memset was changed to
memset(p, '\0', sizeof(*p));
?






Answers Below:

1) 
Before memset: HELLOWORLD
After memset: LD


2)
Before memset: HELLOWORLD
After memset: ELLOWORLD


Be careful when using memset with pointers. In the above example, in order to zero out all the contents of "string", the memset statement should have been as follows

memset(p, '\0', sizeof(string));

To read more about memset check here and here

Friday, November 25, 2011

extern and more

Two more :)

1. What is the difference between declaration and definition of a variable ?
     The definition allocates memory for the variable, whereas the declaration just tells the compiler that the memory for the variable is allocated somewhere else in the program. Note that a variable or a function can be "declared" any number of times, while it can be "defined" only once.


2. What are "extern" variables/functions ?
     When a variable/function is declared as extern in a file it means that the variable/function is defined somewhere in the program and that it can be used harmlessly in this file. The extern keyword extends the scope of the entity beyond the file in which it is defined.
     The functions are by default "extern" entities even though we don't use it while declaring or defining the function. So extern doesn't make much sense when used with functions.
     For variables, however, extern keyword has many implications. It tells the compiler that the definition of the variable is elsewhere and forces the statement to declare the variable. If extern is not used then the statement can be treated as a declaration or a definition depending on the compiler, which will analyze the modules and decide if the statement is a definition or a declaration. On the other hand, if you want to force the statement to define a variable then initialize it, this will make sure the memory is allocated for the variable.
    Check this for more information.

Tuesday, November 15, 2011

Process Memory and Pointers

Friends, here are the two questions for today !

1. What is the size of a pointer in C ?
     That's a loaded question. The size of a pointer is based on the underlying machine. A 16-bit machine will have pointers of size 2 bytes. Likewise, 32-bit machines will have pointers of size 4 bytes. Can you guess the size of a pointer on a 64-bit machine? Note that this is not necessarily true on all the systems, there are a few exceptions, but this answer should generally hold good.
     Another point to note is that size of the pointer doesn't depend on what it is pointing to. For instance, an integer pointer and a character pointer will have the same size on a machine.

2. How is the process memory organized ?
    Simple answer is that it depends on the architecture on which the process is getting executed. However, the general organization of the process memory is given below for UNIX, while other systems have some mutated versions of the same:
 

Text
Data
BSS
Heap

Stack


Text : Contains executable read-only data
Data: Contains initialized global and static variables used by the program
BSS: Contains uninitialized global and static variables. Note that they are internally initialized to zero, but placed in BSS (Block Started by Symbol).
Heap: Dynamically allocated memory is taken out of this memory partition. All your malloc/realloc/free calls operate in this region.
Stack: Used to maintain function calls. More famously known as Program Stack. Note that Heap and Stack grow towards each other.

Click here for more information.

Thursday, November 10, 2011

pointers and increments

Today's 2 questions are based on pointers. Pretty easy ones for you C punters out there.


1) What is the output of the following?
This is based on the question here

#include <stdio.h>

main()
{
   char *ptr =" DARN C !";
   *ptr++;
   printf("%s\n",ptr);
   ptr++;
   printf("%s\n",ptr);

}


Answer:
DARN C !
ARN C !


2) On similar lines, what is the output of this one?

#include <stdio.h>

main()
{
   int array[] = {1, 10, 100, 1000, 10000};
   int *parray = array;

   *parray++;
   printf("%d\n", *parray);

   parray++;
   printf("%d\n", *parray);

   (*parray)++;
   printf("%d\n", *parray);
}



Answer:
10
100
101


If p is a pointer, what is the difference between *p++, p++ and (*p)++ ?

*p++ increments the pointer and dereferences it
p++ increments the pointer
(*p)++ increments the value pointed to by the p




Wednesday, November 9, 2011

Staying Sharp

In the rat race of IT professionals, staying up-to-date and sharp makes a lot of difference between passion for work vs unbearable boredom and between just existing in the software industry vs having the capability  to build world-changing products :)

We have been working in the software industry on C and OS  for quite sometime now and have noticed our minds aren't as sharp as they used to be. It takes a lot more tests and debugging these days to figure out the NULL pointer being dereferenced or the reason why a process is waiting for an infinite amount of time. Nah, C hasn't changed and neither has OS fundamentals.

The basics of C and OS seemed to have fallen through the crags and crannies of our brain into irretrievable pits. We never brush up the fundamentals on a periodic basis and get worse at our jobs and get put in even duller projects.

This blog is mainly for us to polish our basics on C and OS. We plan to solve 2 questions a day from sources such as the internet or books and will be posting those questions on the blog as well. Do join us in our quest to improve our basics / learn things we never knew in the first place!

Do enter your email id in the right-hand side box and get questions on C / OS delivered to your inbox :)