Results 1 to 7 of 7

Thread: Pointer declaration and initialization

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63

    Pointer declaration and initialization

    Hi,
    I'm wondering what actually happens when you declare a pointer and initialize it on the same line.

    Does it set the pointer's value or the value the pointer points to?

    Which of the following is correct?

    int *a = 1;

    or

    int *b = malloc(4);

    Thanks,
    Cedric

  2. #2
    New Member
    Join Date
    Nov 2000
    Location
    Sweden
    Posts
    13

    Talking This must be it, I think!

    I don´t know about that Malloc thing, but this is how I do it:

    int number = 0;
    int* pNumber = 0;

    // sets the pointer pNumber to point at the adress of number
    pNumber = &number;

    *pNumber = 5; // gives the value 5 to the variable number

    cout << "number is: " << number << endl;


    output: number is: 5
    You can´t hit what you cant see

  3. #3
    New Member
    Join Date
    Nov 2000
    Location
    Sweden
    Posts
    13

    Talking This must be it, I think!

    I don´t know about that Malloc thing, but this is how I do it:
    int main()
    {
    int number = 0;
    int* pNumber = 0;

    // sets the pointer pNumber to point at the adress of number
    pNumber = &number;

    *pNumber = 5; // gives the value 5 to the variable number

    cout << "number is: " << number << endl;

    return 0;
    }
    output: number is: 5
    You can´t hit what you cant see

  4. #4
    New Member
    Join Date
    Nov 2000
    Location
    Sweden
    Posts
    13

    Thumbs down Oh well..

    Hmm when I edited my message, it added a second one.. that really sucks... oh well
    You can´t hit what you cant see

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    Maybe I should reiterate my question...
    (if you "don't know about that malloc thing" , please don't reply)

    When a pointer is created, you must initialize it to point to something. I am trying to do this in one line. Is it possible?
    What I'm fearing is that "int *a=1" assigns the value 1 to some unknown place in memory.

    Which of the following two statements is correct? I know they are both valid. But I am looking for code correctness.

    int *a = 1; // assigning the value 1 to *a

    or

    int *b = malloc(4); //assigning the newly allocated memory location to b

    Thanks for any help.
    Cedric

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Assigning a pointer has to be done in two steps, I think.

    First, you have to give the pointer itself a value, normally by using new or malloc:
    Code:
    int *ptr = malloc(sizeof(int) * 5);
    
    int *cplusptr = new int[5];
    Then, you have to dereference that pointer - you can't do it in one step because the outcome of malloc or new decides where in memory you put the value, therefore it's totally separate:
    Code:
    ptr[0] = 5;
    ptr[1] = 10;
    Finally, you free up the memory:
    Code:
    free(ptr);
    
    delete[] cplusptr;
    Make sure you match malloc/free and new/delete otherwise REALLY nasty stuff happens!
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    int *a = 1;
    makes the pointer a point to the address 1. This is invalid memory in windows, causing an access violation when dereferencing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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