CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Can someone recommend a project?

  1. #1
    Join Date
    Jul 2008
    Posts
    18

    Can someone recommend a project?

    Hello,

    I have an interview for a job where I know I will be drilled on my C++ abilities. However, I will not be expected to be a guru, by any means. I'm an EE and I have on and off experience with C and C++. I've always been able to accomplish what I've needed to do, but I've never coded large projects day in and day out. So I need a good well rounded refresher project to get me dreaming in C code.

    Could anyone recommend a project that would take maybe up to 30 hours, total, and cover a lot of the fundamentals of C++? Or perhaps you know a website or something with a bunch of good projects to do? I don't want to jump around writing snippets of code, but would rather just focus on one project in order to get back into thinking in C code. Right now I write Matlab code all the time, so I do have some adequate general programming skills.

    Thanks!

  2. #2
    Join Date
    Aug 2005
    Posts
    11

    Re: Can someone recommend a project?

    For what it's worth, I suggest you make a small video game

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Can someone recommend a project?

    Quote Originally Posted by jfrog
    Right now I write Matlab code all the time, so I do have some adequate general programming skills.
    Not that I think you'll make a good C++ programmer but maybe you could be of some value knowing Matlab.

    Why don't you spend the final hours reading up on the C++/Matlab interface?

  4. #4
    Join Date
    Jul 2008
    Posts
    18

    Re: Can someone recommend a project?

    Quote Originally Posted by _uj
    Not that I think you'll make a good C++ programmer but maybe you could be of some value knowing Matlab.
    Hey, I think I could become a good c++ programmer.

    Why don't you spend the final hours reading up on the C++/Matlab interface?
    I'm already pretty good with compiling .mex files. I use them pretty often.

    For now, I decided to go with just doing some assignments and final projects that teachers have posted on their websites for programming courses. Not extremely challenging, but better than nothing.

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can someone recommend a project?

    Quote Originally Posted by jfrog
    Hello,

    I have an interview for a job where I know I will be drilled on my C++ abilities. However, I will not be expected to be a guru, by any means. I'm an EE and I have on and off experience with C and C++.
    Forget doing a project... make sure that you know the basics. To get an idea of where you are, don't use any reference material and answer the following questions.

    MEMORY MANAGEMENT
    1) What are the differences between malloc/free, new/delete and new[]/delete[]?
    2) Why should you prefer new/delete and new[]/delete[] in C++ over malloc/free?
    3) What is wrong with merely using malloc/free on class objects?
    4) What is the difference between a stack object and a heap object?
    5) When should you use delete?

    CLASSES
    1) What is a constructor?
    2) What is a destructor?
    3) What is a constructor initialiser list?
    4) When should you overload the destructor?
    5) When should you overload the assignment operator?
    6) What does the virtual keyword mean? When should it be used?

    GENERAL
    1) What are the differences between pointers and references?
    2) What is the difference between const* char and const char*?
    3) Describe the meaning of const * char*
    4) Is this a safe piece of code:
    Code:
    int& function()
    {
      int value = 5;
     return value;
    }
    If not, then why not?
    5) What are the differences between the following declarations/definitions?
    a) int var(0);
    b) int var = 0;
    c) int var();
    d) int var;

    ADVANCED MEMORY MANAGEMENT
    1) What are the responsibilities of, and relationships between the new operator, operator new and placement new?
    2) What are the responsibilities of, and relationships between the delete operator and operator delete?
    3) Before using operator delete, what must you first do to the object pointed to through the pointer? What could potentially happen if you fail to do this?

  6. #6
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can someone recommend a project?

    Once you have answered the above, you might want to take a look at:

    http://www.parashift.com/c++-faq-lite/

  7. #7
    Join Date
    Jul 2008
    Posts
    18

    Re: Can someone recommend a project?

    Quote Originally Posted by PredicateNormative
    Forget doing a project... make sure that you know the basics. To get an idea of where you are, don't use any reference material and answer the following questions.
    Hey, this is awesome. Thanks. I know I am not going to impress here, but here goes nothing. This is with absolutely no googling or references:

    MEMORY MANAGEMENT
    1) What are the differences between malloc/free, new/delete and new[]/delete[]?
    malloc and free are the C dynamic memory allocation functions. When using malloc you have to keep in mind the size of the data type you are using and calculate the amount of memory you need yourself. I forget the exact syntax.

    new/delete is C++ dynamic memory allocation, and it's a little easier to use since you don't have to calculate the memory yourself:

    Code:
    int *a;
    int i;
    cout << "Enter size of your array" << endl;
    cin >> i;
    a = new int[i];
    //Do stuff
    delete [] a;
    new/delete is for allocating memory for one object, new[]/delete[] is for an array.

    2) Why should you prefer new/delete and new[]/delete[] in C++ over malloc/free?
    I'm sure there are better reasons, but like I said above, the syntax is easier to use. But infering from your next question, I suppose malloc/free doesn't work with class objects?

    3) What is wrong with merely using malloc/free on class objects?
    Don't know; never tried it. When using C++ I never used malloc/free.

    4) What is the difference between a stack object and a heap object?
    I think I remember a stack as being a thing where you push stuff on and pop stuff off. I don't remember anything about heap objects.

    5) When should you use delete?
    When you are finished using the variables/objects you allocated memory for.

    CLASSES
    1) What is a constructor?
    The member function of a class that is run when an object is created. It's usually used to initialize variables for the object and whatnot.

    2) What is a destructor?
    The member function of a class that is run when the object is destroyed. (called when you leave a function that the object was created in or when you delete it, if it was dynamically created.)

    3) What is a constructor initialiser list?
    Hmmm... Never heard of that term. Is it the arguments you pass to the constructor?

    4) When should you overload the destructor?
    I don't think you can. It takes no arguments.

    5) When should you overload the assignment operator?
    When you need to handle the assignment of a different data type to an object. For example, say you have a vector class. A scalar would be a special case of a vector (in 1 dimension) that you might want to allow. You could have the normal assignment operator for assigning another vector to the vector on the left side of =. And then overload it for assigning a scalar (int, float, etc). So that:

    myVec = otherVec;

    and

    myVec = 3;

    are both valid.

    6) What does the virtual keyword mean? When should it be used?
    The virtual keyword allows a member function of a class to be rewritten by a sub class. Sort of like overloading a function. If you set a virtual function to 0 you make it a pure virtual function and the class becomes abstract (can't create an object, but you can have a pointer to the abstract class).

    GENERAL
    1) What are the differences between pointers and references?
    References are "safer." You can't change the address like with a pointer. So if you have something like:

    Code:
    void myfunc(int & myint){
      myint=3;
    }
    you change the actual value of whatever was passed, but you can't do pointer arithmetic and move around in memory.

    2) What is the difference between const* char and const char*?
    Never used anything like this, but here is my guess:

    "const* char" is a pointer to a constant char

    while

    "const char*" is a constant pointer to a character.

    In other words, for the first one you can change the address of the pointer, but for the second one you can't.

    3) Describe the meaning of const * char*
    Using my logic above, it would be a constant pointer to a constant char. Again, I'm sort of guessing here.

    4) Is this a safe piece of code:
    Code:
    int& function()
    {
      int value = 5;
     return value;
    }
    If not, then why not?
    Hmmm... Looks bad to me. You have declared it to return a reference, but you are returning an int, not a reference to an int. Although I've used pointers quite a bit (more C experience than C++), I've done little with references and I couldn't tell you exactly what would happen.

    5) What are the differences between the following declarations/definitions?
    a) int var(0);
    b) int var = 0;
    c) int var();
    d) int var;
    a) and b) are the same. They declare an integer and initialize it to 0.

    c) and d) are the same. They declare an integer, but do not initialize it. if you do cout << var; there is no telling what you might get.

    ADVANCED MEMORY MANAGEMENT
    1) What are the responsibilities of, and relationships between the new operator, operator new and placement new?
    I've only used new and delete. Don't have a clue what you're talking about.

    2) What are the responsibilities of, and relationships between the delete operator and operator delete?
    Again, never heard of "operator delete".

    3) Before using operator delete, what must you first do to the object pointed to through the pointer? What could potentially happen if you fail to do this?
    Obviously, without understanding 1) and 2) I don't have an answer.

    So, where do you think I stand? What do you recommend I study/do?

    Thanks!!

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can someone recommend a project?

    Ok, I'll not give full answers, since that will take more time than I have. But these would have been the kind of answers that I would have been looking for in an interview:

    Code:
    MEMORY MANAGEMENT
    1) What are the differences between malloc/free, new/delete and new[]/delete[]?
    2) Why should you prefer new/delete and new[]/delete[] in C++ over malloc/free?
    3) What is wrong with merely using malloc/free on class objects?
    4) What is the difference between a stack object and a heap object?
    5) When should you use delete?
    1) Differences between malloc/free, new/delete and new[]/delete[]:
    -malloc/free are C functions responsible for dynamic memory management; new/delete and new[]/delete[] are C++ functions responsible for dynamic memory management.
    -if malloc fails to allocate it will return NULL, if new or new[] fail, then by default they will throw an exception (although if you are using an older compiler, you may need to force them to throw an exception, since versions long ago may have returned NULL); this behaviour can be modified by custmising operator new or operator new[].
    - the new and new[] operators call the constructors on objects, malloc does not.
    - delete/delete[] call the destructors on objects, free does not.
    -you must know the size in bytes of the objects you are allocating when allocating with malloc, you do not need to know this information when using new and new[] since it is inferred in the allocation syntax.
    -new/delete are used for allocating/deallocating memory for single objects, new[]/delete[] are used for allocating/deallocating an array of objects of same type. malloc/free can be used for allocations/deallocations of single objects and arrays.

    2) Reasons should you prefer new/delete and new[]/delete[] in C++ over malloc/free:
    - new/delete and new[]/delete[] will construct and destruct the objects they are responsible for allocating/deallocating, whereas malloc/free will not. Since C++ is object orientated, and uses classes which require explicit construction and destruction, this is a very important factor.
    - new/new[] are exception safe, such that if the constructor of an object filling the allocation fails, the memory allocation will be automatically deallocated. In the case of new[], if the nth elements constructor throws, then all objects from 0...n-1 will have their destructors called and the whole allocation will be automatically deallocated.
    - new/new[] are typesafe, they return a pointer of the correct type (Type*), whereas malloc will return a non-type-safe void* pointer.

    3) The wrongness of merely using malloc/free on class objects:
    They only allocate and deallocate the memory, but do not construct/destruct the objects. Therefore if you merely use malloc you will not get a class object back as expected, but only a memory allocation that is the same size as the class object. Similarly if you merely use free on a fully constructed and correctly allocated class object, the destructor of the object will not be called, it will only be deallocated. This means that any member objects of the class object will never have their destructor's called, and if these member objects have dynamically allocated memory themselves, it will be leaked.

    4) The difference between a stack object and a heap object:
    A stack object is an object whose allocation is on the stack, its size can only be specified at compile time (the complete stack is usually limited to 1MB, but can usually be explicitly altered through the compiler or system depending on the OS). A heap object is an object that has been dynamically allocated by malloc/new or new[], the size of the allocation can be specified at runtime. It's location is not the stack but the heap, the size of the heap is limited by the available memory on the system. Objects located on the heap are often referred to as freestore allocations. Note also, that all objects allocated by malloc must be deallocated using free, all allocations made by new must be deleted by delete, and all allocations made by new[] must be deleted by delete[].

    5) When you should use delete:
    - to delete an object allocated by new. Note: You should never use delete to delete and object allocated by new[] or malloc.

    I've run out of time for the moment, I'll try and answer some more later, but take a look at:

    http://www.parashift.com/c++-faq-lit...tore-mgmt.html
    Last edited by PredicateNormative; August 26th, 2008 at 05:36 AM.

  9. #9
    Join Date
    Jul 2008
    Posts
    18

    Re: Can someone recommend a project?

    Thanks for the explanations, PredicateNormative. Much appreciated.

    I've been able to find the answers to most of the questions online, but I'm still sort of puzzled by your advanced memory management questions.

    ADVANCED MEMORY MANAGEMENT
    1) What are the responsibilities of, and relationships between the new operator, operator new and placement new?
    2) What are the responsibilities of, and relationships between the delete operator and operator delete?
    3) Before using operator delete, what must you first do to the object pointed to through the pointer? What could potentially happen if you fail to do this?
    The new operator would be the new keyword, while "operator new" would be the function it calls when you use new?

    I think I understand placement new... It sets things up to point to the exact part of memory that you specify.

    As for your 3rd question quoted above. I can't find anything that answers this. Is the answer calling the destructor?

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Can someone recommend a project?

    operator new and operator delete are similar to the other operator functions which allow you to change the behaviour of a class.

    These ones specify how new & delete will allocate & deallocate the memory.
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Can someone recommend a project?

    Just want to point out some errata in that list of questions.

    Quote Originally Posted by PredicateNormative
    4) When should you overload the destructor?
    5) When should you overload the assignment operator?
    By "overload" do you mean to say "explicitly define"? An assignment operator unlike a destructor may of course be overloaded, but a better question would be to ask when the compiler-generated default is unsuitable... so I'm inclined to think that's what you meant.

    Quote Originally Posted by PredicateNormative
    2) What is the difference between const* char and const char*?
    3) Describe the meaning of const * char*
    I believe the correct syntax is char * const (for a constant pointer to a mutable char) and const char * const (for a constant pointer to a constant char).

    Here's another memory-related question:
    What is wrong with the following class? (Hint: There are two main problems, and one reasonable solution to both of them - use vectors).
    Code:
    class A
    {
    public:
    	A() :
    		a(new int[100]),
    		b(new int[100])
    	{
    	}
    	~A()
    	{
    		delete [] a;
    		delete [] b;
    	}
    private:
    	int * a;
    	int * b;
    };
    Last edited by Hermit; August 27th, 2008 at 09:16 AM.
    - Alon

  12. #12
    Join Date
    Jul 2008
    Posts
    18

    Re: Can someone recommend a project?

    Quote Originally Posted by Hermit
    What is wrong with the following class? (Hint: There are two main problems, and one reasonable solution to both of them - use vectors).
    Code:
    class A
    {
    public:
    	A() :
    		a(new int[100]),
    		b(new int[100])
    	{
    	}
    	~A()
    	{
    		delete [] a;
    		delete [] b;
    	}
    private:
    	int * a;
    	int * b;
    };
    Would the answer be that copying an instance of class A would result in deleting a and b multiple times?

    I can see that as one problem. I can't see the other problem you are hinting at. Mind telling me or hinting at what it is?

  13. #13
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can someone recommend a project?

    Quote Originally Posted by Hermit
    By "overload" do you mean to say "explicitly define"? An assignment operator unlike a destructor may of course be overloaded, but a better question would be to ask when the compiler-generated default is unsuitable... so I'm inclined to think that's what you meant.
    I did indeed mean to write "explicitly define". I only realised I hadn't written that when I read jfrog's answer shortly before I read your post.

    Quote Originally Posted by Hermit
    I believe the correct syntax is char * const (for a constant pointer to a mutable char) and const char * const (for a constant pointer to a constant char).
    I actually meant to write (and thought I had written)


    GENERAL
    1) What are the differences between pointers and references?
    2) What is the difference between char* const and const char*?
    3) Describe the meaning of const char* const*
    4) Is this a safe piece of code: ...


    Thanks Hermit for pointing these errors out.

  14. #14
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can someone recommend a project?

    Sorry I've been busy the last couple of days and haven't had the time to write anything.

    Quote Originally Posted by PredicateNormative
    CLASSES
    1) What is a constructor?
    2) What is a destructor?
    3) What is a constructor initialiser list?
    4) When should you overload the destructor?
    5) When should you overload the assignment operator?
    6) What does the virtual keyword mean? When should it be used?
    1) Constructor
    - Builds an object (in this case a class) from scratch.
    http://www.parashift.com/c++-faq-lite/ctors.html

    2) Destructor
    Used to release any resources allocated by the object.
    http://www.parashift.com/c++-faq-lite/dtors.html

    3) Initialiser list
    Used to call the constructors of the class members with the specified values/objects.

    4) Overload the destructor
    You can't. But as Hermit pointed out, I actually meant "explicitly define". You should explicitly define the destructor:
    a) When you intend the class to be used as a base class.
    b) To release any resources allocated by the class.

    5) Overload the assignment operator
    You can overload the assignment operator to allow assignments from different types where it makes sense to do so. However, again, as Hermit pointed out, I actually meant "explicitly define", but as Hermit said, the question would have been better phrased "When the compiler-generated default assignment operator unsuitable?" The answer to this is when there are any resources allocated by the class (an explicitly defined assignment operator can be written to copy any resources allocated by the class being copied).

    6) What does the virtual keyword mean? When should it be used?
    The virtual keyword allows derived classes to replace the implementation (.e.g function) marked virtual provided by the base class, such that a base pointer to a derived object will call the derived function implementation instead of the function base implementation.

    Quote Originally Posted by PredicateNormative
    GENERAL
    1) What are the differences between pointers and references?
    2) What is the difference between const* char and const char*?
    3) Describe the meaning of const * char*
    4) Is this a safe piece of code:
    Code:
    int& function()
    {
      int value = 5;
     return value;
    }
    If not, then why not?
    5) What are the differences between the following declarations/definitions?
    a) int var(0);
    b) int var = 0;
    c) int var();
    d) int var;
    1) What are the differences between pointers and references?
    -pointers can be reassigned, references cannot
    -pointers need to be dereferenced to get to the underlying object, references do not
    -references should be treated as being 'the object', pointers should not.
    -pointers should be null checked before use, references should not be null checked (Although it is technically possible to create a null reference, it should never be done - it's just plain wrong).
    -pointers can be incremented/decremented, references cannot

    2) What is the difference between const* char and const char*?
    The first, causes a compile error, the second in is a non-const pointer to a const piece of data.
    That said, the question should have been:
    What is the difference between char* const and const char*?
    The first, is a const pointer to a non-const piece of data, and the second is a non-const pointer to a const piece of data.

    3) Describe the meaning of const * char*
    A compile error!
    The question should have been:
    Describe the meaning of const char* const*
    This is a non-const pointer to a const pointer to a const piece of data.

    4) Is this a safe piece of code:
    Code:
    int& function()
    {
      int value = 5;
     return value;
    }
    No, the return is a reference to a local function variable that will have been popped off the stack at the close of the function body. In other words, by the time the client can use the return, it will have been deleted. Therefore attempting to read or write to the returned reference will yield undefined behaviour.

    5) What are the differences between the following declarations/definitions?
    a) int var(0);
    b) int var = 0;
    c) int var();
    d) int var;
    The first and second and fourth are all initialisations; the first being direct initialisation, the second being copy initialisation, and the fourth default initialisation. The third one is actually a declaration of a function called var, that has a return type of type int. Note that the second is not assignment, but always initialisation. Also note that depending on the compiler, copy initialisation is not guaranteed to be as efficient as direct initialisation.
    [/QUOTE]

    Quote Originally Posted by PredicateNormative
    ADVANCED MEMORY MANAGEMENT
    1) What are the responsibilities of, and relationships between the new operator, operator new and placement new?
    2) What are the responsibilities of, and relationships between the delete operator and operator delete?
    3) Before using operator delete, what must you first do to the object pointed to through the pointer? What could potentially happen if you fail to do this?
    1) The new operator uses operator new to make the memory allocation, and placement new to construct the object in the location allocated by operator new. The FAQ Lite gives the following piece of code to show fashion after which the new operator may be coded.
    Code:
     // Original code: Fred* p = new Fred();
     Fred* p = (Fred*) operator new(sizeof(Fred));
     try {
       new(p) Fred();       // Placement new
     }
     catch (...) {
       operator delete(p);  // Deallocate the memory
       throw;               // Re-throw the exception
     }
    2)What are the responsibilities of, and relationships between the delete operator and operator delete?
    The delete operator first calls the objects destructor and then calls operator delete to deallocate the memory that was allocated for the object. The FAQ Lite gives the following piece of code to show how the delete operator may be coded:
    Code:
     // Original code: delete p;
     if (p != NULL) {
       p->~Fred();
       operator delete(p);
     }
    3) Before using operator delete, what must you first do to the object pointed to through the pointer? What could potentially happen if you fail to do this?
    a)Call the destructor. b) If you fail to call the destructor then any resources allocated by the object being deleted may (and in all probability will) be leaked (along with any resources the resources have allocated etc.).

  15. #15
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Can someone recommend a project?

    Quote Originally Posted by jfrog
    Would the answer be that copying an instance of class A would result in deleting a and b multiple times?

    I can see that as one problem. I can't see the other problem you are hinting at. Mind telling me or hinting at what it is?
    Correct... The other problem has to do with exception safety, and while it isn't all that critical it is still considered a defect. If the allocation for A::b fails, throwing std::bad_alloc, then the memory allocated for A::a is leaked (I think the ordering here depends on the order of declaration of the member variables and not the order of appearance in the initializer list).
    - Alon

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)