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

rempas rempas at tutanota.com
Sat Sep 9 09:30:10 UTC 2023


On Saturday, 9 September 2023 at 09:04:18 UTC, Steven 
Schveighoffer wrote:
> This is not ideal. Why? Because 99% of the time, a poster has 
> come here with a problem they don't know how to solve, and have 
> focused in on where they *think* the problem is. However, the 
> problem isn't there. But us reading the description can only 
> see what the poster sees, and either don't see a problem ("I'm 
> just as confused as you are!") or know there is more to the 
> story.
>
> Not only that, but frequently not-complete code is... not 
> complete. And people tend to focus on problems they can see 
> (e.g. where is that `_len` defined?), frustrating the poster 
> with "trivial" problems that are solved "in the real code".
>
> Inevitably, there is a subsequent post with the real code, and 
> that contains the problem.
>
> The best thing to post is a minimally reproducing example. The 
> next best thing is a link to a complex reproducing example. 
> Which you have done later (I will take a look). I just wanted 
> to point this out because it's a frequent problem on these 
> forums.
>

Yeah... I got my lesson! Tbh, I wanted to avoid posting my source 
as much as possible because I do have this "perfectionist" 
mindset (which I am aware is bad and trying to get rid of, and 
have already done progress) and I want the source to sound as 
"professional" as possible as I take serious my project and have 
hopes for it! But yeah, NEVER again!

> Here, you have allocated `size` bytes for the array. Is this 
> what is intended? Your comments suggest otherwise! If 
> `T.sizeof` is 2 or more, then you still only allocate e.g. 20 
> bytes for a `_cap` of 20. but an array of 20 T's would require 
> 40 bytes.
>
> -Steve

Bingo! You and Brad found out! And like I said to him, it's funny 
that I had a previous function that just allocates memory and I 
removed it and then created that one and in that one, I forgot 
about that. It's super funny cause I even wrote the comment that 
explains it and my reallocation function (that gets triggered 
when someone wants to expand the collection) has that 
multiplication one it.

Thank you and everyone else for the help! Hope I can at least 
finish and share something that people will enjoy using!


More information about the Digitalmars-d-learn mailing list