C and C++ questions for the interview

Multithreading interview questions with example answers
  • What is a thread?
  • What is multithreading?
  • What is the difference between a thread and a process?
  • Why use multithreading in your applications?
  • What is a thread pool?
  • During a thread’s lifetime, what states can it have?
  • What is a race condition?

Top 13 Multithreading Questions Asked In Interview With Explanation and PDF | Most Important

multithreading interview questions c++

struct MyClass // remember, in C++ a struct is a class that defaults to public. { long _iID; char _strLabel[16]; MyClass(long iID) : _iID(iID) { memset(_strLabel, 0, sizeof _strLabel); } virtual bool doSomething(); }; bool MyClass::doSomething() { printf(“ID = %ld Label = %-*.*s “, _iID, sizeof(_strLabel), sizeof(_strLabel), _strLabel); } /* C example with data member that is a function pointer used to simulate a virtual method. Notice how much more verbose the C example is compared to the C++ example above. */ #include struct MyClass { long _iID; char _strLabel[16]; /* sorry, no constructor here but see below!! */ bool (*_pDoSomething)(struct MyClass *pThis); }; extern struct MyClass *newInstance(); /* code for C version of a virtual method. C doesnt have a “this” pointer so we have to explicitly create one here. */ static bool doSomething(struct MyClass *pThis) { printf(“ID = %ld Label = %-*.*s “, pThis->_iID, sizeof(pThis->_strLabel), sizeof(pThis->_strLabel), pThis->_strLabel); } // C equivalent to a constructor struct MyClass *newInstance(long iID) { struct MyClass *pClass = (struct MyClass *)malloc(sizeof (MyClass) ); if (pClass) { pClass->_iID = iID; memset(pClass->_strLabel, 0, sizeof pClass->_strLabel); // assign virtual method. pClass->_pDoSomething = doSomething; } return pClass; } /* typical usage for C class above … */ int main(int argc, char *argv[]) { /* constructor called to create / initialize instance. */ MyClass *pClass = newInstance(100); if (pClass) { strcnpy(pClass->_strLabel, “Howdy folks.”, sizeof(pClass->_strLabel)); pClass->_pDoSomething(); /* … and heres a non-virtual method call. */ doSomething(pClass); /* (non-virtual) destructor call. */ free(pClass); } return 0; }

#include #include #include char *newfmt(const char *fmt, …){ �� char *p; ��� va_list ap; ���� if ((p = malloc(128)) == NULL) ������������ return (NULL); ����� va_start(ap, fmt); � ����(void) vsnprintf(p, 128, fmt, ap); ����� va_end(ap); ����� return (p); } /* Plus a little routine to show how it is used. */ int main(int ac, char *av) { int a = 42; char *str; str = newfmt(“%d”, a); if (!str) { ��� fprintf(STDERR, “Out of memory! “); exit(5); } printf(“%s “, str); free(str); return 0; }

�strstream is the class that specializes iostream to use a strstreambuf for input and output with arrays of characters in memory. You can create an strstream object by associating the object with a previously allocated array of characters. You can then write output to it, read input from it, and apply other operations to it just as you would to another type of stream.

The static keyword is useful for extending the lifetime of a particular variable. If you declare a static variable inside a function, the variable remains even after the function call is long gone (the variable is placed in the alterable area of memory). The static keyword is overloaded. It is also used to declare variables to be private to a certain file only when declared with global variables. static can also be used with functions, making those functions visible only to the file itself.

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 firast accesses the top most element in the argument stack of the printf which is %d and dependin 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 calculte the correct offset (which will be the offset to access the integer varible) but as the actual data objet 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.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *