-
June 16th, 2015, 03:06 AM
#1
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.
-
June 16th, 2015, 05:10 AM
#2
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.
-
June 16th, 2015, 05:54 AM
#3
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.
-
June 16th, 2015, 07:10 AM
#4
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.
-
June 16th, 2015, 07:19 AM
#5
Re: Is memory allocated to a variable name or an address?
 Originally Posted by alwaystudent
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.
-
June 16th, 2015, 07:51 AM
#6
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.
-
June 16th, 2015, 08:17 AM
#7
Re: Is memory allocated to a variable name or an address?
 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;
}
-
June 16th, 2015, 08:33 AM
#8
Re: Is memory allocated to a variable name or an address?
 Originally Posted by alwaystudent
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.
-
June 16th, 2015, 12:48 PM
#9
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
-
June 16th, 2015, 12:53 PM
#10
Re: Is memory allocated to a variable name or an address?
Thanks all, thanks for your perspective and idea's, specially for reinterpret_cast...
-
June 16th, 2015, 12:55 PM
#11
Re: Is memory allocated to a variable name or an address?
 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.
-
June 16th, 2015, 12:55 PM
#12
Re: Is memory allocated to a variable name or an address?
 Originally Posted by alwaystudent
@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.
-
June 16th, 2015, 03:47 PM
#13
Re: Is memory allocated to a variable name or an address?
 Originally Posted by alwaystudent
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.
-
June 17th, 2015, 01:48 AM
#14
Re: Is memory allocated to a variable name or an address?
@GCDEF: you are right:
 Originally Posted by alwaystudent
@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.
-
June 17th, 2015, 04:52 AM
#15
Re: Is memory allocated to a variable name or an address?
@laserlight:thank you!,your Code in post #7 is very shining example
 Originally Posted by laserlight
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|