LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   identify allocation of a pointer (https://www.linuxquestions.org/questions/programming-9/identify-allocation-of-a-pointer-575342/)

mailsrinu28 08-07-2007 09:21 AM

identify allocation of a pointer
 
Hi friends,
Can any help me to know,

1.how to know a memory allocated to a pointer is from heap only and not from other memory area?

2. Can a pointer allocate memory from other areas of memory?


thanks in advance.

dsp

bigearsbilly 08-08-2007 05:03 AM

you can point your pointer anywhere you like.

whether you can do anything with it is another question.

memory can only be accessed if the operating system allows you to.

mailsrinu28 08-09-2007 09:23 AM

thanx for your reply.

my point of asking ques is,

if i say int *ptr;
memory will be allocated in heap,how we will be sure of that?

is there any way find this out? (we will take linux as os)

dsp

ravime 08-09-2007 09:31 AM

Memory allocated will be from heap only.Not form any other memory.

There is no way to find out.
Regards
ravime


Quote:

Originally Posted by mailsrinu28
thanx for your reply.

my point of asking ques is,

if i say int *ptr;
memory will be allocated in heap,how we will be sure of that?

is there any way find this out? (we will take linux as os)

dsp


hal_2001 08-09-2007 09:49 AM

int *ptr; has no memory allocated to it except for ptr variable being on the stack (local variable) or in global memory.

You can use malloc() to allocate a base address of the allocated memory to the pointer.

I think a parameter in the call to malloc can specify the type of memory to be allocated from.

elsheikhmh 08-10-2007 06:01 AM

1. int *i; Will not allocate anything; just empty place holder for some memory address. But i = (int*)malloc(n) will allocate n bytes of memory from the heap.
2. Pointer can point to anything else; from stack if you like:
Code:

int *i;
int x = 4;
i = &x;

Now, i points to the same location where the value 4 (viz. x) is stored.
Code:

int *i;
int x[14];
i = &x[0];

Now, i points to the same location address where the array x (from stack not heap) is stored.

elsheikhmh 08-10-2007 06:09 AM

Quote:

memory will be allocated in heap,how we will be sure of that?
is there any way find this out? (we will take linux as os)
I think you should take a look at kmalloc()
Code:

void * kmalloc (size_t size, int flags);
I has some flags GFP_DMA, GFP_ATOMIC, GFP_BUFFER, and GFP_NFS, may be relevant to your inquiry.

ta0kira 08-10-2007 08:13 AM

Whenever you create a variable in a function (to include the function's arguments,) that is stack memory. Anything created with malloc will be out of heap memory. If you are using C++, anything created with new will be in heap memory by default, but you can override that operator, use in-place new, or use an alternate allocator which can create new structures in memory blocked off on the stack. Each thread has its own stack, so if you allocate heap memory to give to something else in heap memory you are just assigning a pointer to a memory location as you would be in any other case. That memory doesn't become an object until a function performs an operation with it.
ta0kira

PS The only real difference between the two is stack memory is directly associated with the thread of control. When things are created, they are added to the stack. When functions and scopes (i.e. if/for) exit, things are removed one at a time until the stack pointer gets back down to where it started. This allows you to have a program which doesn't have to deallocate memory, but it also requires that the size of every variable be fixed at the time of compilation. Since that isn't always feasible, we have heap memory.

jim mcnamara 08-10-2007 11:18 AM

If you call alloca(), like you would call malloc(), it doesn't allocate from heap, rather from stack. I'm guessing this may be what the original poster meant?

sundialsvcs 08-11-2007 11:35 PM

Here's the skinny:

(1) The construct int *p; is only a declaration. It says that the variable "p" is "a pointer to an int." Nothing more, and nothing less. It does not do anything at all to specify the current value of "p." It simply declares to the compiler how "p" is meant to be used.

(2) The value of "p" should be, at any particular time, either a known-good address value, or null. But it is entirely up to you, the programmer, to make sure that this is so. A fairly-low-level language like "C" does not watch out for you.

(3) Sometimes you will find pointers being initialized to point to "other things," such as other variables. For example, the statement p=&foobar; causes "p" to point to "foobar," whatever or whereever "foobar" may be. In that case, "foobar" could be anywhere. "C" simply assumes that you know what you are talking about.

(4) Sometimes you will find pointers being initialized to point to storage-areas that are dynamically allocated, such as: p=malloc(sizeof(int)); In this case, the malloc() function returns a pointer-value and this gets assigned to "p." Once again, "C" simply assumes that you know what you are talking about.


All times are GMT -5. The time now is 02:53 PM.