gpt4 book ai didi

c - 如何解释 gcc 与 nmake 的这些差异?

转载 作者:行者123 更新时间:2023-11-30 15:54:30 25 4
gpt4 key购买 nike

我在 Dell 工作站上使用了 Linux Ubuntu 上的 gcc,在 Lenovo 工作站上使用了 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

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

最佳答案

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

关于c - 如何解释 gcc 与 nmake 的这些差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com