Does nayone know how t retrieve size bit of a malloc/new memory block? i want to make a pointer to this size bit for easier memory management
Printable View
Does nayone know how t retrieve size bit of a malloc/new memory block? i want to make a pointer to this size bit for easier memory management
Hello Mitsukai,
look at "_msize" and "_msize_dbg" functions. However they are not ANSI standart functions.
But I dont understand, why exactly you want to do.
Regards.
they arent standards so i cannot use them. i need to bit shiftthe starting adress you get in C++ to get the pointer to the size bit
where is the size bit present ?
Is it in the beggining of array ?
Is it in the beggining of memory block alocated ?
-Anant
thats the problem no idea where its located. but its somehwere at the start of the memory blockQuote:
Originally Posted by anantwakode
hi Mitsukai,
I am tring this code to find out (size byte) in the beggining of memory block..
but getting something garbage....
Code:#include<iostream.h>
#include<conio.h>
void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
char temp[40]={0};
// take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
remain = decimal % 2;
decimal = decimal / 2; // whittle down decimal
temp[k++] = remain + 0x30; // makes characters 0 or 1
} while (decimal > 0);
//if (neg_flag)
// temp[k++] = '-'; // add back - sign
//else
// temp[k++] = ' ';
/* // or space
int j=32-k;
while(j)
{
// if(j%4==0)
// {
// binary[n++]=' ';
binary[n++]='0';
}
else
binary[n++]='0';
j--;
}*/
while (k >= 0)
{
// if(k%4==0)
// {
// binary[n++]=' ';
binary[n++] = temp[--k];
// }
// else
// binary[n++] = temp[--k];
// reverse spelling
}
binary[n-1] = 0; // end with NULL
}
int main()
{
//int arr1[255];
for(int j=0;j<10;j++)
{
//int arr1[255]={0};
int *temp=new int[j];
//cout<<temp<<endl;
//cout<<--temp<<endl;
--temp;
char arr[33];
dec2bin(*temp,arr);
cout<<*temp<<endl;
cout<<arr<<endl;
cout<<"It should be"<<endl;
cout<<j*4<<endl;
dec2bin((j*4),arr);
cout<<arr<<endl;
//*temp=0;
}
getch();
return 0;
}
-Anant
i dont know what your trieing todo... but i think it requires bit shifting..
Hi Mitsukai,
Here I just reading (base-1) address and trying to analyse and fine where is the size byte is hidden
For that I used dec2bin fun for displaying value at (base-1) in binary format... for bit analysing...
Here I assume that there must be size byte in the beggining of base address of memory block...(allocated memory)
here actually I am getting 4 bytes as size of integer is 4 byte , I using dev-c++
-Anant
i dont see any malloc() code though..
The location of size information for allocated block is very implementation specific. It all depends upon the memory manager of the OS. On Windows it might be at start of allocated memory or Unix it might be somewhere else.
I think its not at all good way to get the size for allocated memory.
Even you able figure out where the size is getting store. Its not safe in future OS might chagne the way its doing things. Tommorow OS might come some totaly diff. implementation of calculating the size.
Thanks,
Vinod
thats true, to bad there is no standard for this or something
I think thats totaly diff. topic of discussion.
Its might create lots of problem if we are trying to define stabdard at binary level.
i am sure there lot of people on this forum will have diff views about it :)
Vinod