|
-
May 11th, 2006, 04:22 AM
#1
retrieving size bit
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
-
May 11th, 2006, 05:39 AM
#2
Re: retrieving size bit
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.
-
May 11th, 2006, 06:41 AM
#3
Re: retrieving size bit
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
-
May 11th, 2006, 07:32 AM
#4
Re: retrieving 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
"Devise the simplest possible solution that solves the problems"
-
May 11th, 2006, 07:42 AM
#5
Re: retrieving size bit
 Originally Posted by anantwakode
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 block
-
May 11th, 2006, 08:11 AM
#6
Re: retrieving size bit
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
"Devise the simplest possible solution that solves the problems"
-
May 11th, 2006, 08:31 AM
#7
Re: retrieving size bit
i dont know what your trieing todo... but i think it requires bit shifting..
-
May 11th, 2006, 08:44 AM
#8
Re: retrieving size bit
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
"Devise the simplest possible solution that solves the problems"
-
May 11th, 2006, 08:49 AM
#9
Re: retrieving size bit
i dont see any malloc() code though..
-
May 11th, 2006, 09:45 AM
#10
Re: retrieving size bit
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
-
May 11th, 2006, 09:50 AM
#11
Re: retrieving size bit
thats true, to bad there is no standard for this or something
-
May 11th, 2006, 09:57 AM
#12
Re: retrieving size bit
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|