malloc error when trying to assign the returned pointer to a struct field

Steven Schveighoffer schveiguy at gmail.com
Sat Sep 9 09:47:14 UTC 2023


On Saturday, 9 September 2023 at 09:21:32 UTC, rempas wrote:

> Now, if only one could expect how and why "libc" knows that and 
> doesn't just care to give me the memory I asked it for? Or it 
> could be than D does something additional without telling us? 
> Which can explain when this memory is only present when I 
> assign the value to the "this._ptr` field!
>

You are focusing on the wrong problem.

You asked for size bytes, and malloc gave you size bytes. It 
doesn't "know" anything special.

Then you proceeded at some point to write *past* the size bytes. 
What did you overwrite? Probably some internal malloc 
implementation structure. Then it later noticed "hey, this 
structure doesn't make sense, I'm going to report it to the 
user!" That's why you see the message.

Memory problems are very difficult to find, and typically an 
error is triggered far away from the source, in seemingly 
unrelated code. This is why whenever I see an error that smells 
like memory corruption, I stop all other work and find it. Memory 
errors can come and go based on random chance or how the compiler 
lays out functions. So having it "just go away" isn't enough. 
Very very infrequently, this happens because of a codegen issue, 
but most of the time it's pilot error.

-Steve


More information about the Digitalmars-d-learn mailing list