Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2098221
  • 博文数量: 249
  • 博客积分: 1305
  • 博客等级: 军士长
  • 技术积分: 4733
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-17 10:37
个人简介

不懂的东西还有很多,随着不断的学习,不懂的东西更多,无法消灭更多不懂的东西,那就不断的充实自己吧。 欢迎关注微信公众号:菜鸟的机器学习

文章分类

全部博文(249)

文章存档

2015年(1)

2014年(4)

2013年(208)

2012年(35)

2011年(1)

分类: C/C++

2013-08-17 22:32:27


    C programming : How does free know how much to free?
    In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to some function, I have to also pass the size (ie an array of 10 elements needs to receive 10 as a parameter to know the size of the array), but I do not have to pass the size to the free function. 
    问题:在C语言中,free()函数怎么知道释放多大的空间?
    在C语言中,可以传递任意类型的指针作为free()函数的参数,但是free()函数怎么知道需要释放多少已分配的内存空间呢?当传递一个指针给某些函数的同时,不得不传递相应的所占空间的大小(例如,10个char类型的元素组成的数组,作为函数参数的同时,需要将其长度10作为函数参数传递),但是,并没有传递给free()指针所指向空间的大小啊。

    解答一:
    When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can't (reliably) access that other information - and nor should you :-).
    When you call free(), it simply looks at the extra information to find out how big the block is.
    当调用malloc()时,可以指定分配的内存量。实际上使用的内存量要略微多于指定的内存量,多出的内存量用来存储额外的信息,至少记录分配的块的大小......
    当我们调用free()时,它通过查看这些额外的信息来知道所分配的内存的大小。
   
    引自:    解答二:
     The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the bounds of the allocated block are even slightly overstepped; .)
    大意是说:malloc()函数实现时,记录了它所分配的内存大小,因此,当调用free()函数来释放内存时,不一定需要把分配内存大小传递给它。(通常情况下,存储size的内存与所分配的内存相邻,这就是为什么如果访问超出了所分配内存的边界就会造成严重后果的原因。)
    
    引自:



    下面是一个类似的问题:
    I've been wondering how delete[] knows how much memory to remove from the heap?
    Lets say I have char* szArr = new char[num]; where num = 20;
    Then I want to delete this array later, I call delete[] szArr and not delete szArr.  Is there some table that is kept of items in an array somewhere? AFAIK outside of local scope (局部范围之外)it's not possible to use sizeof on an array, simply because it only returns the 4bytes of the pinter.
    If delete[] knows how many addresses are being reserved, is it possible then to make some kind of "howmany[]" to check this same table of information to find out how many items are in an array outside of the local scope where the array was created? This could save having to pass a reference to the array and the count of items to another function trying to work with the array.
   
     解答:
    
    上述的大致意思如下:
    (1)>I've been wondering how delete[] knows how much memory to remove from the heap?
    内存管理器为每一个动态分配的内存开始的位置附加一个控制块,该控制块包含着一些必要的信息。因此,当你调用new/new[]/malloc时,实际上分配的内存要比所请求的内存多一些。
    所以,当你调用free/delete/delete []时,内存管理器就可以根据控制块中的信息,来释放先前所分配的内存。

    (2)>AFAIK outside of local scope it's not possible to use sizeof on an array, simply because it only returns the 4bytes of the pinter.
   即使在局部范围内,也不能使用sizeof()来获取动态分配数组的大小。记住:sizeof只有在编译时刻被解析,而不是在运行时刻。这就是为什么sizeof只运用在静态分配的数组之上。
    
    char sarray[100]; //sizeof can work with this (at compile)
    
    可以使用Microsoft-specific函数_msize()通过指针来获取动态分配数组的大小,即使当你把指针传递到另一个函数中,而不是在其所分配的函数中,也可以获取的。

    引自:
    
阅读(1301) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~