HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Strange behavior of new-delete/malloc-free
Operating System - HP-UX
1829547
Members
2383
Online
109992
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
Forums
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
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
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
07-21-2003 03:47 AM
07-21-2003 03:47 AM
Hi All,
Please go through the attached code:
Here
1. I allocate space to a unsigned int ** variable using new and also malloc later.
2. I assign some value to it.
3. I print the value.
4. I ***DEALLOCATE*** the allocated space using delete/free.
5. I ***ASSIGN*** values to deallocated pointer.
6. I print the values using the pointer(***DEALLOCATED***).
I know that the memory is freed(returned back to the system) after a call to delete/free.
In this example I am not only referencing data at locations already deallocated but am also reassigning some values at the locations.
This code snippet works perfectly fine, without even a warning when I was expecting segmentation violation/Bus Error core-dump.
Can anyone please explain this strange behavior.
Regards,
Shashibhushan Gokhale
Please go through the attached code:
Here
1. I allocate space to a unsigned int ** variable using new and also malloc later.
2. I assign some value to it.
3. I print the value.
4. I ***DEALLOCATE*** the allocated space using delete/free.
5. I ***ASSIGN*** values to deallocated pointer.
6. I print the values using the pointer(***DEALLOCATED***).
I know that the memory is freed(returned back to the system) after a call to delete/free.
In this example I am not only referencing data at locations already deallocated but am also reassigning some values at the locations.
This code snippet works perfectly fine, without even a warning when I was expecting segmentation violation/Bus Error core-dump.
Can anyone please explain this strange behavior.
Regards,
Shashibhushan Gokhale
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2003 04:08 AM
07-21-2003 04:08 AM
Re: Strange behavior of new-delete/malloc-free
This is actually normal behaviour. You'll only get a SIGSEGV if you try to access a memory address outside your processes address space.
The malloc/free/new/delete operators are all implemented with the underlying brk/sbrk system calls. When you call the malloc or new library routine, your data segment will be expanded through a call to sbrk(). When you call free or delete, the data segment is NOT reduced. The memory region is just added to a free list by the delete/free routine so your process can allocate it again later.
Therefore according to the OS, the addresses your process is trying to access are all valid because they are still within your processes data segment, so the signals are not delivered. Set the pointer to NULL or some other random value after freeing it and you'll get your core.
This is where a tool like Purify really helps out. Grab an evaluation copy and watch it scream at your program.
Regards,
Steve
The malloc/free/new/delete operators are all implemented with the underlying brk/sbrk system calls. When you call the malloc or new library routine, your data segment will be expanded through a call to sbrk(). When you call free or delete, the data segment is NOT reduced. The memory region is just added to a free list by the delete/free routine so your process can allocate it again later.
Therefore according to the OS, the addresses your process is trying to access are all valid because they are still within your processes data segment, so the signals are not delivered. Set the pointer to NULL or some other random value after freeing it and you'll get your core.
This is where a tool like Purify really helps out. Grab an evaluation copy and watch it scream at your program.
Regards,
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2003 06:52 AM
07-21-2003 06:52 AM
Solution
As mentioned, this is perfectly normal because the memory is still mapped to the processes Virtual Address Space. You are actually operating under a misconception. Free() does not return memory the to system; it merely allows subsequent malloc()'s within the same process to reuse the memory. The only way to return memory to the OS is through the sbrk() system call; however, if you use sbrk() you must not use malloc(), calloc(), ... free() or absolute chaos is assured.
If it ain't broke, I can fix that.
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
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP