0

In the below code, I get memory through malloc and pointed it by pointer ptr. When I assigned value as shown I stored the data in pointer and we know that pointer is located in stack frame.

So my question is: My data(integers) is stored in stack or in heap?

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *ptr;
  ptr=(int*)malloc(5*sizeof(int));
  ptr[0]=5;
  ptr[1]=6;
  ptr[2]=8;
  ptr[3]=10;
  ptr[4]=11;
}
Chen Li
  • 4,824
  • 3
  • 28
  • 55
  • what does malloc do? If you know the answer to that then you know the answer to your question – pm100 Mar 11 '22 at 05:07
  • Vishal Mourya, "My data(integers) is stored in stack or in heap?,"My data(integers) is stored in stack or in heap?" --> _Why_ is it of interest to you? – chux - Reinstate Monica Mar 11 '22 at 05:14
  • regarding: `void main()` There are only two valid signatures for main(). They are: `int main( void )` and `int main( int argc, char *argv[] )` Your compiler should have told you about this problem – user3629249 Mar 11 '22 at 21:34

2 Answers2

2

Yes, the pointer is stored on the stack, but the memory it points to is on the heap. Therefore, the integers are stored on the heap.


Edit to answer the question from the comments:

Why we didn't use * like *ptr[0]=5; this means 5 store at the location where ptr[0] is pointing So, my question is why we didn't use *before ptr

In C, an array access is defined in terms of pointers. So the array element access ptr[0] can be written as *ptr. To access element n of the array, you can write ptr[n] or *(ptr+n).

You would use *ptr[0] if your array stored pointers and you wanted to access the value. You can also think of that as a two-dimensional array. In other words, *ptr[0] is equivalent to ptr[0][0].

From the C standard:

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

Also see this question: Is an array name a pointer?

Armandas
  • 2,276
  • 1
  • 22
  • 27
  • Why we didn't use * like *ptr[0]=5; this means 5 store at the location where ptr[0] is pointing So, my question is why we didn't use *before ptr –  Mar 11 '22 at 14:30
0

Pedantically speaking, from the perspective of the C standard, all the objects involved here simply occupy some memory that is suitably aligned, and disjointed from other objects.

While the notion of a stack and heap are commonplace, the C standard makes no reference to either terms. How and where data is stored is mostly implementation-defined.

The principle thing differentiating them is their visibility and effective lifetimes (see: Storage duration, Lifetime, and Scope):

  • The use of ptr is valid until main returns.

  • The use of the memory at the address returned by malloc is valid until it is deallocated.


Practically speaking, however, it is generally fair to refer to objects with

  • automatic storage duration as existing on the "stack",
  • static storage duration as existing in the "data segment", and
  • allocated storage duration as existing on the "heap",

as this a very common way C implementations work.

With this terminology, the object designated by the identifier ptr exists on the stack, and the objects accessed via the pointer returned by malloc exist on the heap.

Oka
  • 23,367
  • 6
  • 42
  • 53
  • Re “existing on the "heap": If a memory management implementation uses a heap, then it is where free blocks of memory are kept. When memory is allocated, a block (or a part of it) is taken **off** the heap and provided for use. So allocated memory is off the heap, not on or in it. When `free` is called, the block is returned to the heap to be available for later reallocation. Generally “heap” should not be used for this, as it is a name for data structure and associated algorithms, not for memory management, and memory management does not necessarily use a heap data structure. – Eric Postpischil Mar 11 '22 at 12:32
  • @EricPostpischil While I don't disagree, we're treading in hair-splitting territory, as "heap" is very often used to plainly and generally describe where dynamically allocated memory resides. There are no end of articles, learning resources, and [Q&As](https://stackoverflow.com/search?q=%5Bc%5D+heap), that conflate the two. I did try to separate the answer into the pedantic and practical, but would "existing on memory sourced (from|via) the heap" be a more satisfying description? – Oka Mar 11 '22 at 18:38
  • Just call it “dynamically allocated memory”. (The C standard calls it “allocated storage” but also uses “allocated” to refer to memory reservations generally, whether static, automatic, thread, or dynamic.) Some implementations of `malloc` *et al* use “zone” or “pool” to describe some of their memory management, but there is no reason to use implementation-specific terms at all. Yes, there is plenty of material that uses the term “heap.” That is no reason not to improve the practice. Using a correct clear term is better than use a widespread sloppy term. – Eric Postpischil Mar 11 '22 at 18:47