I used gcc with Linux Ubuntu on a Dell workstation and Microsoft Visual C++ with a Lenovo workstation and got some following differences that I would like to explain.
A colleague even wrote an own malloc and I wonder which strategies there are for memory allocation. There seem to be different strategies to allocate locations in memory. There also seems to be a difference between (g)cc, nmake and others. For instance, (g)cc appears to ignore old allocation that was freed and rather allocates newly freed resources. This is how it looks with 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 );
With gcc on Ubuntu(Dell) it looks like this :
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 );
in short:
using MSVC++ :
alloc() got addr1
alloc() got addr2
alloc() got addr3
free(ALL)
alloc() got addr1
using gcc on Ubuntu :
alloc() got addr1
alloc() got addr2
alloc() got addr3
free(ALL)
alloc() got addr3
How does one explain these differences?…
Hmmm. That is interesting, but I’m not surprised. Your sample is very small though. You may want to loop some of those malloc/free commands to see if it’s consistent. You’re right that the m$ and *nix strategies for memory allocation and eventual garbage collection are different… why would you expect them to be the same. The language only defines what the compiler should interpret code as requiring, the compiler then must manage how to best arrange that behaviour for the target OS/machine type.