0

我在戴尔工作站上使用 gcc 和 Linux Ubuntu,在联想工作站上使用 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(Dell) 上使用 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

如何解释这些差异?...

4

2 回答 2

1

嗯。这很有趣,但我并不感到惊讶。不过,您的样本非常小。您可能想要循环其中一些 malloc/free 命令以查看它是否一致。你是对的,内存分配和最终垃圾收集的 m$ 和 *nix 策略是不同的......你为什么希望它们是相同的。该语言仅定义编译器应根据需要解释代码的内容,然后编译器必须管理如何为目标操作系统/机器类型最好地安排该行为。

于 2012-10-17T06:31:12.080 回答
0

MSVC 中的并发 CRT将提供与 glibc 中的 ptmalloc3 分配器类似的模式,后者使用SLAB 分配器,通过设计提供优化的缓存重用与 LIFO 而不是传统的 FIFO 分配器。

于 2012-10-17T14:22:44.340 回答