How to access header for memory block via pointer arithmetic?How do function pointers in C work?Dynamic allocation (malloc) of contiguous block of memoryhow 'free' works when pointer is incrementedWhy isn't there a “memsize” in C which returns the size of a memory block allocated in the heap using malloc?Can a local variable's memory be accessed outside its scope?How to manage memory alignments and generic pointer arithmetics in a portable way in C?Need help understanding pointer arithmeticMemory allocation for array of structure pointersDifference in memory block layout allocated by malloc and calloc?Why does “The C Programming Language” book say I must cast malloc?
Can two aircraft be allowed to stay on the same runway at the same time?
My colleague treats me like he's my boss, yet we're on the same level
Why haven't the British protested Brexit as ardently as the Hong Kong protesters?
How to differentiate between two people with the same name in a story?
Tikz: Draw simplified BLE-Stack
Can a level 20 Berserker barbarian use the Frenzy feature all day with one use?
What caused the end of cybernetic implants?
Do universities maintain secret textbooks?
IList<T> implementation
Why does the U.S. military maintain their own weather satellites?
What is the practical impact of using System.Random which is not cryptographically random?
A vector is defined to have a magnitude and *a* direction, but the zero vector has no *single* direction. So, how is the zero vector a vector?
Can UV radiation be safe for the skin?
The correct way of compute indicator function in Mathematica
Doubt about a particular point of view on how to do character creation
Where does MyAnimeList get their data from?
What is the following VRP?
How can I portray a character with no fear of death, without them sounding utterly bored?
Could a complex system of reaction wheels be used to propel a spacecraft?
Can you use Apple Care+ without any checks (bringing just MacBook)?
Using font to highlight a god's speech in dialogue
Understanding data transmission rates over copper wire
Is Borg adaptation only temporary?
LINQ Extension methods MinBy and MaxBy
How to access header for memory block via pointer arithmetic?
How do function pointers in C work?Dynamic allocation (malloc) of contiguous block of memoryhow 'free' works when pointer is incrementedWhy isn't there a “memsize” in C which returns the size of a memory block allocated in the heap using malloc?Can a local variable's memory be accessed outside its scope?How to manage memory alignments and generic pointer arithmetics in a portable way in C?Need help understanding pointer arithmeticMemory allocation for array of structure pointersDifference in memory block layout allocated by malloc and calloc?Why does “The C Programming Language” book say I must cast malloc?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am working on a homework problem which consists of making a c program that a user can use to manage memory. Essentially we are trying to mimic what malloc() and free() do in our own way. The function I am currently working on is an initmemory(int size) function that allocates the overall block that the user will use, and from that block, smaller blocks will be allocated as the program calls myalloc() funciton (basically our version of malloc()). My question is, I am trying to access the header portion of the overall block in order to save the size and allocation status of the block, but when I try to perform pointer arithmetic, I end up just moving one bit. How can I access the header in order to save the size and allocation status of the block with my pointer variable startOfMemory
void initmemory(int size)
printf("this is the initial size: %dn", size);
//realSize = size + initial padding + anchorHeader + sentinelBlock
int realSize = size + 12;
printf("I am the new realSize: %dn", realSize);
//checks how many remainders are left
int check = realSize % 8;
printf("this is the value of check: %dn", check);
//will only change realSize if check is not zero
if(check != 0)
//adds enough bytes to satisfy 8-byte alignment
realSize = realSize + (8 - check);
/*
* this is only to make sure realSize is 8-byte aligned, it should not run
* unless the above code for some reason does not run
*/
check = realSize % 8;
while(check != 0)
realSize = realSize + (8-check);
check = realSize % 8;
printf("I'm in the while check loop");
// initializes the memory to be allocated.
void *startOfMemory = malloc(realSize);
void *placeOfHeader = startOfMemory - 1;
printf("my memory location is at: %pn", startOfMemory);
printf("my realSize is: %dn", realSize);
printf("memory location of placeOfHeader: %pn", placeOfHeader);
free(startOfMemory);
int main()
initmemory(5);
return 0;
the memory location for startOfMemory, which calls the malloc() fuction is at 0x87a3008 (which makes sense because of the 8 byte alignment)
when I do pointer arithmetic, as in the variable place of header, the memory location of placeOfHeader is at 0x87a3007.
c pointers memory-management heap-memory
add a comment |
I am working on a homework problem which consists of making a c program that a user can use to manage memory. Essentially we are trying to mimic what malloc() and free() do in our own way. The function I am currently working on is an initmemory(int size) function that allocates the overall block that the user will use, and from that block, smaller blocks will be allocated as the program calls myalloc() funciton (basically our version of malloc()). My question is, I am trying to access the header portion of the overall block in order to save the size and allocation status of the block, but when I try to perform pointer arithmetic, I end up just moving one bit. How can I access the header in order to save the size and allocation status of the block with my pointer variable startOfMemory
void initmemory(int size)
printf("this is the initial size: %dn", size);
//realSize = size + initial padding + anchorHeader + sentinelBlock
int realSize = size + 12;
printf("I am the new realSize: %dn", realSize);
//checks how many remainders are left
int check = realSize % 8;
printf("this is the value of check: %dn", check);
//will only change realSize if check is not zero
if(check != 0)
//adds enough bytes to satisfy 8-byte alignment
realSize = realSize + (8 - check);
/*
* this is only to make sure realSize is 8-byte aligned, it should not run
* unless the above code for some reason does not run
*/
check = realSize % 8;
while(check != 0)
realSize = realSize + (8-check);
check = realSize % 8;
printf("I'm in the while check loop");
// initializes the memory to be allocated.
void *startOfMemory = malloc(realSize);
void *placeOfHeader = startOfMemory - 1;
printf("my memory location is at: %pn", startOfMemory);
printf("my realSize is: %dn", realSize);
printf("memory location of placeOfHeader: %pn", placeOfHeader);
free(startOfMemory);
int main()
initmemory(5);
return 0;
the memory location for startOfMemory, which calls the malloc() fuction is at 0x87a3008 (which makes sense because of the 8 byte alignment)
when I do pointer arithmetic, as in the variable place of header, the memory location of placeOfHeader is at 0x87a3007.
c pointers memory-management heap-memory
Hm,0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?
– Ctx
Mar 27 at 23:13
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type.int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.
– KL-Yang
Mar 28 at 6:55
add a comment |
I am working on a homework problem which consists of making a c program that a user can use to manage memory. Essentially we are trying to mimic what malloc() and free() do in our own way. The function I am currently working on is an initmemory(int size) function that allocates the overall block that the user will use, and from that block, smaller blocks will be allocated as the program calls myalloc() funciton (basically our version of malloc()). My question is, I am trying to access the header portion of the overall block in order to save the size and allocation status of the block, but when I try to perform pointer arithmetic, I end up just moving one bit. How can I access the header in order to save the size and allocation status of the block with my pointer variable startOfMemory
void initmemory(int size)
printf("this is the initial size: %dn", size);
//realSize = size + initial padding + anchorHeader + sentinelBlock
int realSize = size + 12;
printf("I am the new realSize: %dn", realSize);
//checks how many remainders are left
int check = realSize % 8;
printf("this is the value of check: %dn", check);
//will only change realSize if check is not zero
if(check != 0)
//adds enough bytes to satisfy 8-byte alignment
realSize = realSize + (8 - check);
/*
* this is only to make sure realSize is 8-byte aligned, it should not run
* unless the above code for some reason does not run
*/
check = realSize % 8;
while(check != 0)
realSize = realSize + (8-check);
check = realSize % 8;
printf("I'm in the while check loop");
// initializes the memory to be allocated.
void *startOfMemory = malloc(realSize);
void *placeOfHeader = startOfMemory - 1;
printf("my memory location is at: %pn", startOfMemory);
printf("my realSize is: %dn", realSize);
printf("memory location of placeOfHeader: %pn", placeOfHeader);
free(startOfMemory);
int main()
initmemory(5);
return 0;
the memory location for startOfMemory, which calls the malloc() fuction is at 0x87a3008 (which makes sense because of the 8 byte alignment)
when I do pointer arithmetic, as in the variable place of header, the memory location of placeOfHeader is at 0x87a3007.
c pointers memory-management heap-memory
I am working on a homework problem which consists of making a c program that a user can use to manage memory. Essentially we are trying to mimic what malloc() and free() do in our own way. The function I am currently working on is an initmemory(int size) function that allocates the overall block that the user will use, and from that block, smaller blocks will be allocated as the program calls myalloc() funciton (basically our version of malloc()). My question is, I am trying to access the header portion of the overall block in order to save the size and allocation status of the block, but when I try to perform pointer arithmetic, I end up just moving one bit. How can I access the header in order to save the size and allocation status of the block with my pointer variable startOfMemory
void initmemory(int size)
printf("this is the initial size: %dn", size);
//realSize = size + initial padding + anchorHeader + sentinelBlock
int realSize = size + 12;
printf("I am the new realSize: %dn", realSize);
//checks how many remainders are left
int check = realSize % 8;
printf("this is the value of check: %dn", check);
//will only change realSize if check is not zero
if(check != 0)
//adds enough bytes to satisfy 8-byte alignment
realSize = realSize + (8 - check);
/*
* this is only to make sure realSize is 8-byte aligned, it should not run
* unless the above code for some reason does not run
*/
check = realSize % 8;
while(check != 0)
realSize = realSize + (8-check);
check = realSize % 8;
printf("I'm in the while check loop");
// initializes the memory to be allocated.
void *startOfMemory = malloc(realSize);
void *placeOfHeader = startOfMemory - 1;
printf("my memory location is at: %pn", startOfMemory);
printf("my realSize is: %dn", realSize);
printf("memory location of placeOfHeader: %pn", placeOfHeader);
free(startOfMemory);
int main()
initmemory(5);
return 0;
the memory location for startOfMemory, which calls the malloc() fuction is at 0x87a3008 (which makes sense because of the 8 byte alignment)
when I do pointer arithmetic, as in the variable place of header, the memory location of placeOfHeader is at 0x87a3007.
c pointers memory-management heap-memory
c pointers memory-management heap-memory
asked Mar 27 at 23:07
dannyBandsdannyBands
1
1
Hm,0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?
– Ctx
Mar 27 at 23:13
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type.int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.
– KL-Yang
Mar 28 at 6:55
add a comment |
Hm,0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?
– Ctx
Mar 27 at 23:13
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type.int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.
– KL-Yang
Mar 28 at 6:55
Hm,
0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?– Ctx
Mar 27 at 23:13
Hm,
0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?– Ctx
Mar 27 at 23:13
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type. int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.– KL-Yang
Mar 28 at 6:55
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type. int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.– KL-Yang
Mar 28 at 6:55
add a comment |
1 Answer
1
active
oldest
votes
placeOfHeader is not somewhere in the allocated region.
You probably want to write something like this.
//alloc(realSize)
void *placeOfHeader = malloc(realSize);
*((size_t*)placeOfHeader) = realSize;
void* startOfMemory = (size_t*)placeOfHeader + 1;
return startOfMemory;
//free(startOfMemory)
void* placeOfHeader = (size_t*)startOfMemory - 1;
size_t realSize = *((size_t*)placeOfHeader);
free(placeOfHeader)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387834%2fhow-to-access-header-for-memory-block-via-pointer-arithmetic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
placeOfHeader is not somewhere in the allocated region.
You probably want to write something like this.
//alloc(realSize)
void *placeOfHeader = malloc(realSize);
*((size_t*)placeOfHeader) = realSize;
void* startOfMemory = (size_t*)placeOfHeader + 1;
return startOfMemory;
//free(startOfMemory)
void* placeOfHeader = (size_t*)startOfMemory - 1;
size_t realSize = *((size_t*)placeOfHeader);
free(placeOfHeader)
add a comment |
placeOfHeader is not somewhere in the allocated region.
You probably want to write something like this.
//alloc(realSize)
void *placeOfHeader = malloc(realSize);
*((size_t*)placeOfHeader) = realSize;
void* startOfMemory = (size_t*)placeOfHeader + 1;
return startOfMemory;
//free(startOfMemory)
void* placeOfHeader = (size_t*)startOfMemory - 1;
size_t realSize = *((size_t*)placeOfHeader);
free(placeOfHeader)
add a comment |
placeOfHeader is not somewhere in the allocated region.
You probably want to write something like this.
//alloc(realSize)
void *placeOfHeader = malloc(realSize);
*((size_t*)placeOfHeader) = realSize;
void* startOfMemory = (size_t*)placeOfHeader + 1;
return startOfMemory;
//free(startOfMemory)
void* placeOfHeader = (size_t*)startOfMemory - 1;
size_t realSize = *((size_t*)placeOfHeader);
free(placeOfHeader)
placeOfHeader is not somewhere in the allocated region.
You probably want to write something like this.
//alloc(realSize)
void *placeOfHeader = malloc(realSize);
*((size_t*)placeOfHeader) = realSize;
void* startOfMemory = (size_t*)placeOfHeader + 1;
return startOfMemory;
//free(startOfMemory)
void* placeOfHeader = (size_t*)startOfMemory - 1;
size_t realSize = *((size_t*)placeOfHeader);
free(placeOfHeader)
edited Mar 28 at 9:27
answered Mar 27 at 23:24
cprogrammercprogrammer
3,5253 gold badges26 silver badges48 bronze badges
3,5253 gold badges26 silver badges48 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387834%2fhow-to-access-header-for-memory-block-via-pointer-arithmetic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hm,
0x87a3008 - 1 = 0x87a3007
. What exactly surprises you? What do you expect and why?– Ctx
Mar 27 at 23:13
@Ctx well I want to access the previos 4 bytes in order to add the size and allocation status of the block of memory, I am under the impression that if I did the pointer arithmetic that you highlighted, it would move me back 4 bytes. Is that only when the block of memory is allocated as an array? Or is it because the pointer type is void?
– dannyBands
Mar 28 at 3:21
int32_t *p = (int32_t*)startOfMemory-1;
, will move forward 4 bytes, the pointer arithmetic is related with pointer type.int64_t *p = (int64_t*)startOfMemory-1;
will move forward 8 bytes.– KL-Yang
Mar 28 at 6:55