• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Few interesting C++ questions

Joined
5/27/09
Messages
106
Points
28
1). What is the difference between malloc and new? Why is one preferable to the other?

2). 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?

C++:
[/SIZE]
[SIZE=3] class A {[/SIZE]
  [SIZE=3]      public:[/SIZE]
  [SIZE=3]         A();[/SIZE]
  [SIZE=3]      private:[/SIZE]
  [SIZE=3]         int a;[/SIZE]
  [SIZE=3]         float f;[/SIZE]
  [SIZE=3]         char str[35];[/SIZE]
  [SIZE=3]         long *lp;[/SIZE]
  [SIZE=3]      };[/SIZE]
  [SIZE=3] [/SIZE]
  [SIZE=3]      A::A()[/SIZE]
  [SIZE=3]      {[/SIZE]
  [SIZE=3]         memset(this, 0, sizeof(*this));[/SIZE]
  [SIZE=3]      }[/SIZE]
[SIZE=3]

3). 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


Let's see some answers... :)
 
Forget malloc and all that. It's old style C. Use new and delete.
It's only asking for trouble in code.

And in the example, I would use string and vectors, Otherwise your class will be very very buggy.
HTH
 
1). What is the difference between malloc and new? Why is one preferable to the other?

2). 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?

C++:
[/SIZE]
[SIZE=3] class A {[/SIZE]
  [SIZE=3]      public:[/SIZE]
  [SIZE=3]         A();[/SIZE]
  [SIZE=3]      private:[/SIZE]
  [SIZE=3]         int a;[/SIZE]
  [SIZE=3]         float f;[/SIZE]
  [SIZE=3]         char str[35];[/SIZE]
  [SIZE=3]         long *lp;[/SIZE]
  [SIZE=3]      };[/SIZE]
  [SIZE=3] [/SIZE]
  [SIZE=3]      A::A()[/SIZE]
  [SIZE=3]      {[/SIZE]
  [SIZE=3]         memset(this, 0, sizeof(*this));[/SIZE]
  [SIZE=3]      }[/SIZE]
[SIZE=3]

3). 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


Let's see some answers... :)


I believe this is a C++ test. Did you give this test or you still have time to send it back to them?
Anyway for question 3) a) b) and c) may need recompilation but no 4) should not need recompilation. Recompilation for a) is required only if the constructor is overloading an old one with different arguments and you want to call the new one now from the applications. Also if there was no constructor previously and now you add a constructor, then also you need to recompile as the default constructor will no longer be valid.
 
1). What is the difference between malloc and new? Why is one preferable to the other?

2). 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?

C++:
[SIZE=3] class A {[/SIZE]
  [SIZE=3]      public:[/SIZE]
  [SIZE=3]         A();[/SIZE]
  [SIZE=3]      private:[/SIZE]
  [SIZE=3]         int a;[/SIZE]
  [SIZE=3]         float f;[/SIZE]
  [SIZE=3]         char str[35];[/SIZE]
  [SIZE=3]         long *lp;[/SIZE]
  [SIZE=3]      };[/SIZE]
  
  [SIZE=3]      A::A()[/SIZE]
  [SIZE=3]      {[/SIZE]
  [SIZE=3]         memset(this, 0, sizeof(*this));[/SIZE]
  [SIZE=3]      }[/SIZE]


3). 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


Let's see some answers... :)

About Q-2 : Ask below questions to yourself when you want to use such memset in general.
1) What if class has virtual member function
2) What if class is derived
 
1) Malloc is a function/ new is an operator, constructors cannot be evoked with malloc

2) memset can be used if

a) if is a base class constructor

cannot be used
a) in derived class constructor ,because the virtual table will be cleared after memset


3)
b) data member ...if sizeof is used then recompilation is needed
c) change the destructor to virtual ....only this would need recompilation of all libraries
 
3)
b) data member ...if sizeof is used then recompilation is needed

wouldn't this change the object size in general? the new object won't fit in memory after addition of a new data member. correct me if i'm wrong.

---------- Post added at 08:08 PM ---------- Previous post was at 07:59 PM ----------

a) b) and c) may need recompilation but no 4) should not need recompilation.

a) will not need recompilation.

only if functionality of new constructor is desired in those depend libraries, it will require adjustment of code thus recompilation

Recompilation for a) is required only if the constructor is overloading an old one with different arguments and you want to call the new one now from the applications. Also if there was no constructor previously and now you add a constructor, then also you need to recompile as the default constructor will no longer be valid.

correct, but that's the case that i mentioned, but the question does not tell you that the new constructor will be used, simply dependency issue
 
Back
Top