CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    What is this statement doing?

    Could someone explain to me in english what this piece of code does.
    Also if you could show equivilant code not using dynamic allocation.

    Code:
    int *myPtr = new int;
    The above seems to be working in my program.

    I previously tried something similar to this and the app kept crashing
    Code:
    int *myPtr;
    
    cout....
    cin>> *( myPtr );  //Store input into the value myPtr points to
    I am writing a simple sorting program that uses pointer notation for everything. Meaning I do not want to do this.

    Code:
    int size;
    int *myPtr;
    
    cout<<"Size : ";
    cin>> size;
    
    myPtr = &size;
    Google is your friend.

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: What is this statement doing?

    Code:
    int *myPtr = new int;
    The right hand side of the above line dynamically allocates a single integer on the heap returns a pointer of type int*. The left hand side of the above line implicitly copy constructs a pointer on the stack using the pointer returned from the new expression. It is the equivalent of writing:

    Code:
    int *myPtr(new int);
    The result is that myPtr now points to a dynamically allocated piece of memory. Incidently, this needs to be released using delete when you have finished with it else you will have a memory leak.
    Code:
    delete myPtr;
    The above line deletes the allocation that myPtr points to, any attempt to access the address myPtr currently still points to will yeild undefined behaviour.

    Now you might see the problem with the code you posted...
    Quote Originally Posted by g.eckert View Post
    I previously tried something similar to this and the app kept crashing
    Code:
    int *myPtr;
    
    cout....
    cin>> *( myPtr );  //Store input into the value myPtr points to
    The problem is that you have constructed an integer pointer myPtr but the pointer has not been initialised or assigned to point to a valid piece of memory. In effect, the pointer could be pointing anywhere. What you need to do is point it to a piece of memory that holds a valid integer. You can do this dynamically, or you can allocate a stack integer variable as follows.

    Code:
    int var = 0;
    int *myPtr = &var; //now myPtr points somewhere valid.
    
    //You can use myPtr from now on to access var
    This aside, why on earth do you want to purposely use pointers to access everything? If you cannot access a single object directly, then the next best thing is accessing it by reference, but if that is not possible (through design constraints), then only as a last resort should you access by pointer. They should NOT be used when there is no need.
    Last edited by PredicateNormative; April 17th, 2009 at 06:13 AM.

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: What is this statement doing?

    Hey thanks for clearing that up. Explained very well.

    This program is for a CPS class and should demonstrate our knowledge of pointers. The directions say, 'write a function to sort an array using a bubble sort algorithm and a program to test it. Use pointer notation for the program'. I assumed this meant use pointers for everything. Now thinking about it more it might mean use pointer notation for the sorting function. I dont know, ill have to check about that but I agree with you it doesnt make sense to use pointers for size variables and such I thought I needed to because of the directions.
    Google is your friend.

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: What is this statement doing?

    I think your original interpretation is correct, it looks like you are meant to use pointer notation for as much as possible. Although this isn't sensible for real world programming, I can understand it as an exercise as long as your lecturer goes on to point out that pointers should only be used when you have to use them (traversing an an array is an example).

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: What is this statement doing?

    Nope, I think "in-memory" sorting is a good example for the use of pointers. You don't move the objects in an array (of whatever sort), you just move pointers to the objects. I. e. you have an array of pointers to objects, sort it, and the pointers just point to different object than before.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured