Tuesday, February 3, 2009

C++ : TechPitcher

C++ : TechPitcher: "
Implement strtok() function. (It tokenizes the string )

Implementation of strtok
view plaincopy to clipboardprint?

1. char* strtok(char *s, const char *delim) {
2. static char *old = NULL;
3. char *token;
4. if(! s) {
5. s = old;
6. if(! s) {
7. return NULL;
8. }
9. }
10. if(s) {
11. s += strspn(s, delim);
12. if(*s == 0) {
13. old = NULL;
14. return NULL;
15. }
16. }
17.
18. token = s;
19. s = strpbrk(s, delim);
20. if(s == NULL) {
21. old = NULL;
22. } else {
23. *s = 0;
24. old = s + 1;
25. }
26. return token;
27. }


char* strtok(char *s, const char *delim) {
static char *old = NULL;
char *token;
if(! s) {
s = old;
if(! s) {
return NULL;
}
}
if(s) {
s += strspn(s, delim);
if(*s == 0) {
old = NULL;
return NULL;
}
}

token = s;
s = strpbrk(s, delim);
if(s == NULL) {
old = NULL;
} else {
*s = 0;
old = s + 1;
}
return token;
}


Write implementation of memcpy and memmove functions? What is the difference between the two?
Implementation of memcpy
view plaincopy to clipboardprint?

1. void memcpy(void* dest, void* src, int n_size) {
2. char* d = (char*) dest;
3. char* s = (char*) src;
4. while(n_size-- > 0) {
5. *d++ = *s++;
6. }
7. }


void memcpy(void* dest, void* src, int n_size) {
char* d = (char*) dest;
char* s = (char*) src;
while(n_size-- > 0) {
*d++ = *s++;
}
}

Implementation of memmove

1. void memmove(void* dest, void* src, int n_size) {
2. char* d = (char*) dest;
3. char* s = (char*) src;
4. if(d > s) {
5. while(n_size-- > 0) {
6. *d-- = *s--;
7. }
8. } else {
9. while(n_size-- > 0) {
10. *d++ = *s++;
11. }
12. }
13. }


void memmove(void* dest, void* src, int n_size) {
char* d = (char*) dest;
char* s = (char*) src;
if(d > s) {
while(n_size-- > 0) {
*d-- = *s--;
}
} else {
while(n_size-- > 0) {
*d++ = *s++;
}
}
}

memmove ensure correct copying when the two buffers overlap.

What is a dangling pointer?
A dangling pointer is a pointer to memory that is no longer allocated. The pointer still points to the memory which earlier was allocated for the data. The data is deleted and the memory may now be used with some other purpose.
view plaincopy to clipboardprint?

1. {
2. char *danglingPointer = NULL;
3. /* ... */
4. {
5. char localChar;
6. danglingPointer = &localChar;
7. }
8. /* Memory was allocated to danglingPointer in the block. */
9. /* As soon as program is out of block dandlingPointer points to Memory, */
10. /* which is free for other use.*/
11. }


{
char *danglingPointer = NULL;
/* ... */
{
char localChar;
danglingPointer = &localChar;
}
/* Memory was allocated to danglingPointer in the block. */
/* As soon as program is out of block dandlingPointer points to Memory, */
/* which is free for other use.*/
}

What is a double pointer? Why is it required? Is it necessary in C++?
Pointer to a pointer is called double pointer. It is needed when one wants to manipulate the pointer itself like in linked list manipulation etc. In C++ same thing can be accomplished using reference which also avoids the overhead of copying."

No comments: