CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    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

  2. #2
    Join Date
    May 2006
    Posts
    7

    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.

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    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

  4. #4
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: retrieving size bit

    Quote 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

  6. #6
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  7. #7
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: retrieving size bit

    i dont know what your trieing todo... but i think it requires bit shifting..

  8. #8
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up 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"

  9. #9
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: retrieving size bit

    i dont see any malloc() code though..

  10. #10
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

    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

  11. #11
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: retrieving size bit

    thats true, to bad there is no standard for this or something

  12. #12
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

    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
  •  





Click Here to Expand Forum to Full Width

Featured