- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- malloc and dynamic arrays
Operating System - HP-UX
1820389
Members
3864
Online
109623
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 02:00 PM
10-13-2002 02:00 PM
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...
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 02:17 PM
10-13-2002 02:17 PM
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
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
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP