1

Let's say you have a static array as an attribute of a struct

   struct example {

           char array [5];

};

struct example* exp  = (struct example*)malloc(sizeof(struct example));

...initialize and so on...

Now is the static array in the struct considered to be on the heap or stack? In addition is it any more efficient to have an array with specified size in the struct vs a dynamically allocated array, i.e. a pointer that will point to the dynamic memory once you allocate it, attribute in terms of time to allocate? Lastily if it is on the stack does the stack eliminate the variable right when the struct is freed?

1
  • 1
    what do you understand malloc to do? Commented Oct 13, 2015 at 4:35

3 Answers 3

5

You are using the malloc function to allocate a memory.
So, using malloc function to allocate a memory for any variable, or a structure, it will allocate a memory in heap only. You declaring a variable "a", this is the member of structure. So, in here you are not allocating a memory. You just declare a structure. After that you allocating a memory using malloc, in here only the memory will be allocated in the heap.

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

Comments

0

In your code struct example will have size of 5 bytes which contains the character array in there. With the malloc command you have allocated 5 bytes which contains the structure completely. malloc will allocate memory off the heap. The character array array is the first (and only) 5 bytes of the allocated memory.

Given this is a small amount of memory you may just want a stack variable to hold it rather than allocate off the heap. If you are only using it for the duration of the function. In which case you can simply go:

struct example  exp;

Now this is just a structure on the stack, so no overhead used in allocating it. Of course if this was a massive structure then it wouldn't be so wise to do it that way.

Comments

-1

The array is a part of the structure. Wherever the structure is, the array is inside it.

If you use heap, it is no different from allocating a variable size array. In fact, you can increase the size of the allocation and it'll give you more space for the array. E.g. if you allocate 10 bytes, you will be able to use 10 elements in the array - array[0] to array[9]. And, in reverse, if you allocate only 2 bytes, you still can use the 2 elements of the array - array[0] and array[1]. So the number 5 that you have specified only matters for sizeof().

Of course, if you use it as a local variable in the stack, it is all different.

7 Comments

Not sure what you are talking about with the number of bytes allocated ; allocating more than 5 is wasted space, and allocating less than 5 will cause undefined behaviour when you access the array
Array is just a piece of memory which is accessed through the pointer. How much memory is available depends on how much is allocated, not on how much is declared. If you don't believe me - try it.
No, arrays with declared size cannot be accessed out of bounds. Attempting to do so causes undefined behaviour. "Try it" does not prove anything because any possible behaviour can occur when there is undefined behaviour.
x[y] operator defined in C99 as equivalent of *(x+y). The + operator as applied to a pointer is defined as adding offset muliplied by sizeof() to the pointer. There's nothing in these definitions which would limit the offset to any specified amount. You only need to make sure the memory at the specified offset is available. malloc() returns untyped pointer. If you don't believe me - read it,
Read all of 6.5.6.1/8. " If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element" . "The array" is the thing declared with [], not however much space you allocate with malloc.
|

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.