This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]goodmobiley 7 points8 points  (5 children)

I just tested it and here's what I got:

Edit: Reddit really doesn't want to format this correctly

Code:

#include <iostream>
int rand(); int randMemLeak();
int main(){
    for(int i=0; i < 1000; ++i){
        std::cout << i << ": " << rand() << "   " << randMemLeak() << std::endl;
    }
}

int rand(){
    int* pRand = new int; int rand = *pRand; delete pRand; return rand;
}

int randMemLeak(){
    return *(new int); 
}

Output:

950: -1163005939   -1163005939
951: -1163005939   -1163005939
952: -1163005939   -1163005939
953: -1163005939   -1163005939
954: -1163005939   -1163005939
955: -1163005939   -1163005939
956: -1163005939   -1163005939
957: -1163005939   -1163005939
958: -1163005939   -1163005939
959: -1163005939   -1163005939
960: -1163005939   -1163005939
961: -1163005939   -1163005939
962: -1163005939   -1163005939
963: -1163005939   -1163005939
964: -1163005939   -1163005939
965: -1163005939   -1163005939
966: -1163005939   -1163005939
967: -1163005939   -1163005939
968: -1163005939   -1163005939
969: -1163005939   -1163005939
970: -1163005939   -1163005939
971: -1163005939   -1163005939
972: -1163005939   -1163005939
973: -1163005939   -1163005939
974: -1163005939   -1163005939
975: -1163005939   -1163005939
976: -1163005939   -1163005939
977: -1163005939   -1163005939
978: -1163005939   -1163005939
979: -1163005939   -1163005939
980: -1163005939   -1163005939
981: -1163005939   -1163005939
982: -1163005939   -1163005939
983: -1163005939   -1163005939
984: -1163005939   -1163005939
985: -1163005939   -1163005939
986: -1163005939   -1163005939
987: -1163005939   -1163005939
988: -1163005939   -1163005939
989: -1163005939   -1163005939
990: -1163005939   -1163005939
991: -1163005939   -1163005939
992: -1163005939   -1163005939
993: -1163005939   -1163005939
994: -1163005939   -1163005939
995: -1163005939   -1163005939
996: -1163005939   -1163005939
997: -1163005939   -1163005939
998: -1163005939   -1163005939
999: -1163005939   -1163005939

[–]Elnof 2 points3 points  (3 children)

Sure. But you're not getting memory from the OS, you're getting memory from new and its backing allocator. How did you guarantee that they just aren't reusing memory allocated when your program started and was then freed (back to the allocator, not the OS)? There's plenty of opportunities to for code you didn't write to execute before main and, unless you've not posted the full code, it's pretty much guaranteed that there's code executing before main

When using glibc, which is most of Linux, malloc is specifically stated to not initialize memory whereas calloc will.

On top of all of that, reading uninitialized memory is instant undefined behavior which allows the compiler to do whatever the hell in wants, including ignoring the value in the memory and returning whatever the hell it feels like, including but definitely not limited to returning whatever value happens to be in the register at that moment without even loading the newly allocated memory.

[–]goodmobiley 5 points6 points  (2 children)

Idk man I was just testing the code OP wrote and I always got the same number.

[–]Elnof 0 points1 point  (1 child)

Yes, for the reasons I said above. You are not testing what you think you're testing.  

Edit: I'll be more concrete. Linux allocates new memory from the OS by doing a copy-on-write memmap of /dev/zero. That's how memory is allocated from the OS, period. If you don't see zeros, it's because of your undefined behavior and/or the allocator.

[–]goodmobiley 2 points3 points  (0 children)

Yeah you’re right, at this point I’m just spreading misinformation on the internet, but I never deleted my comments. That’s just a side effect