Thread: rand and srand

  1. #16
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Wow...now that I test that, I guess I really don't understand memory allocation as much as I thought I did.

    edit: TactX, your suggestion didn't work either. I get two completely identical outputs on two seperate runs of the same program, both runs occuring in the same second.
    Last edited by pianorain; 10-18-2005 at 09:34 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Holy !@#$. Fine.
    Okay then, be an ............... I'll remember how grateful you are to volunteers helping you in their spare time and refrain from ever helping you in the future.

    *plonk!*
    My best code is written with the delete key.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Okay then, be an ............... I'll remember how grateful you are to volunteers helping you in their spare time
    I am grateful to people who help. I am grateful to cwr, lemnisca, TactX and pianorain. As I said "Whatever floats your boat" is not helpful. And since then all you've done is mix very little information with lots of sarcasm. It shouldn't be any wonder that my frustration gets the better of me when responding to those kinds posts.

  4. #19
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Quote Originally Posted by pianorain
    edit: TactX, your suggestion didn't work either. I get two completely identical outputs on two seperate runs of the same program, both runs occuring in the same second.
    Yeah, I've tested it too and got the same results (testet on Linux/gcc and Windows/VS6).

    But I can't really explain why the allocated memory is identical in two subsequent calls of the programm even if it is modified by the programm. What am i missing? Can anyone explain that, please?

    Edit: typo
    Last edited by TactX; 10-18-2005 at 10:49 AM.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can anyone explain that, please?
    Each process uses "fake" addresses, rather than the real addresses that the fake addresses are mapped to in physical (or virtual) memory. That's why the malloc solution wouldn't work well in practice. Can you be more specific about your second solution (code if possible)? Because it reeks of undefined behavior.
    My best code is written with the delete key.

  6. #21
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Prelude
    Because it reeks of undefined behavior.
    Reeks? No, I'm fairly certain that it is. Here's what I tried:
    Code:
    #include <iostream>
    #include <ctime>
    #include <windows.h>
    
    using std::endl;
    using std::cout;
    
    int main()
    {
    	unsigned int *p = new unsigned int;
    
    /*
    used just so that I have time to start the 
    program twice in a second
    */
    	Sleep(1000); 
    
    /*
    between incrementing an uninitialized value
    and the actual pointer address,
    surely something here is unique
    */
    	cout << ++(*p) << " " << (int)p << " " << time(NULL) << endl;
    	delete p;
    	return 0;
    }
    I'd show you a sample output, except it's pretty meaningless. I think it's enough to say that in the few times I ran the program, it always showed the same values except for the time. When ran in the same second, everything was the same.

    edit:
    And yes, I'm sorry...that's C++.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    
    int main(void)
    {
    	unsigned int *p = malloc(sizeof(unsigned int));
    	Sleep(1000);
    	printf("%d %d %d\n",++(*p),(int)p,(unsigned long)time(NULL));
    	fflush(stdin);
    	free(p);
    	return 0;
    }
    Last edited by pianorain; 10-18-2005 at 01:49 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Reeks? No, I'm fairly certain that it is.
    Well, there's reasonable doubt, so I didn't want to jump to any conclusions.
    My best code is written with the delete key.

  8. #23
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Bottom line: The C standard does not guarantee a way to always get a unique value every time an instance of the same program is run, so it's impossible to create a 100% portable way to do it.

    Why don't you start saying what you mean and limit your criteria a bit? Most operating systems have a way to get the process ID, even if it's not always through getpid(). Most operating systems also allow you to retrieve the time in ways more granular than seconds. Some operating systems (not many today though) don't use virtual memory so malloc() calls all return a unique address. Come up with a realistic scenario and you're likely to get more definitive responses.
    If you understand what you're doing, you're not learning anything.

  9. #24
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Quote Originally Posted by Prelude
    >Can anyone explain that, please?
    Each process uses "fake" addresses, rather than the real addresses that the fake addresses are mapped to in physical (or virtual) memory. That's why the malloc solution wouldn't work well in practice. Can you be more specific about your second solution (code if possible)? Because it reeks of undefined behavior.
    O.K. the point about memory mapping is clear. What I thought of was (basically the same as pianorains):
    Code:
    int main(void){
    	int *foo;
    	foo=malloc(sizeof(int));	// we believe that malloc() will never fail
    	++(*foo);
    	printf("%d\n",*foo);
    	free(foo);
    
    	return 0;
    }
    It's not clear to me why *foo keeps always the same value when the program is called subsequently, even though it is incremented.

    And btw. I can't see undefined behaviour here. I'm not accessing bad memory or anything else...

    Enlighten me

    Edit: typo

  10. #25
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The memory that malloc() allocates is not guaranteed to be the same between runs even if malloc() is always returning the same fake address. You can't depend on *foo even pointing to the same real memory location as the previous instance (and if the program is running twice at the same time you're guaranteed that it's not the same physical memory being addressed.)
    If you understand what you're doing, you're not learning anything.

  11. #26
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    That's all clear to me. My question is why *foo keeps the same value all the time in subsequent calls. If the physical location of *foo would always be different then *foo should have different values (at least sometime). And if it's always the same physical location, *foo should be +1 for each call.

    PS: My english is not the best. I've had almost no practice in the last years

  12. #27
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can't see undefined behaviour here. I'm not accessing bad memory or anything else...
    You're accessing an undeterminate value, which is undefined behavior. As for why it keeps the same value, I would imagine (speculating wildly) that it's due to the system cleaning up the memory for your process rather than just leaving it with old values from whatever ran last.

    >And if it's always the same physical location, *foo should be +1 for each call.
    The memory location that your pointer really points to is always going to be unique and different, even though each process might see the address as the same. If all foos point to the same real address then that would cause all sorts of trouble.
    My best code is written with the delete key.

  13. #28
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Quote Originally Posted by Prelude
    >I can't see undefined behaviour here. I'm not accessing bad memory or anything else...
    You're accessing an undeterminate value, which is undefined behavior.
    Uhmm, well. In this case I wanted this behaviour, so I cannot call it "undefined". But that's not the point...

    Well for now I'll just accept that it is this way and believe that my OS cleans up my mess

  14. #29
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For all intents and purposes, you cannot use malloc() to share memory between processes reliably. And it doesn't really matter what the OS does with free()d memory from your perspective, as long as it meets the requirements of the C standard. As long as you set the allocated memory to something useful before using it then it doesn't matter if the OS zeroes out unused memory or leaves it the same or even fills it in with the fibonacci sequence. You just can't depend on newly malloc()d memory containing anything you expect it to.
    If you understand what you're doing, you're not learning anything.

  15. #30
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Uhmm, well. In this case I wanted this behaviour, so I cannot call it "undefined".
    Sure you can. Just because it happens to do what you want doesn't mean it's not hopelessly broken.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed