ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My reply:
You are right ... But we want to understand it.
Hopefully, based on all this information, you now understand the issues. And why your program might "appear" to work on one platform ... and fail on another ... despite the fact that it's a bug on all platforms.
Hopefully, too, you've found and corrected the problem (my money is definitely on mhcox's suggestion: double-freeing pointers).
Most of all, please take sundialsvcs's suggestions to heart - maybe even formalize them in coding guidelines for your team, and enforce them in team code reviews:
Quote:
I advise the following self-disciplines with respect to dynamically allocated memory.
1. "A pointer's value shall be NULL or it shall point to a valid block of storage." Therefore, the statement immediately following should be p = NULL;.
2. "All dynamically-allocated blocks of storage shall be immediately initialized to a known value, such as zeroes." Either use a storage allocation function that does this, or do it yourself.
3. "Allocate all buffers four to eight bytes larger than they need to be, make sure all those bytes are known-zero, and don't tell anyone they exist when you quote the size of the buffer."
4. "When manipulating strings, always use functions that require you to give the size of the buffer and that will not write beyond that." Remember the trailing-null that some routines stomp past the end of the buffer.
5. "Always check. Don't assume. When something is wrong, fail noisily."
6. "Insert debugging code into your program until you are certain that it is completely debugged. Then, and only then ... leave it in. "
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by sundialsvcs
"Allocate all buffers four to eight bytes larger than they need to be, make sure all those bytes are known-zero, and don't tell anyone they exist when you quote the size of the buffer."
While I support the other advices, that one may be more dubious, as it basically allows code presenting small overflow bugs to run undetected, including by valgrind, purify or Sun's studio RTC.
Hopefully, based on all this information, you now understand the issues. And why your program might "appear" to work on one platform ... and fail on another ... despite the fact that it's a bug on all platforms.
Hopefully, too, you've found and corrected the problem (my money is definitely on mhcox's suggestion: double-freeing pointers).
Most of all, please take sundialsvcs's suggestions to heart - maybe even formalize them in coding guidelines for your team, and enforce them in team code reviewsefinitely good advice there!
Finally - please let us know what happens!
Sincerely .. PSM
.
.
.
7. Regularly compile your code with memory error detection tools like Purify, valgrind, etc. and run the code through a suite of unit-tests.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.