Test Questions

Posted on 2006-05-08
Last Modified: 2009-08-26
These are the test questions I had in my written test.
1.      memset is sometimes used to initialize data in a constructor like the example below. What is the benefit of initializing this way?  Does it work in this example?  Does it work in general ? Is it a good idea in general?

      class A {
         int a;
         float f;
         char str[35];
         long *lp;

         memset(this, 0, sizeof(*this));

2.      You have a class that many libraries depend on. Now you need to modify the class for one application. Which of the following changes require recompiling all libraries before it is safe to build the application?

         a.  add a constructor
         b. add a data member
         c. change destructor into virtual
         d. add an argument with default value to an existing member function

Question by:koppcha
    LVL 86

    Expert Comment


    >>What is the benefit of initializing this way?

    You will avoid errors that could result in a random memory state
    >>Does it work in this example?

    memset(this, 0, sizeof(*this));

    will defintely work for the purpose of setting that class' members to zero.

    >>Does it work in general ?

    Yes. If the defaults the variables are set to are handled correctly by the code tha deals with them.

    >>Is it a good idea in general?

    Initializing variables or memory in general is always a good idea, since odd things can happen if you get to use a randomly initialized value, NULLing data is a good choice for sure.
    LVL 86

    Accepted Solution

    Oh, I forgot about


    All of them.
    LVL 8

    Author Comment

    Thanks jkr

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now