How to store an interger value of 4 bytes in a memory of chunk which is malloced as type char
Posted
by Adi
on Stack Overflow
See other posts from Stack Overflow
or by Adi
Published on 2010-03-25T11:42:28Z
Indexed on
2010/03/25
11:53 UTC
Read the original article
Hit count: 239
type-conversion
|c
Dear all,
Hello Guys!! This is my first post in the forum . I am really looking forward to having good fun in this site. My question is :
int mem_size = 10;
char *start_ptr;
if((start_ptr= malloc(mem_size*1024*1024*sizeof(char)))==NULL) {return -1;}
I have allocated a chunk of memory of type char and size is say 10 MB (i.e mem_size = 10 ); Now I want to store the size information in the header of the memory chunk, To make myself more clear
Lets Say : start_ptr = 0xaf868004 (This is the value I got from my execution, it changes every time)
Now I want to put the size information in the start of this pointer.. i.e *start_ptr = mem_size*1024*1024;
But I am not able to put this information in the start_ptr. I think the reason is because my ptr is of type char which only takes one byte but I am trying to store int which takes 4 bytes, is the problem .
I am not sure how to fix this problem..
I would greatly appreciate your suggestions.
Cheers!!
Aditya
© Stack Overflow or respective owner