Operating System - HP-UX
1820389 Members
3864 Online
109623 Solutions
New Discussion

malloc and dynamic arrays

 
Max_4
Frequent Advisor

malloc and dynamic arrays

Hi all,

I was just wondering, I've seen malloc being used for allocating space
for dynamically-sized arrays but as far as I know malloc returns a pointer to a chunk of memory space NOT necessarily CONTIGUOS space in memory. How does the compiler manage to still access the right objects when doing pointer arithmetic when accessing array elements??
More concretely, let's suppose I'll need an n-integer dynamic array.

int *array, n, i;

printf("Enter the number of ints: ");
scanf("%d", &n);
array = malloc(n * sizeof(int));

/* Reading user input into the array */
for (i = 0; i < n; i++) {
printf("Enter digit %d: ", i);
scanf("%d", array++);
}

How does the compiler manage to handle array++? If the address of array were for example 0x8100, array++ will give 0x8104 (4 bytes for the integer) and doing array++ again we'll have 0x8108. But what would happen if 0x8104 is not the next free available space that malloc allocated for the integers? What if malloc just started at 0x8100 ended just before 0x8104 (only one int) and then skipped to 0x9100 (to allocate part of the remaining integers) because it was there when it could find the next available space for an integer?

How does the implementation of malloc cope with this? and how I'm able to use malloc to create dynamically-sized arrays, since arrays by definition are contiguos mapping of memory and malloc may not necessarily return a pointer to a completely contiguos chunk of memory?

Or maybe I'm really puzzled trying to understand the inner workings of malloc, and another possible assumption for me could be that malloc works by returning the first available contiguos chunk of memory to satisfy the request, rather than maybe grabbing all the scattered free chunks of memory to fullfill the request.
This poses one question if memory becomes really fragmented then malloc would not be able to satisfy space requirements, even though if you sum up all the free scattered chunks you'd have more than sufficient memory to satisfy the request....??


Thank you very much to all in advance...


1 REPLY 1
Bill Hassell
Honored Contributor

Re: malloc and dynamic arrays

When malloc returns a pointer, it is (for your program) contiguous. What may be confusing is that the kernel may not keep all parts of your program in a contiguous area. Looking at a physical memory map is a good way to hurt your brain!

The kernel manages your program space so you don't have to. So don't worry about your malloc'ed space--each request returns a contiguous area for you to use. However, always check every malloc call for success. There are a lot of broken programs out there that core dump because they assume a malloc always works.

Note that if you are using massive amounts for malloc (ie, 900 megs or more), you'll need to read about memory management for 32bit programs. Look in /usr/share/doc on any 11.0 or earlier system.


Bill Hassell, sysadmin