|
-
Apr 18th, 2001, 10:33 PM
#1
Thread Starter
Hyperactive Member
-
Apr 19th, 2001, 06:33 AM
#2
Hyperactive Member
I use pointers... for strings!
char *variable;
The "*" means a pointer.
Designer/Programmer of the Comtech Operating System(CTOS)
-
Apr 19th, 2001, 06:35 AM
#3
Frenzied Member
Do you realy use them in your programs?
Try windows programming and see.
-
Apr 19th, 2001, 08:42 AM
#4
Thread Starter
Hyperactive Member
OK
yeah ok, that was one of the answers I was waiting for  
Do you use them in winprog to access memory access indirectly? to use the free store?
Amon Ra
The Power of Learning.
-
Apr 19th, 2001, 10:15 AM
#5
Frenzied Member
Well yes they are used fpr memory access, but mostly because nearly all API functions require pointers as parameters.
-
Apr 19th, 2001, 12:04 PM
#6
Monday Morning Lunatic
They're used in most C/C++ programs. (Technically all, because functions are used as pointers ).
And you can't just put a value into a pointer and read the memory at that location, because the memory protection prevents you using memory outside of your program. You'd actually have to allocate extra memory, or know EXACTLY where your program is in memory.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 19th, 2001, 04:10 PM
#7
Guru
Pointers are used a lot in C++ for polymorphic purposes.
Code:
Base* x = new Derived;
And stuff like that.
Last edited by Yonatan; Apr 28th, 2001 at 04:17 PM.
-
Apr 19th, 2001, 05:39 PM
#8
Thread Starter
Hyperactive Member
:)
Amon Ra
The Power of Learning.
-
Apr 19th, 2001, 05:45 PM
#9
Monday Morning Lunatic
To expand on what Harry said, the polymorphism he was talking about was how you can use one pointer to do something to a set of classes.
Code:
class Shape {
public:
void Draw() { }
};
This is your base class Shape. From it, we derive two classes Circle and Square:
Code:
class Circle : public Shape { // Inherit
public:
void Draw() { /* Draw circle */ }
};
class Square : public Shape {
public:
void Draw() { /* Draw square */ }
};
Then, we can do this neat thing:
Code:
Shape *pShape;
Circle TheCircle;
Square TheSquare;
pShape = &TheCircle; // Get address
pShape->Draw(); // Calls Circle::Draw
pShape = &TheSquare;
pShape->Draw(); // Calls Square::Draw
Is that cool or is that cool?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 20th, 2001, 05:54 AM
#10
Guru
Originally posted by parksie
To expand on what Harry said
WHO??? 
Code:
class Shape {
public:
void Draw() { }
};
I'm quite sure that the Draw function in the base class has to be virtual.
-
Apr 20th, 2001, 08:45 AM
#11
Thread Starter
Hyperactive Member
-
Apr 20th, 2001, 09:21 AM
#12
Thread Starter
Hyperactive Member
Again
Tell me if I am wrong, but do pointers and references allow
slightly faster programs? and cost less memory? (as the vars referred to are not copied
on the stack when a function is called). I just want to be sure on this Thanks
Amon Ra
The Power of Learning.
-
Apr 20th, 2001, 09:24 AM
#13
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 20th, 2001, 09:25 AM
#14
Monday Morning Lunatic
Re: Again
Originally posted by Amon Ra
Tell me if I am wrong, but do pointers and references allow
slightly faster programs? and cost less memory? (as the vars referred to are not copied
on the stack when a function is called). I just want to be sure on this Thanks
Only in certain cases. Because, if you pass a pointer, it still has to copy 4 bytes onto the stack (the size of a 32-bit pointer). Passing parameters as pointers only helps if they're large structures or classes.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 20th, 2001, 10:16 AM
#15
Guru
Re: Re: Again
Originally posted by parksie
Passing parameters as pointers only helps if they're large structures or classes.
It also helps if you want the original parameters to be changed by the function.
For example:
Code:
void Swap1(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void Swap2(int& a, int& b)
{
int t = a;
a = b;
b = t;
}
// ...
int x = 5, y = 7;
Swap1(&x, &y);
Swap2(x, y);
Incidentally, the code generated by the Swap1 and Swap2 functions is identical. The only difference is in the prototype, which is pretty meaningless in the compiled EXE. 
Another important comment:
If your function accept a class object by value, and you pass it some object of the specified class, it passes only a copy of the class. This means the class's copy constructor is called when the function starts, and the class's destructor is called when the function ends.
This, of course, doesn't occur if the function accepts the object by reference or by pointer.
However, it can generate very hard-to-find bugs, such as this one:
Code:
#include <iostream.h>
class PointerToInt
{
private:
int* pInt;
public:
int GetValue();
void SetValue(int);
PointerToInt();
~PointerToInt();
};
int PointerToInt::GetValue()
{
return *pInt;
}
void PointerToInt::SetValue(int nValue)
{
*pInt = nValue;
}
PointerToInt::PointerToInt()
{
pInt = new int;
}
PointerToInt::~PointerToInt()
{
delete pInt;
}
void PrintByVal(PointerToInt x)
{
cout << x.GetValue() << endl;
}
void PrintByRef(PointerToInt& x)
{
cout << x.GetValue() << endl;
}
int main()
{
PointerToInt i; // pointer created
i.SetValue(5); // pointed value is changed
PrintByRef(i); // ok
i.SetValue(17); // pointed value is changed
PrintByVal(i); // pointed value printed ok, but the pointer is ruined :rolleyes:
// no crash yet...
// i.SetValue() and i.GetValue() would crash at this point
return 0; // crash: the pointer was ruined, so the destructor fails
}
Try to find the bug yourself. 
This bug can be fixed by adding a custom copy-constructor:
Code:
#include <iostream.h>
class PointerToInt
{
private:
int* pInt;
public:
int GetValue();
void SetValue(int);
PointerToInt();
PointerToInt(const PointerToInt&);
~PointerToInt();
};
int PointerToInt::GetValue()
{
return *pInt;
}
void PointerToInt::SetValue(int nValue)
{
*pInt = nValue;
}
PointerToInt::PointerToInt()
{
pInt = new int;
}
PointerToInt::PointerToInt(const PointerToInt& pti)
{
pInt = new int;
*pInt = *pti.pInt;
}
PointerToInt::~PointerToInt()
{
delete pInt;
}
void PrintByVal(PointerToInt x)
{
cout << x.GetValue() << endl;
}
void PrintByRef(PointerToInt& x)
{
cout << x.GetValue() << endl;
}
int main()
{
PointerToInt i; // pointer created
i.SetValue(5); // pointed value is changed
PrintByRef(i); // ok
i.SetValue(17); // pointed value is changed
PrintByVal(i); // copy constructor is called - no problems occur
return 0; // no crash
}
Last edited by Yonatan; Apr 20th, 2001 at 10:33 AM.
-
Apr 20th, 2001, 12:05 PM
#16
Monday Morning Lunatic
I didn't mention changing parameters because the question related to passing IN values...but oh well, valid point
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 20th, 2001, 04:25 PM
#17
Thread Starter
Hyperactive Member
Hrmmm..
Code:
int main()
{
PointerToInt i; // pointer created
i.SetValue(5); // pointed value is changed
PrintByRef(i); // ok
i.SetValue(17); // pointed value is changed
-> PrintByVal(i); // pointed value printed ok, but the pointer is ruined [:rolleyes:] // no crash yet...
// i.SetValue() and i.GetValue() would crash at this point
return 0; // crash: the pointer was ruined, so the destructor fails
}
at the arrow, why is the pointer ruined?
Amon Ra
The Power of Learning.
-
Apr 20th, 2001, 04:58 PM
#18
Guru
When you don't build your own copy constructor, the compiler adds the default one.
The default copy constructor simply copies all the member variables from the copied object to the new object.
Consider this:
PHP Code:
PointerToInt i;
This allocates data with the new keyword - because that's what the default constructor of PointerToInt does.
This changes the value pointed by the pointer, to 5 - no problem here.
This passes the address of 'i' to the PrintByRef function.
NOTE: No new instance of PointerToInt is created - the address of the old one is passed.
There are still no problems here.
PHP Code:
i.SetValue(17);
Changes the value to 17 - still ok.
Here's the big deal.
The object is passed by value. To keep from having it changed by the function (because it's by value and mustn't be changed), a new object is created (copied from the old one) and this new object is passed to the function. Nobody cares if this new object is changed because it is only temporary.
As you would expect, the new object's copy constructor is called (with a reference to the old object as a parameter), then the PrintByVal function code is executed, and finally the destructor is called.
At this point (before calling the constructor, function and destructor), the object contains a perfectly good pointer.
When the compiler compiled the class, it didn't find a copy constructor because one was not specified - so it used a default copy constructor which just copies all the members.
This copy constructor is called. The pointer itself (not just the value pointed by it) is copied to the new object.
Then the function code is called - nothing special here.
Finally, the destructor is called. The destructor deletes the pointer. Then the pointer in class 'i', which contains the same value (because it was copied), becomes an invalid pointer - since it was deleted!
Finally, the end of main is reached. This calls the destructor of the local object 'i'. The destructor attempts to delete the pointer, but it was already deleted - resulting in a crash, whose origin is hard to track as you can see.
If you didn't understand the 'new' and 'delete' stuff, look at this:
PHP Code:
int* a;
int* b;
// A
a = new int;
b = a;
// B
*a = 17;
cout << *b;
// C
delete a;
// D
*b = 3;
- Here, only one new int is allocated on the heap. Let's pretend its address is 0x123456. We don't know what it points at.
a and b are both assigned the address of the new int (0x123456).
a = 0x123456 -> ?
b = 0x123456 -> ?
- First, the value pointed by a is changed to 17.
a = 0x123456 -> 17
But don't forget that b has the same value!
b = 0x123456 -> 17
Then the value pointed by b is printed. This is, of course, 17.
- The heap memory 0x123456, which was previously allocated, is now freed.
a = 0x123456 = invalid pointer
But remember that b also has this same value!
b = 0x123456 = invalid pointer
Now, a and b are referred to as "dangling pointers", because they contain a value which is an invalid pointer.
- This code attempts to change the value pointed by b to 3.
However, since the pointer is now invalid, this results in an invalid indirection and a crash.
This program also crashes similarly:
PHP Code:
int* a;
int* b;
a = new int;
b = a;
delete a; // ok
delete b; // error!
Happy crashes
-
Apr 20th, 2001, 07:38 PM
#19
Thread Starter
Hyperactive Member
thank you sooo much!!
Thhanks a lot for your explanation! so basically the very last example crashes because
you want to delete a pointer(or the location it is pointing at) that has already been deleted?
Amon Ra
The Power of Learning.
-
Apr 21st, 2001, 05:31 AM
#20
Guru
Re: thank you sooo much!!
Originally posted by Amon Ra
Thhanks a lot for your explanation! so basically the very last example crashes because
you want to delete a pointer(or the location it is pointing at) that has already been deleted?
You got it
-
Apr 21st, 2001, 11:43 AM
#21
Thread Starter
Hyperactive Member
:):):):):):):)
Thank you very much Now I feel more positive abou starting winprog
Amon Ra
The Power of Learning.
-
Apr 21st, 2001, 11:48 AM
#22
Monday Morning Lunatic
It's not overly taxing, you just need to keep an eye on what you're allocating and where...if only I'd been as diligent when I was learning  
Well...experience is a good teacher but her prices are high 
If you're serious about windows development, get yourself a copy of the Platform SDK from MSDN.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 28th, 2001, 01:41 PM
#23
Fanatic Member
Originally posted by Warmaster199
I use pointers... for strings!
char *variable;
The "*" means a pointer.
How do you use pointers for strings?
Alcohol & calculus don't mix.
Never drink & derive.
-
Apr 28th, 2001, 01:46 PM
#24
Monday Morning Lunatic
Okay...back to basics. Forget the string class. Pure code from now on 
In C&C++, strings are stored in memory as a null-terminated array of characters. This allows the string to be represented as a single pointer, of type char*:
Code:
char *pcStr; // Declare the pointer
pcStr = new char[30]; // Allocate memory and set pcStr to the location
strcpy(pcStr, "Hello!\0"); // Copy in a string and make sure it's terminated well
This allocates 30 bytes in memory to store the string. Then, it copies the string "Hello!\0" in (the \0 is a null character and just forces it to get it right ).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 28th, 2001, 04:13 PM
#25
Guru
parksie: I hope you know that a string literal is a macro for an array of null-terminated characters
Code:
"string" == { 's', 't', 'r', 'i', 'n', 'g', 0 }
So
Code:
"Hello\0" == { 'H', 'e', 'l', 'l', 'o', 0, 0 }
An extra null-terminator isn't really necessary is it 
Oh and one more thing: If you do create a string on the heap (by using new) don't forget to delete it:
Code:
char* str;
str = new char[17];
// ...
delete[] str;
An alternative is to create it on the stack:
Code:
char str[17] = "Hello"; // = { 'H', 'e', 'l', 'l', 'o', 0 };
// 'new' and 'delete' unnecessary
Wynd: If this all is confusing, un-forget the string class
-
Apr 28th, 2001, 04:17 PM
#26
Monday Morning Lunatic
1: Yes, I do know that...I was just being idiotic 
2: There was no point deleting it because that wasn't what the example was showing
3: I much prefer the string class anyway
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|