Whats the point of pointers?

This is a discussion on Whats the point of pointers? within the C++ Programming forums, part of the General Programming Boards category; here is my code using this... Code: #include <iostream> using namespace std; int main() { int a; int *newname = ...

  1. #16
    ------------
    Join Date
    Jun 2005
    Posts
    79
    here is my code using this...
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
            int *newname = new int;
    	*newname = 100;
    	a = 50;
    
    	cout<<"A is equal to "<< a <<"\n";
    	cout<<"Newname is equal to "<< *newname << "\n";
    	*newname = a;
    	cout<<"Newname is now equal to "<< *newname << "\n";
    	delete newname;
    }
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #17
    ------------
    Join Date
    Jun 2005
    Posts
    79
    lol... cant hurt to tell me that again... repeating stuff helps me (especially if it annoys me... lol... its like an annoying song you keep hearing, then you cant get it out of your head... exept while learning its a good thing ) im going to stop going through the tutorials for a while and stick with what ive learned so far (making little codes to drill this in my mind to make sure i dont forget) so that way i dont make some stupid mistakes because i didnt take the time to actually learn it completely... thanks for all the help BTW, this message board is really helpful

    edit:

    i guess ill start tomarrow, im getting tired... going to lay down and watch the NBA finals!

    edit of edit:

    lol its over... ive been watching it on this little TV beside my comp and didnt reolize it was over
    Last edited by Goosie; 06-21-2005 at 10:52 PM. Reason: addition
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Quote Originally Posted by Dae
    Alright,

    Saying *ptr = new int is 'dereferencing' the pointer, which means before it was just ABLE to point to a memory location, now it actually HAS a memory location.. and therefor is just like a normal int, but has special differences. In your example forget that whole 'a' variable you have there, that has nothing to do with your 'ptr' pointer.

    Can you assign a value to "int* ptr;" declaration? (eg. int* ptr = 50) no, because it has no memory to put a definition. So you put 'ptr = new int' which means it gives ptr the memory location a standard int has, and therefor you can now treat it like an int. You would have to set values using '*ptr = 50' or '*ptr = b' (and int b = 50 lets say), and you can change its memory address just like a normal pointer.. so therefor you basicly just have a normal int (a dereferenced int pointer). The only difference I think is that you can "delete" the memory location of the ptr, which would make it a standard pointer again.

    Hope I wasnt too unclear.

    Actually, no, when you type "*ptr = new int "
    your allocating space on the heap as opposed to the stack. And *ptr is your pointer to the space on the heap that you allocated. You type int to tell it how much space you want. in this instance it would allocate i believe 4 bytes(sizeof(int)).

    as another example: you can also do *ptr = new char[10] to allocate space for 10 chars.

    if you dont delete these, they stay on the memory... to many and the computer will run out of virtual memory, because it causes a memory leak. they MUST be deleted.

    some examples of how to delete them:
    for: *ptr = new int;
    use: delete ptr;

    for: *ptr = new char[10];
    use: delete[] ptr;

    it doesnt matter how much space... but if its anything other than a standard size... use delete with brackets.


    although i must warn you. for ever "new" there must be a delete. if there is not a "new" the the data is on the stack... you should NEVER call delete on the stack. because you will create a hole... this will cause problems when the stack unwinds.

    hope this helps...

  4. #19
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by faze
    Actually, no, when you type "*ptr = new int "
    your allocating space on the heap as opposed to the stack.
    Oh thats right, well I meant saying 'new int' gave it the space of a normal int, but forgot to use the word allocating.. and thats the first time I've read the word heap since I read 10 lines of a C tutorial >.<

    So what is dereferencing again?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Im taking an oop class. In oop depending on if its on the stack or the heap we treat them very differently... im not sure if you did or not, but dont take my post offensively, it wasnt ment that way.

    and deref. is when you do:
    Code:
    int someVar;
    someVar = *blah;

    the only major difference... is like i said... this is putting the value on the stack. the *ptr = new int. is assigning a pointer to the heap as well as allocating space on the heap. Im not sure if this can be called JUST deref (or maybe it is).
    Last edited by faze; 06-22-2005 at 01:36 AM.

  6. #21
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by faze
    Im taking an oop class. In oop depending on if its on the stack or the heap we treat them very differently... im not sure if you did or not, but dont take my post offensively, it wasnt ment that way.

    and deref. is when you do:
    Code:
    int someVar;
    someVar = *blah;

    the only major difference... is like i said... this is putting the value on the stack. the *ptr = new int. is assigning a pointer to the heap as well as allocating space on the heap. Im not sure if this can be called JUST deref (or maybe it is).
    Ah okay, thanks!

    Yeah, I'm going to go look up a few things.. the problem with skipping C tutorials and going onto C++ ones is you have to pick these things like heap, stack, arrays, sizeof, and dereferencing concepts as you go along without knowing if its even correct info. I'll just go google them, I really dont want to put in time reading C tutorials that use printf, getch(), and malloc and wierd header files :P
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #22
    ------------
    Join Date
    Jun 2005
    Posts
    79
    lol faze just confused me a little... but im not worried about that stuff yet, im guessing i'll figure that out later (if it isnt part of the tutorial later on at least...)
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    Quote Originally Posted by Dae
    Ah okay, thanks!

    Yeah, I'm going to go look up a few things.. the problem with skipping C tutorials and going onto C++ ones is you have to pick these things like heap, stack, arrays, sizeof, and dereferencing concepts as you go along without knowing if its even correct info. I'll just go google them, I really dont want to put in time reading C tutorials that use printf, getch(), and malloc and wierd header files :P
    yea, theres alot of those, i kina lucked out... i was always facinated by C and after a bunch of tutorials i thought i was good at it. then i took the class in college and realized how much i actually missed.

    after being in a class about(still in it) c++ i dont think i could learn that from a tutorial... all the concepts of a constructor, destructor, operator=, etc. atleast in depth anyway.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 12:59 PM
  3. do pointers affect what they point to directly?
    By PHP in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 01:29 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM

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