iLeoDo
iLeoDo

Reputation: 397

Array in C and malloc

1.How the two dimension stored in the memory, are they consecutive? (I mean the int[M][N], not the dynamic allocation, I think the int[M][N] happened in the stack area, so continual, isn't it?)

2.Does the area allocated by malloc must is consecutive?

3.If there is no need to dynamic allocate memory space, where should I use? stack or heap. For example, I want a char array to store 10000 chars, so should I use:

char a[10000];

or

char *a = calloc(sizeof(char),10000);

Is the "function call stack" in the same area as the variable stack??In the same stack or different?

Upvotes: 3

Views: 3258

Answers (5)

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

In int numbers[m][n] the n's are consecutive ints in memory, e.g. numbers[0][0] is followed by numbers[0][1].

On the other hand, lets say n=10, then numbers[m][9] is followed by numbers[m+1][0].

malloc returns consecutive memory. you decide how to use it.

A 10000 byte array on the stack is no problem unless the function is recursive and (as Carey notes) unless you are developing in a small stack environment ie. embedded.

Yes the callstack and local variables are identical.

Upvotes: 4

Chris Dodd
Chris Dodd

Reputation: 126203

C does not support 2D arrays. It supports arrays of arrays and arrays of pointers to arrays, either of which can be use like a 2D array, but they're not at all the same, and neither of them is really a 2D array.

An array occupies contiguous memory locations. So when you have an array of arrays, each subarray is individually contiguous, and these arrays are laid out in memory one after another, making the whole thing contiguous.

When you have an array of pointers to arrays, each subarray is individually contiguous, but the subarrays don't need to be contiguous with each other -- they might be spread all over memory. The top level array is contiguous, but it only contains the pointers to the subarrays, not the subbarrays themselves.

In either case, you can get a single (row) subarray out of the '2D' array, but there's no easy way to get a column array -- you need to create a separate array and copy the value out of each individual row array if you want it in that form.

Upvotes: 0

slugonamission
slugonamission

Reputation: 9642

1) I'll be honest, I don't know.

2) Yes, the area allocated by malloc is contiguous in the virtual address space. This can be deduced with a simple thought exercise; given a pointer, if you iterate over the full space, you always increment the pointer by one and still get valid data. Note though that it's possible for non-contiguous physical mappings to be returned, although this is all handled at levels below you (i.e. within the operating system and the processor's TLBs).

3) Typically, use the heap. It's generally a larger segment, especially if you're using threads (i.e. a test I just ran showed pthreads' default stack size as 8MB).

As for the second part of 3), the function call stack is the same as the variable stack. When you create a variable statically (i.e. not through malloc), it exists within the containing function's stack frame, which of course resides on the stack. For more information on this, have a look at the stack convention for .

Upvotes: -1

speeder
speeder

Reputation: 6296

When you allocate something[a][b] is it contiguous?

Yes

When I made games that had tiled maps I would even access a tile using a pointer in rendering.

Ie: if the map was 10x10, I would render tile [1][3] by using arrayName[14] for example.

Also keep in mind using that fact, that in some places, [b] instead of [a] is what will be completely on the memory, this might cause bugs if you are relying on code that came from FORTRAN (like CBLAS) or in some specific devices and situations (like some GPU with some particular drivers).

When you use malloc, it must be contiguous?

Yes, and it will fail if contiguous memory is not available. It is a very common mistake to expect that a program will work because the total free memory exists, without taking in account memory fragmentation.

I made once a game that failed to work because it had 30mb of memory but could not load a 16mb image... I did not realised at the time that memory fragmentation caused by my code was making no block of 16mb be available.

If there are no need for dynamic allocation, I should use stack or heap?

In C, dynamic allocation usually means heap allocation anyway, early C books even explicitly say that malloc and similar family of functions (calloc included) operate only on the heap. while automatic allocation attempts to use the stack.

Upvotes: 1

Jonathan Wood
Jonathan Wood

Reputation: 67193

  1. The memory is contiguous. All items in one dimension will be consecutive followed by the next dimension.

  2. Yes. All memory allocated by a single call to malloc() is contiguous.

  3. If you need a lot of memory, I recommend allocating it dynamically. For less memory, you could allocate statically. malloc() does use the heap. I don't recommend the stack except for only very small amounts of memory.

Upvotes: 1

Related Questions