开发者

memset after malloc [closed]

开发者 https://www.devze.com 2023-03-16 10:37 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have three lines (version) of a linux product. V1 works fine in the customer. V2 and V3 crashed and the fix seems to be a memset call afte开发者_JS百科r a malloc call.

What is the deeper explanation on this topic? Why memset resolved the issue?


My guess without a code example is that you were operating on the buffer or struct you malloc'd with assumptions that its contents would be initialized with certain default values. Malloc doesn't initialize the memory it hands back, so unless you memset or use some other initialization, the values in that memory could be anything, and therefore, if you're trying to check a pointer assuming it'd be NULL or that an int will be zero, you can't make that assumption without initializing the memory first.


Maybe because there is a wrong assumtion that the allocated buffer is zeroed. So for example if the buffer contains a string and is printed somewhere before initialize, it can result in an access violation. Zeroing the buffer would fix such an issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消