我在戴尔工作站上使用了Linux Ubuntu的gcc,在联想工作站上使用了Microsoft Visual C ++,并且有一些我想解释的差异。
一位同事甚至写了一个自己的malloc,我想知道有哪些策略用于内存分配。在内存中分配位置似乎有不同的策略。 (g)cc,nmake和其他人之间似乎也存在差异。例如,(g)cc似乎忽略了被释放的旧分配,而是分配了新释放的资源。这就是它在Microsoft Visual C ++中的外观:
Message MA01 from malloc.c: Hello, memory-allocating World!
MA02: Main array successfully allocated, with size 48 bytes.
MA03: Main array malloc returned address 988360 (dec), f14c8 (hex).
MA04: Main array now contains the following string:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 988472 (dec), f1538 (hex).
Number of g's in array is 2.
Executed free( gcountp );
MA05: Cowabunga array successfully allocated, with size 11 bytes.
MA06: Cowabunga array malloc returned address 988472 (dec), f1538 (hex).
MA07: Cowabunga array now contains the following string: Cowabunga!
MA08: Main array now contains the following string:
Cowabunga!
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 988544 (dec), f1580 (hex).
Number of g's in array is 1.
Executed free( gcountp );
MA09: Executed free( arrayp );
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 988360 (dec), f14c8 (hex).
Number of g's in array is 1.
Executed free( gcountp );
MA10: Executed free( extrap );
在Ubuntu(戴尔)上使用gcc,它看起来像这样:
Message MA01 from malloc.c: Hello, memory-allocating World!
MA02: Main array successfully allocated, with size 48 bytes.
MA03: Main array malloc returned address 30273552 (dec), 1cdf010 (hex).
MA04: Main array now contains the following string:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 30273616 (dec), 1cdf050 (hex).
Number of g's in array is 2.
Executed free( gcountp );
MA05: Cowabunga array successfully allocated, with size 11 bytes.
MA06: Cowabunga array malloc returned address 30273616 (dec), 1cdf050 (hex).
MA07: Cowabunga array now contains the following string: Cowabunga!
MA08: Main array now contains the following string:
Cowabunga!
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 30273648 (dec), 1cdf070 (hex).
Number of g's in array is 1.
Executed free( gcountp );
MA09: Executed free( arrayp );
g-count successfully allocated, with size 4 bytes.
g-count malloc returned address 30273648 (dec), 1cdf070 (hex).
Number of g's in array is 1.
Executed free( gcountp );
MA10: Executed free( extrap );
简而言之:
使用MSVC ++:
alloc() got addr1
alloc() got addr2
alloc() got addr3
free(ALL)
alloc() got addr1
在Ubuntu上使用gcc:
alloc() got addr1
alloc() got addr2
alloc() got addr3
free(ALL)
alloc() got addr3
如何解释这些差异?...
答案 0 :(得分:1)
嗯。这很有趣,但我并不感到惊讶。你的样本非常小。您可能想要循环其中一些malloc / free命令以查看它是否一致。对于内存分配和最终垃圾收集的m $和* nix策略是不同的,你是对的......你为什么期望它们是相同的。该语言仅定义编译器应将代码解释为需要的内容,然后编译器必须管理如何最好地为目标OS /机器类型安排该行为。
答案 1 :(得分:0)
MSVC中的concurrency CRT将提供与glibc中的ptmalloc3分配器类似的模式,该模式使用SLAB allocator,它通过设计提供了与LIFO而不是传统FIFO分配器的优化缓存重用。