New Reply

Jumping straight into C++ without programming knowledge

 
Share Thread Thread Tools
Dec13-12, 07:46 PM   #18
 

Jumping straight into C++ without programming knowledge


Quote by coalquay404 View Post
I'm asking how you handle creation of objects at runtime without using pointers.
Heap objects are instantiated automatically for you by declaration so you don't have to get a pointer to one and then call new, you can just start using it, e.g.

Code:
int main()
{
  CSomething foo;
  foo.dostuff();
  return 0;
}

And of course you can pass in parameters to the constructor.
Code:
int main()
{
  CSomething foo(1,2,3,4);
  foo.dostuff();
  return 0;
}
This last bit is called RAII is a central concept in C++, though due to its roots in C, you're free to circumvent it at any time by using a pointer and calling new().
 
Dec13-12, 09:06 PM   #19
 
Recognitions:
Homework Helper Homework Help
The real issue here is not the langauge (C++) but instead the class itself and the progression from the simplest of programs to more complex ones as students progress through the class, and how well the instructor and the textbooks explain what is going on (how these programs work).

One issue is some students will want to know how a computer actually works internally, rather than just accept how everything works from a language (like C++) perspective. Those students may later decide to learn the basics of assembly programming, but I don't think this requires taking a full semester class on assembly programming unless there's really an interest in this (and if the school even offers such a class).
 
Dec14-12, 06:28 AM   #20
D H
 
Mentor
Quote by justsomeguy View Post
Heap objects are instantiated automatically for you by declaration so you don't have to get a pointer to one and then call new, you can just start using it, e.g.

Code:
int main()
{
  CSomething foo;
  foo.dostuff();
  return 0;
}
You meant the stack, not the heap. The stack is used for passing parameters, returning results, and for local variables. The heap is used for memory that is allocated dynamically (new/delete in C++, malloc/free in C).

Strictly speaking, there is no stack or heap in C++, or in C for that matter. The stack and heap are how many, but not all, operating systems model memory.

C and C++ have three types of storage:
  • Automatic storage. Your CSomething foo is an automatic variable. In most systems, these live on the stack.
  • Dynamic storage. Objects allocated with new and deallocated with delete have dynamic storage. In most systems, these dynamic objects live on the heap.
  • Static storage. Objects defined at file scope, static class members, and static local variables have static scope. The memory for these objects is created by the compiler; it's part of the executable. Non-primitive static objects (instances of some class or union) are constructed before main starts and destructed after main finishes.

This last bit is called RAII is a central concept in C++
RAII is a very important concept. The acronym stands for the rather meaningless "resource allocation is initialization." Put simply, it means "Put away your toys. All of them." Allocated memory is but one of many resources that need to be "put away". Connections to I/O devices, connections to other computers, other processes also need to be "put away" when the thing that created those connections is done playing with them.

The RAII concept addresses all resources, not just allocated memory. A class that allocates resources for an instance of that class should ensure that those resources are properly "put away" when that class instance goes out of scope. This places a heavier development burden on the author of a class but significant reduces the burden on a user of that class. RAII goes a long ways to solving the memory leak problem, along with a host of other resource-related problems as well.
 
Dec14-12, 08:36 AM   #21
 
You're right of course D_H, my flub on heap/stack. I was trying to not get too deep into just what RAII means in order to answer the question.
 
Dec14-12, 08:49 AM   #22
 
Recognitions:
Homework Helper Homework Help
Quote by D H View Post
Strictly speaking, there is no stack or heap in C++, or in C for that matter. The stack and heap are how many, but not all, operating systems model memory.
What about _alloca() (dynamic allocation from stack, that is freed when a function returns to higher level)?

I'm wondering if we're getting off topic from the original post which was asking if an introduction to programming class using C++ is a good idea. I think once the student gets into this class, either these aspects of programming will be explained, or the OP can ask questions here.
 
Dec14-12, 11:02 AM   #23
D H
 
Mentor
Quote by rcgldr View Post
What about _alloca() (dynamic allocation from stack, that is freed when a function returns to higher level)?
alloca is a non-standard function. It is not part of the C or C++ standard. It's not POSIX, While it is available on many machines, several discourage its use.

There is zero need for alloca in C++. Use std::auto_ptr (C++03), boost::scoped_ptr (Boost), or std::unique_ptr (C++11) instead.


I'm wondering if we're getting off topic from the original post which was asking if an introduction to programming class using C++ is a good idea. I think once the student gets into this class, either these aspects of programming will be explained, or the OP can ask questions here.
Perhaps. The original question was answered quite readily (no problem), then we got dragged into the inevitable language holy wars, and now we're getting down in the technical weeds.
 
Dec14-12, 11:17 AM   #24
 
For my part, I did not mean to start a 'holy war'. I use a lot of languages for different purposes, there aren't many I don't like or can't find a good use for from time to time. I just intended that you can (and people often do) write useful C++ code without ever touching an implicit pointer. I don't think that pointers are "bad" by any stretch of the imagination or even that you shouldn't use them in C++ when they are the best option, just that you don't ever really *need* to.
 
Dec14-12, 11:55 AM   #25

Math 2012
 
Recognitions:
Science Advisor Science Advisor
To get back to the OP's question, the key thing is the structure of the course, not the language it uses.

What you should be learning on a first course IMO is programming. To do any practical work, you have to use some computer language, but it doesn't really matter what language it is.

On the other hand, a lot of books, courses, and web tutorials don't teach programming, they teach a programming language. That's fine if you already know how to program and just need to learn another language, but it's a bad way to start.
 
Dec14-12, 04:45 PM   #26
 
Learning C++ is not that bad IMO.

If you understand the state and flow-control in any language (I know I harp on about these two things in many posts), then no matter if its a web-platform, a mobile platform, a normal PC platform, a micro-controller or embedded system, or anything else, then you'll pick up everything you need to know quickly.

Just learn the flow-control and how the language implements that as well as how the language and the code changes the state-space, how its accessed, and issues regarding its access and you'll be good to go.
 
Dec16-12, 09:01 AM   #27
 
If you go this route, play special attention to the lessons on pointers and memory management, linked lists, structures and the like. You'll sink or swim by whether you learn these topics thoroughly. I almost want to say that a lot depends on the quality of the teacher or textbook. Expect to do a lot of work to learn these foundational topics, but if you do learn them, all other programming later should be easier.
 
New Reply
Thread Tools


Similar Threads for: Jumping straight into C++ without programming knowledge
Thread Forum Replies
Linearity vs. "takes straight lines to straight lines" Calculus & Beyond Homework 1
Recommended programming language (and texts) for middle-school beginner programming Programming & Comp Sci 9
how much programming knowledge does a mathmatician need? General Math 24
Good Phys. Knowledge vs. Good Math Knowledge General Math 2
Transformations taking straight lines to straight lines Differential Geometry 5