Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Pointer, weird error!!

  1. #1
    Join Date
    May 2007
    Beans
    99

    Pointer, weird error!!

    Hi
    I am learning c++ pointers, I tried the following fragment:
    Code:
    int p1*;
    *p1=1;
    int p22=4;
    cout<<p22;
    and it gives:

    Code:
    Segmentaiton fault (core dumped)
    but when I added new int:
    Code:
    int p1*;
    int y;
    *p1=1;
    int p22=4;
    cout<<p22;
    it works fine!!! is there anything I did not get about pointers causes this behavior???

  2. #2
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Pointer, weird error!!

    Quote Originally Posted by Adntu View Post
    Hi
    I am learning c++ pointers, I tried the following fragment:
    Code:
    int p1*;
    *p1=1;
    int p22=4;
    cout<<p22;
    and it gives:
    This code shouldn't even compile. The first line should be changed to:
    Code:
    int *p1;
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  3. #3
    Join Date
    May 2007
    Beans
    99

    Re: Pointer, weird error!!

    I am sorry, I wrote it incorrect here, but in my file it is:
    Code:
    #include <iostream>
    using namespace std;
    main ()
    {
    int *pa;
    float *ps;
    char *pd;
    
    
    *pa=1;
    *ps=2;
    *pd=3;
    cout << *pa<<*ps<<*pd;
    return 0;
    }
    as I said I am just trying it out.
    so what might be the cause???
    thanks

  4. #4
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Pointer, weird error!!

    Pointers are generally used to point to addresses, you're using them to store values... If that's all you need, just use a normal variable,

  5. #5
    Join Date
    May 2007
    Beans
    99

    Re: Pointer, weird error!!

    no, this is not a functional code, just I am trying to use the pointers, would you please tell me what is wrong with my code? I just wrote it similar to some website and to one book I have????

  6. #6
    Join Date
    Apr 2007
    Beans
    69

    Re: Pointer, weird error!!

    This is your problem:
    Code:
    int *pa;
    *pa=1;
    You create a pointer to a memory address then write to it. However, you have never allocated memory at that address to store the value.

    You need to do something like:
    Code:
    int *pa = new int;
    *pa=1;
    delete pa;
    Be sure to use a corresponding delete with every new otherwise the memory will not be freed and you will have created a nice memory leak in your program.

  7. #7
    Join Date
    Sep 2006
    Beans
    41

    Smile Re: Pointer, weird error!!

    try this

    #include <iostream>
    using namespace std;

    int main ()
    {

    // initialise values
    int a = 3;
    float s = 3.14;
    char d = 'c';

    // create pointers
    int *pa;
    float *ps;
    char *pd;

    //give pointers addresses of values
    pa=&a;
    ps=&s;
    pd=&d;


    cout << "actual value of pa: " << *pa << endl;
    cout << "actual value of ps: " << *ps << endl;
    cout << "actual value of pd: " << *pd << endl;

    cout << "address value of pa: " << pa << endl;
    cout << "address value of ps: " << ps << endl;
    cout << "address value of pd: " << pd << endl;

    return 0;


    }

    make any sense?? note pedants: i've left the new and delete stuff out to make it a bit clearer.. see previous post.. this would be considered poor practice in real life but for teaching purposes makes a point
    Last edited by siiib; May 9th, 2007 at 06:42 PM. Reason: rectified error

  8. #8
    Join Date
    Apr 2007
    Beans
    69

    Re: Pointer, weird error!!

    I'm going to briefly explain what siiib has done a bit.

    int a = 3; // this will create an int on the stack and allocate memory for it.
    int *pa; // this will create a pointer of type int.
    pa=&a; // this will set pa to point to the memory address of a.

    Now any manipulation of *pa will change a because pa points to the address of a. So:
    *pa=14; // now a == 14.

    A pointer simply points to a memory address. You need allocated memory at that address to manipulate the value. You can either do this by creating an object on the stack like in siiib's example or you can create it on the heap using new. If the object is created on the stack the memory will be freed once the object is out of scope. For example if you have it in a function call x() then once the program leaves x() then the memory will be freed. If you create it on the heap using new then it will remain until you explicitly free it using delete.

    Simply put a pointer poitns to something you either have to set it to point to something already created or create something for it to point to.

  9. #9
    Join Date
    Sep 2006
    Beans
    41

    Re: Pointer, weird error!!

    heh thanks for the explanation.. i was eating my dinner at the time..

    i think what has you confused is that you are trying to 'allocate' a value using the dereference operator.. the dereference operator * is special in that it gives you the value at an address not the address.. by logical extension and thinking ahead (like i think you have) you'd think that it would work the other way wouldn't you?? but it doesn't really work like that.. *pa ==4; is not something that you'd normally see..thats just how it is i'm afraid.

    hth

  10. #10
    Join Date
    May 2007
    Beans
    61
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Pointer, weird error!!

    They're what we call dangling pointers, they point to some unknown location. Pointers point to a memory address, that's what they store, the address. If you just declare a pointer without initializing it with a default value, it points to a unknown area in memory. When you try to dereference it and assign it a value like this
    Code:
    *p = 10;
    chances are your writing to memory you don't have permission to write to, hence the Segmentation Fault. If you want to give it something to point to other than another variable, then you can allocate a new data with new like this, but don't forget to release the allocated memory with delete before the program end's or you will have a memory leak.

    Code:
    #include <iostream>
    
    int main()
    {
        int *p = new int; // Allocates memory for a new int
        
        *p = 42; // Now we can access that memory
        
        std::cout << *p << std::endl;
    
        delete p; // Never, ever forget to free memory!!!
    
        return 0;
    }
    Hope that helped explain things, pointers are very useful for somethings but it's recommended that where ever possible you should use references.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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