Debugging a submission that works well on student's computer but not on my computer
You've said that having an identical environment for everyone would be an incomplete solution, since students' code could still work "by accident" in that environment. One option would be to provide access to two or more sufficiently different environments which would be the same for everyone; such environments could be provided in several ways, for example:


Distribute virtual machine images, with instructions for how students can use them to test their code.
Provide access to virtual machines through a paid service; for example, Codio is specifically designed for use in CS education.
There are some web services such as repl.it, which compile and run your code, and show the results in a console.
Some universities have student-run computing societies offering accounts for students on a shared server.
Different environments may already be available if students are on campus, and the campus has enough different computer labs open for student use at some times.


To catch undefined behaviour when grading submissions, you could test their code in the same two (or more) environments that students have been encouraged to use. Assuming you have a script that can automatically run all submissions, it shouldn't be a lot of extra work to run that script twice.



Another option is to provide students with a software tool which can detect and report undefined behaviour, as discussed in this Q&amp;A on Stack Overflow. Such a tool could also be used to give feedback to students whose submissions fail on your machine during grading; although static analysis and other techniques won't generally be able to detect all kinds of undefined behaviour, the kinds they do detect should cover most or all of the mistakes that students are likely to make.