Join 307,124 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,028 people online right now. Registration is fast and FREE... Join Now!
Well Ime starting to get used to OOP now thanks to born2code, thanks for the link... Its actually pretty simple if you use the right tutorial... well it will probably get more complex as I learn more about OOP
Ime kind of learning OOP from different tutorials at the moment also, some tell it easier than others aswell
Much better than pointers though, i find them annoying. Polymath, as a programer your self, could you tell me how important pointers are and are they used alot in programs? becuase Ime not enjoying pointers there kind of confusing, might have to revise over pointers again sometime with a better tutorial than this book ime reading...
Thanks for the help
This post has been edited by xAlanxEnglandx: 29 Jul, 2008 - 03:16 PM
Much better than pointers though, i find them annoying. Polymath, as a programer your self, could you tell me how important pointers are and are they used alot in programs? becuase Ime not enjoying pointers there kind of confusing, might have to revise over pointers again sometime with a better tutorial than this book ime reading...
The importance of pointers depends on the programming language you're using, and what you're using it for.
In general low-level computing terms, pointers are the single dominant issue, since your Processor needs a method of indexing individual bytes in memory, and a pointer is merely an address, or an index, for the computer's memory (Or, perhaps more accurately, a representation of an address)
The reason for high level languages is to escape from this very primitive view of the system, and to allow common operations to be done simply without explicitly using pointers (Compilers do an excellent job of hiding all that), however there always comes a situation where some kind of pointer or index is needed.
Conceptually speaking, you could argue that a variable storing the position of a particular element within an array is like a pointer - Except that its a pointer which is very specific to that array. The variable is acting as a place-holder for something in the array.
in C and C++, the term 'pointer' is used to mean a place holder whose domain is your entire system memory; If it makes things easier, you could think of the memory like a very, very large array of bytes (of which your program will probably only use a small amount - the rest is usually locked out to your program by the OS)
For most programmers, where the issue of pointers start to become confused is partially related to the ugly syntax which C/C++ use to declare and access pointer variables, and also because programmers are ultimately not concerned with where data is stored in memory, they only care that the data is accessible to their program - yet the pointer adds a layer of indirection which needs to be handled carefully.
There's no easy or simple way to become comfortable with the concept of indirect access to data via pointers, except to just practice. Trial and error helps - particularly when it comes to de-mystifying or clarifying whatever assumptions about pointers you may have picked up along the way (Some of which will turn out to be false).
This post has been edited by Bench: 30 Jul, 2008 - 10:20 AM
Well ime learning C++, just revised pointers but is it true you have to assign the memory address to the pointer first before you can display it (in c++)?
Pointers are confusing, took me over a month to get used to them. Pointers are used in most cases when you want to assign something based on user input. Yes, you could use a vector, but vectors tend to make a program quite large (in compiled size, not number of lines) and run slower than a pointer. In general, if i had a program that generated a given number of random numbers:
cpp
#include <iostream> #include <cstdlib> #include <ctime> using namespace std;
get_rand( int low, int high) { int random; random= lower + ( rand() % (high-low) ); return (random); }
int main() { int input_num; int upper; int lower; int * rands; cout << "How many rands do you want? "; cin >> input_num; rands=new int [input_num]; //if we created an array like ' int rands[input_num]; ' here, it would give an error cout << "Bottom of range: "; cin >> lower; cout << "Top of range: "; cin >> upper; for (int i = 0; i < input_num; i++) { rands[i]= get_rand (lower, upper); } for (int m=0; m < input_num; m++) { cout << rands[m] << endl; } cout << "Press <ENTER> to exit. "; cin.get(); delete[] rands; return 0; }
Another good way to use pointers is with a char array for user input, such as in C (which are great for ciphers and letter manipulation, 'cause that's hard to do with a basic string type). Basically, pointers can add a easy way to create an array that isn't of fixed legnth. You can do it with other containers, but i bet you $100 that vectors, basic strings, and other abstractions have a pointer in there somewhere! Rejoice, C is awesome because you can use pointers like in low level languages to add some speed and (believe it or not) simplicity, but you don't HAVE to like in higher level languages. The ONLY way to get used to pointers is to use them.
Well ime learning C++, just revised pointers but is it true you have to assign the memory address to the pointer first before you can display it (in c++)?
Yes, you need to have initialised the pointer first before you can do anything with it. There's no "default" value (it starts out undefined), so good common practise when declaring a pointer is to assign a null value to begin with to record the fact that it doesn't point anywhere. eg, int* ptr = NULL; or int* ptr = 0; (Both do the same thing in C++)
Pointers are confusing, took me over a month to get used to them. Pointers are used in most cases when you want to assign something based on user input. Yes, you could use a vector, but vectors tend to make a program quite large (in compiled size, not number of lines) and run slower than a pointer. In general, if i had a program that generated a given number of random numbers:
Perhaps you could show some evidence to backup that claim about speed, since I would have to disagree - STL containers are highly optimised versus typical solutions involving dynamic arrays for common operations such as insertion and removal of elements. Most C++ programmers recognise that the STL is generally as efficient, or often more efficient than a homebuilt solution. (Provided that you pick the right container for the job of course)
I do agree that writing your own data structures are an excellent way to learn pointers, however, once you have, and feel comfortable with them, the best thing to do is to use the STL, except for the rare case where a homebuilt solution can be shown to have some significant benefits, because the STL is generally simpler to create and maintain, and has performance benefits on top.
QUOTE(polymath @ 31 Jul, 2008 - 04:21 PM)
You can do it with other containers, but i bet you $100 that vectors, basic strings, and other abstractions have a pointer in there somewhere!
Of course they do! behind the scenes, there's generally alot of memory management going on (Typically, placement new is used to allow memory to be reserved or allocated in 'pages', rather than the naive implementations of constant reallocation which you would expect from a homebuild solution)
This post has been edited by Bench: 31 Jul, 2008 - 09:57 AM
It's a common mistake, which I see in about 3 out of 5 threads in this forum.
Here's an example: #include <iostream.h>.h ~ this is what I want to talk about.
For those of you who have studied C in the past, I'm sure you'll recall that when including a standard header file, you will add the .h suffix to the end of the library name. In C++ a lot of these have been deprecated, and there is a common fix to a large number of them.
This is the most common: #include <iostream.h> should be #include <iostream>
Now, when including standard C libraries in a C++ program, you could use #include <stdio.h> However, a large number of names of C standard libraries have been altered for C++ Don't get me wrong, there's nothing wrong with using #include <stdio.h> but there's another way. In this method, we prefix the names of header files with c and remove the .h
Here are some of the common ones: #include <stdio.h> -> #include <cstdio> #include <stdlib.h> -> #include <cstdlib> #include <time.h> -> #include <ctime>
Notice the pattern? This can't be said for all C libraries in C++ but it does apply to the majority.
To add to Bench's comments -- STL *MAY* be slightly bloated in some instances but the STL container classes have been reviewed and re-reviewed by some some of the best programmers in the C/C++ industry. They are much more stable and reliable than the average homebrew. If you really think you can do a better job that these guys then fine write your own, but if you are at that point then you probably have been reading over and learning about techniques developed by these guys and I am sure they would love to have your input on the next version of the standard.
To add to Gabehabe's comments. The <iostream.h> style headers have been depreciated -- meaning that there is a better way to do things than using the <iostream.h> class. This does not mean that you can totally get away from .h headers, but for the standard C/C++ runtime environment you should use the new headers. But for NON-STANDARD headers like conio.h there is no cconio. You will also find yourself using things like <gl/gl.h> don't try <gl/cgl> or <cgl/gl>, or <sys/utime.h> does not become <sys/cutime>.
But ALL of headers that belong to the C++ Standard exclude the .h, and all of the C++ standard headers implementing the C standard headers are now prefixed with a 'c' to denote that they are part of the C library. Note that everything in these libraries are part of the std namespace. So, if you are not using the std namespace you need "std::printf" rather than just "printf" (note I am not sure if this is true an EVERY compiler -- it *should be* but some of the compilers support many of the standard C function natively and don't seem to care about the namespace).
This post has been edited by NickDMax: 18 Aug, 2008 - 08:33 AM
I prefer to see c++ notation used in all C++ code wherever possible. I know that headers like windows.h and allegro.h are not cwindows and callegro, but for all cases where that is the case (like cstdlib, ctime, cstring, and cstdio, just to name a few) to use the c notation instead of the .h notation because the .h can be ambiguous whether you are using depreciated notation of C++ libraries or standard c notation libraries. Besides the fact that cstdlib just makes more sense than stdlib.h when you're using C++ (not to mention it sounds cooler). It is not the c++ stdlib, it is the C stdlib.
For those of you using Visual Studio, I came across a nice tool that detects memory leaks and alerts you when the programs ends if you run in debug mode (F5).
Now, the ideal condition is that you can avoid them in the first place, however, having the resource there is always handy. README inside the zip folder is easy to read and very self explanatory.
Attached File(s) vld_10.zip ( 476.69k )
Number of downloads: 50