The heap, using new and delete, and pointers

This is a discussion on The heap, using new and delete, and pointers within the C++ Programming forums, part of the General Programming Boards category; Ok, so when you use the 'new' keyword, the pointer is kind of like the variable, right? So if you ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    The heap, using new and delete, and pointers

    Ok, so when you use the 'new' keyword, the pointer is kind of like the variable, right? So if you had

    int* variable = new int;
    *variable = 6;

    ok so is that correct (syntax)? what would happen if i tried to increment the address of *variable? like this:

    variable++;

    could i also do this:

    int* variable = new int = 6;

    ?????

    and if i try to assign the pointer an address like this:

    variable=x;

    instead of:

    variable=&x;

    then am i saying that the address is whatever x equals, and not pointing to the variable x?

    I dont have a compiler on this computer right now so i cant test it out. please answer my questions.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    14
    >Ok, so when you use the 'new' keyword, the pointer is kind of l>ike the variable, right?
    No, the pointer is still the pointer, it only points to a space you have reserved using new.


    >So if you had

    >int* variable = new int;
    >*variable = 6;

    >ok so is that correct (syntax)? what would happen if i tried to >increment the address of *variable? like this:

    The pointer would point to the next byte in the memory.. which you most like has not reserved.. (bad idea)
    variable++;

    >could i also do this:

    int* variable = new int = 6;

    not sure.. but I dont think so.
    >?????

    >and if i try to assign the pointer an address like this:

    variable=x;

    you can't set the value of a pointer to a value of a variable, "level-error" I am assuming that x is not a pointer.

    >instead of:

    variable=&x;

    /Laos

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    I believe if you difference sp? the pointer like this *variable++ it will work cause when you difference a pointer it gives the value instead of the address but you might wanna check with someone first.

    Ryan

  4. #4
    Unregistered
    Guest

    malloc

    Use malloc, free and realloc. <stdlib.h>
    They are more comfortable.
    in *ptr++ you should know that the *operator has higher
    priority than the ++operator.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    int* variable = new int = 6;

    From compiler point of view, this is valid, provided that you cast 6 from (const int) to (int *), the correct type of variable.

    int* variable = new int = (int *) 6;

    From human point of view, variable is a pointer to int. It is real variable and has 6 as a value. The assignment is incorrect, as you lose address of reserved memory, which cannot be released then.

    variable=x;

    am i saying that the address is whatever x equals, and not pointing to the variable x?
    Yes, it is true. However, x must be retyped as (int *) x.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Ok, so when you use the 'new' keyword, the pointer is kind of like the variable, right?
    Certainly yes. It is variable like any other. You can add, get difference, compare etc. Value of a pointer is an address, not location it points to. Address is actually number and can be safely
    casted to (int) on 32 bit machines without any changes in its bit pattern.

Popular pages Recent additions subscribe to a feed

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