|
-
Sep 8th, 2001, 11:33 PM
#1
Thread Starter
Fanatic Member
Question about deleting pointers
Say you have this code:
Code:
float *Pi = new float;
*Pi = 3.141592;
delete Pi;
Why wouldn't you just use this:
Code:
float Pi = 3.141592;
Alcohol & calculus don't mix.
Never drink & derive.
-
Sep 9th, 2001, 12:50 AM
#2
You could. Using a pointer in that situation is just dumb. A much better example would be:
Code:
float* floats = new float[1000];
Now you have, basically, an array of floats. Then you can delete it as above. Another example is:
Code:
myStruct* strct = new myStruct;
SomeFunc(strct);
...
where sizeof(myStruct) > 4
in this case, you are passing a pointer(DWORD), instead of the entire struct, which increases speed. You can also create pointers to functions, which help a lot when doing some things (callback functions). Its all in knowing when to use a pointer. In your example, there is just no reason. But other times, is speeds execution time up, by not haveing to copy data from location to location.
Z.
-
Sep 9th, 2001, 05:06 AM
#3
Monday Morning Lunatic
One thing you missed:
Code:
float *i = new float;
delete i;
float *j = new float[100];
delete[] j;
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
-
Sep 9th, 2001, 07:27 AM
#4
more reasons: the C++ STL library
Code:
#include <vektor>
using namespace std;
class CParentClass
{ // members
}
class CFirstChildClass : public CParentClass
{ // Blabla
}
class CSecondChildClass : public CParentClass
{ // Whatever
}
void SomeFunc()
{
CParentClass *pFC = new CFirstChildClass;
// ask for user input and fill class members
// insert the pointer into a vektor <CParentClass*> object that is always available
}
void SomeOtherFuncThatIsCalledAtAnEntirelyOtherTime()
{
// you can now access the object
}
Advantages:
1) Polymorphic access: It doesn't matter which child class the contents of the vektor are.
2) You still have exactly the object you created, not a copy as if you were using vektor<CParentClass>
3) If you used CFirstChildClass obj; and inserted &obj into the vektor, obj would be out of scope in SomeOtherFunc... and most probably deleted.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 9th, 2001, 10:22 AM
#5
Monday Morning Lunatic
vector not vektor 
You don't need the STL to use virtual inheritance, which is what goes on here:
Code:
class Parent {
public:
virtual void Func() = 0;
};
class ChildA : public Parent {
public:
void Func() { cout << "Die!" << endl; }
};
class ChildB : public Parent {
public:
void Func() { cout << "Live!" << endl; }
};
void somecode() {
Parent *ptr;
ChildA *thing = new ChildA;
ChildB *other = new ChildB;
ptr = thing;
ptr->Func();
ptr = other;
ptr->Func();
delete thing;
delete other;
}
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
-
Sep 9th, 2001, 12:52 PM
#6
true, but this way I can at the same time show the "no copy" and the polymorphic advantages.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 9th, 2001, 12:54 PM
#7
Monday Morning Lunatic
Okay then I just wouldn't mix templates in if they're still on the "*** do pointers do" stage 
Not quibbling, though
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
-
Sep 9th, 2001, 01:01 PM
#8
Yeah, maybe write an essay about the difference between heap and stack?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 9th, 2001, 01:03 PM
#9
Monday Morning Lunatic
Aaaargghhh yeah!
Damn that really messed me up when I was learning. Hmm...perhaps another FAQ entry?
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
-
Sep 9th, 2001, 01:31 PM
#10
It's just that I don't have time for it at the moment.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 9th, 2001, 05:44 PM
#11
Thread Starter
Fanatic Member
Originally posted by parksie
Okay then I just wouldn't mix templates in if they're still on the "*** do pointers do" stage
Heh, that's pretty much me. I know the basics of what they do, but I'm still a little fuzzy on it.
Alcohol & calculus don't mix.
Never drink & derive.
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
|