4

Suppose I declare an array as int myarray[5]

Or declare it as int*myarray=malloc(5*sizeof(int))


Will both the declarations set equal amount of memory in number of bytes? Without considering that the former declaration is for the stack and the latter on the heap.

Thank you!

2
  • You can also use int* myarray = new int [5] Commented Oct 19, 2012 at 1:04
  • 1
    From the comment above, the keyword "new" is not available in C. Commented Dec 11, 2020 at 21:13

4 Answers 4

6

There's a fundamental difference, that may not be apparent in the way you use myarray:

  • int myarray[5]; declares an array of five integers, and the array is an automatic variable (and it is uninitialized).

  • int * myarray = malloc(5 * sizeof(int)); declares a variable that is a pointer to an int (also as an automatic variable), and that pointer is initialized with the result of a library call. That library call promises to make the resulting pointer point to a region of memory that's big enough to store five consecutive integers.

Because of pointer arithmetic, array-to-pointer decay and the convention that a[i] is the same as *(a + i), you can use both variables in the same way, namely as myarray[i]. This is of course by design.

If you're looking for a difference, then maybe the following helps: The array-of-five-ints is a single object, and it has a definite size. By contrast, the malloc library call doesn't create any objects. It just sets aside enough memory (and suitably aligned), but it could for example allocate a lot more memory.

(In C++ there's of course the additional distinction between memory and objects.)

Sign up to request clarification or add additional context in comments.

1 Comment

i would also like to add a consideration about the fact that if you are using the C++ and you are using pointers, you probably want to use references to avoid nasty behaviours, especially if you only modify the pointer value during the declaration.
3

Short Answer: No normally the latter use a small larger memory.

Long Answer: Memory management will certainly use some extra memory to manage the returned pointer and be able to track it, and free it at a later time and you declare an extra pointer to point to that memory. So its actual memory is sizeof(int*) + malloc_overhead. But in first case you use exactly 5 int(plus alignment possibly).

Comments

3

Neither is guaranteed to allocate exactly 5*sizeof(int) bytes, though both will give you at least that much space (assuming no allocation failures or stack exhaustion).

In the first case, the stack variable may be surrounded by alignment padding, and/or stack canaries (depending on compile options). These could result in the stack pointer being adjusted by more than 5*sizeof(int) bytes.

In the second case, you allocate a int * on the stack (sizeof(int *) bytes), plus the space that malloc returns. malloc may allocate additional memory in the form of allocation tracking structures, alignment padding, linked-list pointers, etc. Thus, in that case you are also not guaranteed to allocate exactly 5*sizeof(int) bytes.

If you want to be very precise about your memory usage, the mmap function allows you to request pages of virtual memory from the OS. The memory you request this way will be precisely the amount you request (ignoring the space taken up in the kernel to track those allocations).

3 Comments

Depending on the statement "equal amount of memory", one may "see" that malloc can potentially consume much more memory or none at all. Often times memory is provided to an application via paging, and if one is paying attention to the memory consumption marked for the program from the outside, it's possible to see assigned a whole page (app needs more heap space in general) or no page (app already had enough freed space for the array in an existing page).
Plus one for most complete answer, IMO
i think that this reply is slightly OT, or you are right but the question is not explained so well. You are basically explaining what happens behind the curtains, but it's not what he is asking here, if i'm using a language and i ask you about what is the size on an int you should be able to say a number according to the language specifications and not about how the OS or the compiler works. Also you have a very limited control on how the memory is organized, even the inline option it's nothing more than an hint for the compiler.
2

Dynamic allocation will require at least a few extra bytes; however many bytes for the pointer variable in addition to the 5 int-sized elements, and potentially some extra bytes to track the size of the allocated region so that it can be freed properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.