Tutoring C++ in HEP: Challenges & Common Questions

In summary: The line segment from the beginning of memory to the location where the pointer is pointing is called the "base address". The line segment from the location where the pointer is pointing to the end of memory is called the "offset".2. What are some "gotchas" that students commonly run into when programming in C++?The most common C++ programming errors are the "gotchas." These are errors that are common to C++, but that can lead to program crashes or other problems. Some of the more common C++ gotchas are as follows: 1. Incorrect use of pointers. For example, assigning a pointer to the
  • #1
ChrisVer
Gold Member
3,378
464
Hi people,
This semester I am going to be a tutor for a master course class on C++ in HEP...
I would like to listen from people with more expertise on the field:
1. What do students find (in general) difficult to understand in C++ from your experience with teaching it? In my case I think the most difficult part in C++ was the use of references and pointers -at certain points I am still feeling uncomfortable with using them. So I am trying to refresh, even theoretically, the whole point of refs and pointers (having to do with memory treatments of the computer). What are the most common , you think, questions one might receive on the language?
This could as well deserve to be in Academic Guidance, but I'm also aiming for CS experts. Nevertheless, mods feel free to move it if you find it necessary..
 
Science news on Phys.org
  • #2
I tend to stay away from pointers, still get confused using them after all these years.
 
  • #3
I agree w/ you that pointers are one of the hardest, if not the hardest, things for C programmers to really grasp. As for C++, the whole trio of inheritance, encapsulation and abstraction are fairly easy to grasp as concepts but learning how to USE them in effective ways is a whole 'nother story and can be taught successfully via books and classrooms about as well as it can be taught that way how to ride a bike.
 
  • Like
Likes ChrisVer
  • #4
The notion of method signatures, name mangling of method names, and method overloading and when it's good to use.

The bad idea of overloading operators in such a way as to make them confusing to use. A common use was to overload the plus operator for strings concatenation but some folks said was a mistake since it string concatenation isn't commutative. Most of us though thought the argument was nonsense with respect to strings but made good sense for other operator usage,

The bad idea of multiple inheritance and how it gets implemented in C++ where a class inherits from two parent classes who in turn inherit from the same grandparent class aka the diamond problem giving you two paths to the grandparent member variables which can give you erroneous results and a debugging nightmare. I'm not sure if this is a feature of C++ or vendor specific.

http://www.geeksforgeeks.org/multiple-inheritance-in-c/

The gotchas book might be good to find things that students will get wrong:

http://www.drdobbs.com/two-c-gotchas/184401613

http://stevedewhurst.com/cpp_gotchas/index.html

And the Effective C++ by Scott Meyers is good too:

https://www.amazon.com/dp/1491903996/?tag=pfamazon01-20

And lastly design patterns and when they are good to use:
 
  • Like
Likes ChrisVer
  • #5
ChrisVer said:
Hi people,
This semester I am going to be a tutor for a master course class on C++ in HEP...
I would like to listen from people with more expertise on the field:
1. What do students find (in general) difficult to understand in C++ from your experience with teaching it? In my case I think the most difficult part in C++ was the use of references and pointers -at certain points I am still feeling uncomfortable with using them. So I am trying to refresh, even theoretically, the whole point of refs and pointers (having to do with memory treatments of the computer). What are the most common , you think, questions one might receive on the language?
This could as well deserve to be in Academic Guidance, but I'm also aiming for CS experts. Nevertheless, mods feel free to move it if you find it necessary..
I will be teaching a community college course in C++ starting Monday. This course is the first in a series of three classes, so we just touch on classes at the end of the quarter. Except for the use of the cout and cin streams instead of the C standard library functions, printf() and scanf() and friends, what we'll be doing in this class is not much different from C.

I agree that students have a difficult time with pointers and references, but I don't agree with the advice to stay away from them. Because C and C++ are call-by-value languages, the only way that a function in these languages can modify its parameters is to make them either pointers or references, which are effectively the same things but with slightly different syntax. I don't see how anyone can really get how a function can modify its parameters without understanding how pointers (or references) work.

One way to help get across a good understanding of this mechanism is to show many examples throughout a long stretch of the quarter. A technique that seems helpful to me is to use a debugger and single-step through the function and watch what changes.

Something else that I think is helpful is an image that shows the relationship between a pointer and the memory it points to. In the following image I use something I came up with many years ago that I have seen more recently in a few books.
Pointer.png

The variables, in green boxes, have three things: an identifier, the address in memory of the variable, and the value of the variable. When a pointer variable is initialized with a valid address, the pointer can access the memory at that address, either reading from it or writing to it.
 
  • Like
Likes ChrisVer and jedishrfu
  • #6
Much depends on how you plan to teach C++. It has often been taught as "C with extra stuff", that is, starting with C style. There is an obvious historical basis for this, both in the language's evolution and in the prior experience of those teaching it. Modern C++ has changed very considerably, especially starting with 11, resulting in very different-looking solutions to basic problems.

You gave the example of pointers. There is no reason for a beginner to use raw pointers in a modern C++ program except as part of a learning exercise. Code using new and delete is a good indicator of obsolete treatment, again with the exception of historically-oriented learning exercises, and with the exception of the kind of library code beginners won't be writing. The raw pointer is heavily overloaded semantically, and there is always a more specific alternative available that gives the compiler more of a chance to prove the correctness of your code.
 
  • Like
Likes ChrisVer
  • #7
  • #8
ChrisVer said:
hmm I just read about that here: http://scrupulousabstractions.tumblr.com/post/37576903218/cpp11style-no-new-delete , thanks to you bringing it up. So if I understand well the use of new and delete is now (since C++11) avoided because for each object defined by new you had to delete it at some point?

Roughly yes. It gives more opportunity for error. RAII is preferred in modern C++. In general, we create objects by value, and their destruction is guaranteed when they pass out of scope. When an object must live beyond the lifetime of the scope in which it was constructed, std::unique_ptr and friends are used. For every use of new, you must know exactly when, where, and by whom the delete will be performed. It is a surmountable problem that C programmers have managed for several decades, but it is a source of error nonetheless.

If you are teaching a beginner course, these details will have no context. I am not a professional teacher and take no position on what you ought to teach. I only mention it at all because it can affect the style and content of a beginner course from very early on. There is also the question of what you are teaching: the C++ language, or programming, for which latter purpose you happen to be using C++. In a general course on programming, it makes sense to introduce the pointer concept. In a course on writing modern C++, they can be left out for quite a while.
 
  • #9
I used to teach a two-semester introductory programming course using C++, most recently about 12 years ago. I ended up using pure C++ style, e.g.:
  • stream I/O using cin, cout, fstreams, <<, >>, getline, etc.; not C-style printf(), scanf(), etc.
  • std::string and its member functions for text manipulation, not C-style char* strings and their associated functions.
  • std::vector instead of C-style arrays
  • passing in/out arguments to functions using references, not pointers
The last few years, I ended up not doing anything with pointers at all. At that level, after introducing the C++ features listed above, I found it very difficult to come up with non-hokey situations that required pointers. The course did not include linked lists, trees, etc. Also, the guy who taught our upper-level computer science courses told me that he preferred that I cover basic object-oriented programming (i.e. class design and implementation) instead of pointers, because he covered pointers in his courses anyway (e.g. computer architecture and data structures).

This course was for undergraduates who usually did not know much if anything about programming at all, to begin with. If your students have a significant amount of programming experience already, that may indicate a different approach.
 
Last edited:

1. What is C++ and why is it used in High Energy Physics (HEP) research?

C++ is a programming language used in HEP research for its efficiency, speed, and ability to handle large amounts of data. It is a popular choice among physicists because it allows for the development of complex and high-performance software for data analysis and simulations.

2. What are the challenges of tutoring C++ in HEP?

One of the main challenges of tutoring C++ in HEP is the steep learning curve for beginners. C++ is a complex language with a variety of features and syntax that can be overwhelming for new learners. Additionally, explaining advanced concepts and techniques in a way that is understandable to students with different backgrounds and levels of experience can be challenging.

3. How can I effectively teach C++ in HEP?

To effectively teach C++ in HEP, it is important to have a strong understanding of the language yourself. It is also helpful to have experience in HEP research to better understand the specific needs and applications of C++ in this field. Additionally, using hands-on examples and projects can be helpful in illustrating concepts and allowing students to practice their skills.

4. What are some common questions students have when learning C++ in HEP?

Some common questions students may have when learning C++ in HEP include: How can I optimize my code for better performance? What are the best practices for debugging and troubleshooting? How can I effectively use C++ libraries and frameworks in my HEP research? How can I transition from other programming languages to C++?

5. Are there any resources available for learning C++ in HEP?

Yes, there are many resources available for learning C++ in HEP. Online tutorials, courses, and forums can be helpful for beginners. Additionally, many universities and research institutions offer workshops and classes specifically for C++ in HEP. Collaborating with experienced colleagues and attending conferences and workshops can also be beneficial for learning and staying updated on new developments in C++ for HEP research.

Similar threads

Replies
81
Views
4K
  • STEM Educators and Teaching
Replies
4
Views
2K
Replies
4
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • STEM Career Guidance
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • STEM Academic Advising
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
127
Views
16K
Replies
26
Views
1K
Back
Top