Implement an aligned malloc function using the built-in malloc function

  Рет қаралды 3,749

Interview Kickstart

Interview Kickstart

Күн бұрын

Пікірлер: 12
@omer12berar
@omer12berar Жыл бұрын
the example is wrong , 13 is not aligned to 9 . in the line of code where you compute the offset rv = 7 and not 3
@ravishankar7972
@ravishankar7972 Жыл бұрын
Offset = 6 is correct, rv should have returned 9 instead of 13.
@ravishankar7972
@ravishankar7972 Жыл бұрын
If storing offset needs at least 4 bytes of space I don’t see this as a good solution either.
@dimabirenbaum9200
@dimabirenbaum9200 Жыл бұрын
Thank you for the video. One comment, for completeness of the code I think we can add a check "if(rv == NULL) return NULL;". Since malloc can fail on allocation.
@guuu6566
@guuu6566 Жыл бұрын
You are absolutely correct, this sort of check is probably prudent for completeness.
@sohamehta
@sohamehta Жыл бұрын
@@guuu6566 thanks for responding!
@revanthrajashekar5983
@revanthrajashekar5983 Жыл бұрын
in case the offset is less, aren't we allocating more memory than actually requested ?
@guuu6566
@guuu6566 Жыл бұрын
Yes and no (if I understand your question correctly). Yes, we are always allocating more memory than requested; and in fact we are always allocating more memory than necessary (we should probably subtract `1` from our malloc request). But no: this is independent of the runtime offset calculated; we over-allocate in malloc to handle any possible returned alignment. Interestingly, there's actually another issue in this code (which may pop-up as a runtime failure on systems which require aligned memory load/stores): `storage` may not be at a valid memory offset for a uint32_t. In particular, if `alignment` is less than `4`. This question was intended for large alignment values; but we should either fix it for small alignments, or add a check to reject alignment
@sohammehta810
@sohammehta810 Жыл бұрын
@@guuu6566 thanks!
@ravishankar7972
@ravishankar7972 Жыл бұрын
@@guuu6566I had the same notion, well summarized👏
@guuu6566
@guuu6566 11 ай бұрын
To elaborate a bit further: The address pointed to by the local variable storage may not be 4-byte aligned. And if this code was ran on a system that required aligned stores, then it would presumably fail. Yes, if we put an appropriate restriction on alignment, we could avoid the problem. But that likely goes against the spirit of the question. If my goal was to make the simplest change to this code to resolve this concern, I would do something along the lines of: uint32_t temp = offset + sizeof(uint32_t) memcpy(storage, &temp, sizeof(uint32_t)); With the presumption that memcpy would resolve any alignment concerns. We would of course have to similarly use memcpy to read this value in the aligned-free function. If we wanted to avoid memcpy, then we could do another calculation to figure-out the appropriate aligned location for storage. Something along the lines of storage = ROUND_DOWN(storage, 4) (i.e. Shift storage "down" in memory such that it becomes aligned). Then also do the same within free.
@bonafontciel
@bonafontciel Жыл бұрын
I just want to give you feedback to improve your videos. Please don’t use white background it hurts people’s eyes specially for those coding day and night
How to read a 128-bit timestamp on 64-bit architecture?
16:07
Interview Kickstart
Рет қаралды 1,4 М.
Memory pool allocator in C without using built-in malloc() and free() functions
36:10
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 47 МЛН
WORLD BEST MAGIC SECRETS
00:50
MasomkaMagic
Рет қаралды 32 МЛН
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 186 МЛН
AI can't cross this line and we don't know why.
24:07
Welch Labs
Рет қаралды 330 М.
Coding Was HARD Until I Learned These 5 Things...
8:34
Elsa Scola
Рет қаралды 400 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 306 М.
One reason to Modify your Memory Allocator (C/C++)
10:23
Jacob Sorber
Рет қаралды 33 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,4 МЛН
i wrote my own memory allocator in C to prove a point
5:23
Low Level Learning
Рет қаралды 368 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 221 М.
what's in a heap? simple malloc implementation internals explained
36:07
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 47 МЛН