Very simple pointer question

This is a discussion on Very simple pointer question within the C++ Programming forums, part of the General Programming Boards category; Ok, I have an extremely simple program. This compiles and (as far as I know) it works fine. Code: #include ...

  1. #1
    Shadow12345
    Guest

    Very simple pointer question

    Ok, I have an extremely simple program. This compiles and (as far as I know) it works fine.
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main() {
    	int localvariable = 5;
    	int age = 16;
    	int * pVariable = &localvariable;
    	cout << "Localvariable is " << localvariable << endl;
    	cout << "*pVariable points to localvariable " << endl;
    	cout << "*pVariable is " << *pVariable << endl;
    	cout << "Now I want to change what *pVariable points to " << endl;
    	pVariable = &age;
    	cout << "Age is " << age << endl;
    	cout << "*pVariable = &age" << endl;
    	cout << "*pVariable is " << *pVariable << endl;
    	getch();
    	
    	return 0;
    }
    simple enough. However I never called delete on the pointer, aren't you supposed to always do that? However in this code, when I call delete on pVariable, I get a runtime error
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main() {
    	int localvariable = 5;
    	int age = 16;
    	int * pVariable = &localvariable;
    	cout << "Localvariable is " << localvariable << endl;
    	cout << "*pVariable points to localvariable " << endl;
    	cout << "*pVariable is " << *pVariable << endl;
    	cout << "Now I want to change what *pVariable points to " << endl;
    	pVariable = &age;
    	cout << "Age is " << age << endl;
    	cout << "*pVariable = &age" << endl;
    	cout << "*pVariable is " << *pVariable << endl;
    	
    	delete pVariable; //<--THIS IS THE ONLY CHANGE
    	
    	getch();
    	
    	return 0;
    }
    Why does the top code work, but the bottom code doesn't?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No!!

    You only delete pointers to allocated memory! You are not actually deleting the pointer (it can be reused), just the memory...

    int i = 10;
    int *p = new int;

    delete p;

    p = new int;

    delete p;

    p = &i;

    ...


    But warning:

    int i = 10;
    int *p = new int;

    p = &i; //...memory leak! Delete old memory first!

    ...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple pointer question
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 01:05 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM

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