0

デルのワークステーションでLinuxUbuntuでgccを使用し、LenovoワークステーションでMicrosoft Visual C ++を使用しましたが、以下の違いについて説明します。

同僚が独自のmallocを作成したこともあり、メモリ割り当てにはどの戦略があるのだろうか。メモリ内の場所を割り当てるためのさまざまな戦略があるようです。(g)cc、nmakeなどにも違いがあるようです。たとえば、(g)ccは、解放された古い割り当てを無視し、新しく解放されたリソースを割り当てるように見えます。これは、Microsoft VisualC++での外観です。

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 戦略が異なることは正しいです...なぜそれらが同じであると期待するのでしょうか。言語は、コンパイラが必要としてコードを解釈する必要があるものを定義するだけであり、コンパイラは、ターゲット OS/マシン タイプに合わせてその動作を最適に調整する方法を管理する必要があります。

于 2012-10-17T06:31:12.080 に答える
0

MSVCの同時実行 CRTは、設計上、従来の FIFO アロケーターの代わりに LIFO で最適化されたキャッシュの再利用を提供するSLAB アロケーターを使用する glibc の ptmalloc3 アロケーターと同様のパターンを提供します。

于 2012-10-17T14:22:44.340 に答える