CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2015
    Posts
    21

    Is memory allocated to a variable name or an address?

    When we have memory allocation or memory deallocation in Code, memory is assigned to what?

    1- A pointer's name (or equally the any name addressing related to compiler)

    or

    2- A pointer's address (what we can usually dereference it and get memory content) ?

    in other word: could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration?
    Last edited by alwaystudent; June 16th, 2015 at 03:26 AM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Is memory allocated to a variable name or an address?

    I assume that by "memory allocation in code" you mean "new" ?

    New makes an allocation, and then returns the address of the allocated space (if successful) in the form of a pointer. It is then up to you to store that result for future use. The *memory* itself is not assigned to anything.

    1) The memory is not assigned to anything. A pointer *to* the memory may be assigned to anything you want.
    2) "A pointer's address" While this is possible ("Pointer to pointer"), you are probably confused here.

    "could we memory allocation to specific legal address (e.g: 0x7fff12345678)..."
    - You cannot do memory allocations at a specific address, with standard C++. The OS/StandardLibrary has full control on where allocation takes place.

    "... without any variable declaration"
    Yes. Here:
    Code:
    int main() {
      new int();
    }
    Not very useful though
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    thanks for good explanation,
    for this Code:

    #include"stdio.h"
    main()
    {
    char *A,*B;
    A=B; //A and B have same address(point to-location) but different name,
    A=new char[10]; //is the allocation, matter of the name (A as string) or the location (A as address)
    B=new char[20];
    A[18]='C';//is this really OK or we have potentially hidden error?

    }
    is this really OK or we have potentially hidden error?
    Last edited by alwaystudent; June 16th, 2015 at 06:33 AM.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Is memory allocated to a variable name or an address?

    it isn't entirely clear what you are asking, your question is formulated in a weird manner, you may have to clarify what you mean.

    'operator new' allocates a chunk of memory of the desired size (size is typically provided 'behind the scenes' by the compiler).
    operator new returns a pointer, this pointer value is typically assigned to a (pointer)variable because you will need to delete the memory afterwards.

    could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration?
    do you mean... Can we allocate memory, and assign the pointer to the allocated memory to a specific address ?
    then, yes:
    Code:
    *reinterpret_cast<int**>(0x7fff5678) = new int;
    this will probably crash if you don't know what you're doing.

    or do you mean, can you construct an object at a specific memory location (assumedly, previously allocated some way since otherwise the constructor will crash) ?
    then, yes, this is called 'placement new'.
    Code:
    MyClass* p= new( reinterpret_cast<MyClass*>(0x7fff5678) ) MyClass(param1, param2, param3);
    ...
    p->~MyClass(); // <- destroy previously placed object ,  there is no delete, since nothing got allocated.

    If you mean something else, you'll need to be more clear.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,627

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent View Post
    thanks for good explanation,
    for this Code:
    Code:
    #include"stdio.h"
    main()
    {
    char *A,*B;
    A=B; //A and B have same address(point to-location) but different name,
    A=new char[10]; //is the allocation, matter of the name (A as string) or the location (A as address)
    B=new char[20];
    A[18]='C';//is this really OK or we have potentially hidden error?
    
    }
    is this really OK or we have potentially hidden error?
    Since this is probably homework, why don't you analyze is a line at a time and understand what's going on. There is a problem there, but it's crucial that you understand what and why. Tell us what you think.

  6. #6
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    thanks all, it is not my homework and i am not student , as i am think:
    memory allocation most probably related to pointer name i.e we allocate memory to a "name" and NOT "address"
    because pointer identified by name NOT by address(point to-location)
    in the example code, pointer A and B have same address's ( point to-location) but are individual pointer
    and we can (can we?) different memory allocation from them. and A[18] lead (does lead ?)to the memory error.
    Last edited by alwaystudent; June 16th, 2015 at 08:00 AM.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent
    i am think:
    memory allocation most probably related to pointer name i.e we allocate memory to a "name" and NOT "address"
    because pointer identified by name NOT by address(point to-location)
    in the example code, pointer A and B have same address's ( point to-location) but are individual pointer
    and we can (can we?) different memory allocation from them. and A[18] lead to (does lead to?) memory error.
    It might be instructive to compile and run a program like this to test your ideas:
    Code:
    #include <iostream>
    
    int main()
    {
        using std::cout;
        using std::endl;
    
        // For simplicity, assume that new does not throw an exception:
        int* x = new int(123);
        int* y = new int(456);
        int* z = x;
        cout << x << " @ " << &x << ": " << *x << endl;
        cout << y << " @ " << &y << ": " << *y << endl;
        cout << z << " @ " << &z << ": " << *z << endl;
        cout << "-----\n";
        z = y;
        cout << x << " @ " << &x << ": " << *x << endl;
        cout << y << " @ " << &y << ": " << *y << endl;
        cout << z << " @ " << &z << ": " << *z << endl;
        delete y;
        delete x;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,627

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent View Post
    thanks all, it is not my homework and i am not student , as i am think:
    memory allocation most probably related to pointer name i.e we allocate memory to a "name" and NOT "address"
    because pointer identified by name NOT by address(point to-location)
    in the example code, pointer A and B have same address's ( point to-location) but are individual pointer
    and we can (can we?) different memory allocation from them. and A[18] lead (does lead ?)to the memory error.
    Look at it step by step
    Code:
    #include"stdio.h"
    main()
    {
    char *A,*B; 
    A=B; B wasn't initialized, so A and B now both point to some random and meaningless number
    A=new char[10]; new returns a pointer.  A now points to an array of 10 char(s)
    B=new char[20]; new returns a pointer.  B now points to an array of 20 char(s)
    A[18]='C'; A points to an array of 10 char(s).  A[18] is an invalid address.
    }
    I think you're thinking about it too hard. Pointers are just variables, very similar to any other variable. Rather than storing an int, a char, a double or whatever, they store a memory address.

  9. #9
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    @GCDEF: when pointer variable is declared -even it is not initialized-, it get specific address from memory to point it, so A and B after A=B have same address to point it

  10. #10
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    Thanks all, thanks for your perspective and idea's, specially for reinterpret_cast...

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent
    when pointer variable is declared -even it is not initialized-, it get specific address from memory to point it, so A and B after A=B have same address to point it
    That would be true even if A and B were of type int: after assignment, both A and B have the same value. It so happens that the value of a pointer is an address.

    Did you compile and run the program I posted in post #7? If you did, you would see output that shows the value of the pointer (i.e., the address of what it points to), the address of the pointer itself, and the value of what the pointer points to.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,627

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent View Post
    @GCDEF: when pointer variable is declared -even it is not initialized-, it get specific address from memory to point it, so A and B after A=B have same address to point it
    Are you asking me or telling me? Uninitialized, it will never get a specific address from memory. It will contain whatever happened to be in the computer's memory where its value is stored. It may be an address, it may not. Whatever it is, it isn't something you can use. Setting a pointer to an unitialized value like that is a really bad idea. Regardless, you overwrite both their values in your next two statements.

  13. #13
    Join Date
    Jun 2015
    Posts
    208

    Re: Is memory allocated to a variable name or an address?

    Quote Originally Posted by alwaystudent View Post
    in other word: could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration?
    When you use the new operator to allocate memory the C++ runtime system will request memory from the operating system. A C++ pointer is a variable big enougth to hold a memory address and if the memory request succeeds the new operator will assign the address of the allocated memory to the pointer you have specified. In this scenario you have no control over what address the pointer will hold.

    But C++ is a systems programming language and you are free to talk to the operating system directly or even bypass it completely if you so desire. In that case you may be able to convince the operating system to allocate memory at a certain address for you, say at 0x7fff12345678. Or if you know there is memory available at some location outside the control of the operating system, say around 0x7fff12345678, you could manage it by yourself and do whatever you want with it.

    So the new operator is the standard C++ way to allocate dynamic memory but you are perfectly free to ignore it and allocate dynamic memory any which way you like (if you have the proper access rights and at your own risk of course). But it doesn't stop there. C++ allows you to replace the implementation of the new operator (supplied by your compiler manufacturer) with an implementation of your own (you either develop yourself or get from somewhere). It's like eating the cake and have it. You use new like you did before but now it dances to your tune.
    Last edited by tiliavirga; June 17th, 2015 at 01:59 AM.

  14. #14
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    @GCDEF: you are right:

    Quote Originally Posted by alwaystudent View Post
    @GCDEF: when pointer variable is declared -even it is not initialized-, it get specific address from memory to point it, so A and B after A=B have same address to point it
    Last edited by alwaystudent; June 17th, 2015 at 01:52 AM.

  15. #15
    Join Date
    Jun 2015
    Posts
    21

    Re: Is memory allocated to a variable name or an address?

    @laserlight:thank you!,your Code in post #7 is very shining example

    Quote Originally Posted by laserlight View Post
    That would be true even if A and B were of type int: after assignment, both A and B have the same value. It so happens that the value of a pointer is an address.

    Did you compile and run the program I posted in post #7? If you did, you would see output that shows the value of the pointer (i.e., the address of what it points to), the address of the pointer itself, and the value of what the pointer points to.

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