Advertisement

Newbie to C++

Started by May 29, 2002 12:47 AM
7 comments, last by Symbios 22 years, 6 months ago
I''m a newbie to C++ and after reading some books on it, i am very confused right now. As far as i can understand, pointers are used to directly access the memory for values. Is there any reasons why we need to directly access the memory? Can''t we juz use a variable to get the value instead? Even when we wanted to manipulate the values, we can use variables to temporary store the values, isn''t it? Can someone pls give me a simple scenerio where pointers are neccessary? Second, constructors are used to initialise functions? Do we have to initialise every function? Why do we need to initialise? Can i juz assign values to my variables in the function manually thus skipping the initialising part. Sorry if these questions have been explained many many times in many many books. I juz can''t grab the concept and need scenerios to help me visualise things.
You usually use pointers to dynamicaly allocate memory. You would do this if you don''t know how much memory has to be allocated before compile time. An example would be if you are loading a file which you don''t know the size of.

I''m not really sure what you mean with a contructor for functions. If you mean the class constructor, you usually initialize the member variables in it, and perhaps some other stuff; for example loading a file that is passed at the constructor during object creation of a file class.
Shabaz
Advertisement
quote: Original post by Symbios
As far as i can understand, pointers are used to directly access the memory for values. Is there any reasons why we need to directly access the memory? Can''t we juz use a variable to get the value instead? Even when we wanted to manipulate the values, we can use variables to temporary store the values, isn''t it? Can someone pls give me a simple scenerio where pointers are neccessary?

To sequentially modify the contents of different variables or different locations in memory (because variables are really only aliases for memory locations), you need pointers. A lot of pointer uses can be circumvented via references, but they''re still a necessity. Don''t bother yourself about them; when you start trying to programmatically solve problems that require them, they''ll become clear to you.

quote:
Second, constructors are used to initialise functions?

Constructors are used to initialize objects. Not functions.
Pointers are used for many purposes. First of all, they are very small, so it is quick to pass a pointer as a function parameter. Also, in C (not C++), I believe it is necessary to use pointers to simulate a call by reference parameter. But simply put, a pointer is a location in memory whose data contains the address of another location in memory. As shabaz states, it is commonly used for dynamic memory handling. ( Think of stacks, queues, linked lists, etc. ). Also, things such as function pointers allow passing of code segments to other functions.

Constructors, as far as i know, are used to initialize member data for classes. As far as funtions go, it is possible in C++ (not sure bout C) to use the format:

void f( int b = 0 )
{
/* code */
}

to initialize the variable b. Basically, if nothing is passed to the function, it automatically sets the variable b to 0 ( as in the call: f(). But if an integer is passed, then it disregards the whole = 0 part, and sets B to the value of the integer.

Hope that answers your questions.

---------------------
The Reindeer Effect
NOOO ... smilies bad.

--------------------
The Reindeer Effect
I agree with Oluseyi. Get the rest of the basics down before looking too hard at pointers. Don''t worry, they''ll confuse you enought later. At first they''re probably going to seem really confusing, but after a while you''ll see that they''re actually a pretty simple concept. Don''t even THINK of looking at function pointers until you know pointers backwards and forwards.

One remark (uh, just ignore this if it confuses you more): pointers are usually used to refer indirectly to some value somewhere else in memory. In other words, you use a pointer to get or more likely change the value of a variable that it "points" to rather than use that variable itself. This is neccessary if you need to change the value of multiple variables in a function (because you can only return 1 value from a function and any variables you pass are only copies of the original). This is not taking references into consideration, which is another topic alltogether that you shouldn''t concern yourself with yet.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Advertisement
pointers are mainly used for dynamic allocation memory. if you want to see the real example, take a look at this simple game''s memory specification:

Minimum System Requirements:
128MB RAM

what does it mean? it doesn''t mean that inside the code of the game it has thousands of long/double/int/float/char variables like this:

long foo[100000000];

that the total amount of bytes used are 128MB. in fact, it only have some variables and some pointers. the pointers are used to load those data files (pictures, sounds) and put them in memory for faster access, and those data files are the ones that eat your 128MB of memory. how big the data files? who knows? it could be 600kb, it could be 100MB, it could be 20MB (just like shabaz told ya).

pointers become more useful once you''re dealing with C++ (classes, objects, etc). but, as Oluseyi said, it''s better to practice pointers first. i''d suggest to start practicing with string (char*).

Life is fair.
-Albert Tedja-
My compiler generates one error message: "does not compile."
Like the earlier poster said, I wouldn''t worry to much about pointers, they become more clear. The first real use for pointers comes into arrays. Lets say you have a progam that has an array like this...

int array[5] and you want to read a file in...
1 7 9 13 4...everything is ok...but what if you had one more number or you had no idea how many more. You could use a pointer to fix the problem.

Now, to your constructor question. Every time a class is called the default constructor is called(if you wrote one). You don''t ever have to use them, you can manually assign the data if you wish. But what if you had a huge amount of calls to the class and the class had a counter. Then you could make the default constructor set the counter to zero for you. Classes are very powerful tools... Hope that helps
When you program Games you''ll get objects that need a lot of memory, if you use pointers to allocate some, then the program (game in this case) will go faster!

This topic is closed to new replies.

Advertisement