Thread: pointer which is pointing to new~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    pointer which is pointing to new~

    Hi guys~

    when I read some tutorial files something confused me a bit this moment. the code is as follows:

    int *pPointer;
    void SomeFunction()
    {
    pPointer = new int;
    *pPointer = 25;
    }


    what about the new here ?

    And this line: pPointer = new int;
    is pPointer pointing to the address of new int or to its value ?

    Regards~
    Never end on learning~

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    new is a keyword which lets the compiler know that yolu are dynamically assigning memory while the program runs. Instead of the compiler allocating the memory from the get go, it waits until it reaches that line of code on the program execution and then assigns a memory address to it. It is normally used for making dynamic array and object factories(and things like that).
    And this line: pPointer = new int;
    is pPointer pointing to the address of new int or to its value ?
    Its pointing to its address.
    Last edited by Traveller; 05-09-2002 at 08:27 PM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    YOUR CODE:

    int *pPointer;
    void SomeFunction()
    {
    pPointer = new int;
    *pPointer = 25;
    }


    MY REVISION:


    void function()
    {
    int *pointer;
    pointer=new int;
    pointer=25;
    delete pointer;
    }

    void charpointer()
    {
    char *pointer;
    pointer=new char[25];
    pointer='\n';
    delete [] pointer;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Golden Bunny, your revision is wrong. Your aren't dereferrencing the pointer before you assign a value to it. Also, after you delete a pointer created with new you need to set it to NULL.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    why set it to NULL? it compiles fine........will it decrease the program's overall memory?(diskspace)
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Traveller
    new is a keyword which lets the compiler know that yolu are dynamically assigning memory while the program runs. Instead of the compiler allocating the memory from the get go, it waits until it reaches that line of code on the program execution and then assigns a memory address to it. It is normally used for making dynamic array and object factories(and things like that).


    Its pointing to its address.

    address of new, let me see ...............

    is it a dynamic one ?
    Never end on learning~

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    I'm sorry, if you're trying to be sarcastic I'm totally missing something, otherwise I'm just not getting what you're getting at.

    edit:

    If I'm wrong with something please feel free to correct me.

  8. #8
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Golden Bunny
    why set it to NULL? it compiles fine........will it decrease the program's overall memory?(diskspace)
    You're kidding, right? Are do you have absolutely no clue? What do you think happens if you try to reuse a pointer that was deleted but not set to NULL?
    Last edited by jdinger; 05-09-2002 at 08:49 PM.

  9. #9
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by black



    address of new, let me see ...............

    is it a dynamic one ?
    Yes. That's why it's called a dynamically allocated pointer.

    If you're still confused ( as GB obviously is ) read this.

  10. #10
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Wink

    Originally posted by jdinger


    Yes. That's why it's called a dynamically allocated pointer.

    If you're still confused ( as GB obviously is ) read this.
    I'd really appreciate ur explaination, Jdinger~

    I'll check the page u pointing to and back with questions several hours later, aha~
    Last edited by black; 05-10-2002 at 12:34 AM.
    Never end on learning~

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    int main()
    {
     int n = 12;
     int *np;
     np = &n;
    
     int *created = new int;
    
     *created = 1000; 
    
     printf("\"np\" points to: %i, which is of course: %i  \n\n", *np, n);
    
     printf("\"created\" points to: %i, so where is the variable it points to???\n\n", *created);
    
    
     created = np; //...Oh no, the integer "1000" is now lost forever...
     printf("Re-attaching \"created\" to something else...\n\n");
    
    
     printf("\"np\" points to: %i, which is of course: %i  \n\n", *np, n);
    
     printf("Now \"created\" also points to: %i, so where is the variable it pointed to before?\n  Can you see where it went?\n\n", *created);
    
    
    
    getch();
    
    return 0;
    }

  12. #12
    Unregistered
    Guest
    why set it to NULL? it compiles fine........will it decrease the program's overall memory?(diskspace)
    Well, what you are doing with the delete keyword is freeing the location that pointer is pointing to. Even though the pointer was "deleted" it still exists, only now it points to an invalid location. Most of the time this isn't a problem, but it can cause some big problems when it does happen, so it's a good habit to get into.

    MacLeod

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    >will it decrease the program's overall memory?(diskspace)

    Memory is not diskspace, it is RAM
    "There are three kinds of people in the world...
    Those that can count and those that can't."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM