C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-04-2009, 12:58 PM   #31
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Can you tell me if i made any small errors or if i did it completely wrong?
You made at least three mistakes, and I'll point out the first and third: i was not initialised, so the first printing of *p prints garbage; *p = 0 assigns 0 to the object pointed to by p, but at that point p points to a destroyed object. You mean to write p = 0;

The second mistake will be pointed out by the compiler.

EDIT:
Okay, there is a fourth mistake: x is not initialised either.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-04-2009, 01:01 PM   #32
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
The second mistake will be pointed out by the compiler.
Would this mistake be the redeclaration of int*p?


What do you meen they werent initialized?
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 01:04 PM   #33
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by Ryan0773
Would this mistake be the redeclaration of int*p?
Yes.

Quote:
Originally Posted by Ryan0773
What do you meen they werent initialized?
They contain garbage values. You should write say,
Code:
int i = 123;
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-04-2009, 01:23 PM   #34
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
How could i fix the problem of redeclaration? When the compiler tells me thats the problem it doesn't have a suggestion that could fix it.

Well i guess they were garbage values (if that means that they didn't have any value) but that was just an example, i guess this is what it would look like with values:

Code:
#include <iostream>

using namespace std;

int main()
{
    int i(25 + 4);
    int x(4);
    int *p;
    p = &i;
    cout<< *p <<"\n";
    cout<< *p <<"\n";
    int *p = new int(x);
    p = &x;
    cout<< *p <<"\n";
    delete p;
    p = 0;
    cin.get();
}
the redeclaration error is still there though.


PS: You told me there were four error, the redeclaration, and the two garbage values...whats the last one?

Nvm, the last one was the p = 0 thing

Last edited by Ryan0773; 01-04-2009 at 01:29 PM. Reason: Oops
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 01:29 PM   #35
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by Ryan0773
How could i fix the problem of redeclaration?
Change:
Code:
int *p = new int(x);
to:
Code:
p = new int(x);
Quote:
Originally Posted by Ryan0773
PS: You told me there were four error, the redeclaration, and the two garbage values...whats the last one?
I counted the garbage values as one each.

However, there is another mistake:
Code:
p = &x;
You should not be assigning the address of x to p since p points to dynamically allocated memory.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-04-2009, 01:46 PM   #36
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Talking Thanks alot!

It worked, thanks alot! I still dont fully understand why a pointer would be needed (can they copy large amounts of code or just one simple integer?)but at least i know how to work them. Again thanks alot! Ill be sure to try and get through the other tutorials!
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 01:56 PM   #37
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by Ryan0773
It worked, thanks alot!
No problem, but you might want to post your complete final code in case there are other (undetected) mistakes.

Quote:
Originally Posted by Ryan0773
I still dont fully understand why a pointer would be needed
It is not needed in this case, actually

Quote:
Originally Posted by Ryan0773
can they copy large amounts of code or just one simple integer?
The point is that a pointer points to an object. Copying a pointer is cheap even if copying the object (which could be composed of multiple sub-objects) is not.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-04-2009, 02:11 PM   #38
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
No problem, but you might want to post your complete final code in case there are other (undetected) mistakes.
Code:
#include <iostream>

using namespace std;

int main()
{
    int i(25 + 4);
    int x(4);
    int *p;
    p = &i;
    cout<< *p <<"\n";
    cout<< *p <<"\n";
    p = new int(x);
    p = &x;
    cout<< *p <<"\n";
    delete p;
    p = 0;
    cin.get();
}
This is the final code. It should make it so that the first 2 numbers it shows are 29 and the last one it shows is 4. after that, the new int is deleted and p point to nothing (0) Then it waits for a keypress to exit, correct?


Quote:
Originally Posted by laserlight View Post
The point is that a pointer points to an object. Copying a pointer is cheap even if copying the object (which could be composed of multiple sub-objects) is not.
What do you mean by cheap?
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 02:17 PM   #39
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
There is still that last mistake. This:
Code:
p = new int(x);
p = &x;
cout<< *p <<"\n";
should be:
Code:
p = new int(x);
cout<< *p <<"\n";
Quote:
Originally Posted by Ryan0773
What do you mean by cheap?
Few bytes to copy.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-04-2009, 06:50 PM   #40
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
There is still that last mistake. This:
Code:
p = new int(x);
p = &x;
cout<< *p <<"\n";
should be:
Code:
p = new int(x);
cout<< *p <<"\n";
it doesn't seem to be a major mistake but i guess it would save some time (or is it a big mistake?)

Thanks again for your help!

I still dont really understand what you mean by cheap, is it less work for the computer? Like a shortcut?

Last edited by Ryan0773; 01-04-2009 at 06:57 PM. Reason: Forgot something
Ryan0773 is offline   Reply With Quote
Old 01-04-2009, 08:45 PM   #41
verbose cat
 
Join Date: Jun 2003
Posts: 209
Quote:
Originally Posted by Ryan0773 View Post
I still dont really understand what you mean by cheap, is it less work for the computer? Like a shortcut?
Say you have a class that holds all the points of a 3d object. For a really detailed character, an object of that class might hold several hundred x, y and z coordinate triplets. Now say you want to pass an object of that class to the function that displays it on the screen.

The way variables are passed to functions, a copy is made of what you pass. So, for example, when you pass a simple int to a function, the program makes a copy of that int for the function to use. If you want to pass one of the 3d objects, it would make a copy of all that data, which could end up being several thousand bytes of data.

An alternative is to use a pointer to the object to tell the function where to look for the data. So now the program makes a copy of the pointer for the function to use, and then the function looks at the data that pointer is pointing to. Instead of making a copy of hundreds or thousands of bytes of data, it only has to copy a single pointer and the program is much quicker, especially if you are drawing a 3d scene with dozens of 3d objects having to be passed to the display function several times per second. With a pointer now, the program is working on processing your data rather than spending time copying it all over the place too.

In C++ we prefer to use references, but there are still times when pointers are needed. Under the hood, a reference is doing almost the same thing that a pointer does, but the program takes care of most of the dereferencing and other things that make pointers complicated so you can focus on making the program do what you want.
__________________
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
jEssYcAt is offline   Reply With Quote
Old 01-05-2009, 12:00 AM   #42
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by Ryan0773
it doesn't seem to be a major mistake but i guess it would save some time (or is it a big mistake?)
It is a major mistake. By assigning the address of x to p, p now points to an object that will be automatically destroyed. As such, delete p; becomes a mistake. Furthermore, the object that was created on the previous line cannot be destroyed because there is no way to access it.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-05-2009, 04:25 AM   #43
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Perhaps have a look at: http://cslibrary.stanford.edu/104/
It's quite silly, but it explains the basics.
Also, the tutorial poisons your mind. I don't know why they insist on using T * in C++, but know that it is possible to write T*, as well.
Instead of:
int *p
You can write:
int* p
The later reinforces that the actual type of the variable is int*. If you dereference it, you get an int, but the pointer itself has a type, which is important.

You must also learn the difference between a value and an address. Pointers store addresses. Dereferencing addresses (ie pointers), you get the values.
Everything allocated by new must be freed with delete. It is not automatic. If you assign a new address to a pointer which carries an address from new, that address is lost forever and you cannot free the memory you allocated. That memory is now lost. It will still be there in the program, taking up space, without the ability of being used. It's a memory leak.

Here are a few uses of pointers:
- Dynamic memory allocation. new returns a pointer to the memory allocated (this is usually rare in C++, however, as std::vector & co is typically used for dynamic memory).
- Passing large objects around without copying (although, in C++, typically references are used for this).
- Allow multiple parts of your program to access the same data everywhere, without copying (then it would be multiple instances of that data).
- Allow multiple parts of your program to change some shared data and make sure all other parts get the new data (since all parts point to a single source of the data, if the data is then changed...).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-05-2009, 02:37 PM   #44
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by jEssYcAt View Post
An alternative is to use a pointer to the object to tell the function where to look for the data. So now the program makes a copy of the pointer for the function to use, and then the function looks at the data that pointer is pointing to. Instead of making a copy of hundreds or thousands of bytes of data, it only has to copy a single pointer and the program is much quicker, especially if you are drawing a 3d scene with dozens of 3d objects having to be passed to the display function several times per second.
So the pointer makes the program find the class with the 3d object and instead of the program copying the entire script of the object, using up thousands of bytes, the program gets the information through the pointer and uses it to get the object where you want it. Is that correct?
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 02:39 PM   #45
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
Originally Posted by Ryan0773
So the pointer makes the program find the class with the 3d object and instead of the program copying the entire script of the object, using up thousands of bytes, the program gets the information through the pointer and uses it to get the object where you want it. Is that correct?
Yes, I think you got the gist of the idea.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Tags
basic, basics, c++, lots, noob

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Several Questions, main one is about protected memory Tron 9000 C Programming 3 06-02-2005 07:42 AM
Trivial questions - what to do? Aerie A Brief History of Cprogramming.com 23 12-26-2004 09:44 AM
Questions. anonytmouse A Brief History of Cprogramming.com 4 05-19-2004 02:16 PM
C++ test questions Mister C C++ Programming 9 09-08-2002 12:05 PM


All times are GMT -6. The time now is 12:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22