|
Off Topic Forum Discuss anything and everything in this forum. |
![]() |
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
![]() |
#1
![]() |
![]() Fiendian: Yes, the OS clears the heap of a program completely when it is shut down.
Viper: Java uses garbage collection and reference semantics, meaning that everything but the most simple data type is always handed over by reference. It's like using pointers almost exclusively, just without that syntax. Fiendian: Gc and pointers are not mutually exclusive. You can design a system where you manage pointers in a central manner and count their references and deallocate stuff. Many libraries do that. Fiendian: Java does give the programmer less control about the memory management, yes. Fiendian: In C#, you sometimes have to worry about pointers too, especially in "unsafe" passages that do system stuff. Fiendian: There is no special syntax in Java to deep-copy function parameters. You'd just have to implement a clone function. You have to do that in C++ too btw, to do it properly (copy constructor). Fiendian: The use of "this" in Java and C++ is alike. Viper: Finals are _not_ the java version of constants. The final limitation merely indicates that a variable will not be reassigned again (basic data types will carry the same value for eternity, reference types will just not ever point to something else again). You can still run operations on the objects those vars point to and change their internal state. In C++, const is a concept that goes through the whole language, and if you declare something const, it usually means that it won't be changed a bit anymore, not even in its contents. It is good practice to declare whatever you can as final or const, as that shows you on first look some additional info about your stuff. *edit About pointers: Suppose this is your RAM, as seen by the application: [0, dv0], [1, dv1], [2, dv2], [3, dv3]. Suppose it has only 4 integer slots. At address 0, there is the data value 0. Now you have int x = 600; The compiler could link your variable with an address in your RAM: [0, dv0], [1, 666], [2, dv2], [3, dv3] Now whenever you use x, you actually use the data value in slot 1. You reference that slot directly (reading and writing the value in that thing). In this environment, the address of x (&x) would be 1. Suppose you had a pointer int *y = &x; And suppose the compiler would link your y with the slot 3. [0, dv0], [1, value of x = 666], [2, dv2], [3, value of y = 1] The value in the slot 3 now contains the address of the variable x. When you assign to y in the C++ world, you work with the value in slot 3 directly: y = (*int)(0); //y would now point at slot 0; the content of slot 3 would now be "0". When you work with *y, you work not with the slot that y itself is stored in, but the slot that y points to. You address that target slot indirectly: [0, dv0], [1, value of int x = 666], [2, dv2], [3, value of int *y = 1] *y = 200; [0, dv0], [1, value of int x = 200], [2, dv2], [3, value of int *y = 1] If you declare a variable int &z = x;, you actually merely alias x. The program will behave just as if you had used x instead of z everywhere, so z doesn't even get a slot. Yes, & has many meanings. Just as "static" has many meanings too. If y points at slot 0 = NULL = nullptr, then that in itself isn't a bad thing, but the operating system usually makes sure that your application has no access to slot 0. That's why you get a memory access error, especially when you try to write to it. If you return the reference of a stack entry (returning & on something from a function), then that in itself will not cause a crash. It may just be that at the time you access that target slot, it doesn't belong to your application for the moment. But you can return such a slot and pass that address to another function call (which then, in turn, uses that part of the stack again), and then happily access that slot and wreck everything, without a crash. In Java, you can usually call member methods on unallocated objects: MyClass mc1 = null; mc1.method1("abc"); It will yield an NPE at the precise moment that you try accessing a member variable in method1. If you don't do that, there should be no crash (as the implicit "this" variable, carrying the value null, is not referenced).
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() Last edited by Shrinker; 04-21-2012 at 04:06 AM. |
|
![]() |
![]() |
#2
![]() |
![]() Ahh, pointers... it's hard with them, and even harder without them.
__________________
My Showcase Thread - 2D Game Development My Blog - antonior-software.blogspot.com |
|
![]() |
![]() |
#3
![]() |
![]() So what you're saying, pointers are a lot like women?...
|
|
![]() |
![]() |
#4
![]() |
![]() Damn Shrinker, you think viper will get this? I tried to explain it to him yesterday, and with less confusing examples to at least give him the basic idea.
![]() Referencing a "random" memory space (just declaring a pointer and dereference it without assigning it to a properly allocated variable/object) may or may not cause a crash, it is basically undefined behaviour, but more often than not if I do this I just get a random number if I'm using an int pointer, usually one that is extremely large or extremely small (near the sign boundaries). If you have a pointer that is not pointing at a class instance but is "supposed to", then if you try dereference it for one of its members, like (*obj).name / obj->name, then you will most certainly get a crash because there is no such member when the object itself doesn't exist. If there is a compiler that can see this coming and let the programmer know I would be glad to try it out. ![]() I had this happen a lot during my Quake 2 coding these days, probably mostly because it was 5 AM and I was tired. ![]() EDIT: Now that I read your post again, it actually is a pretty good way of explaining it, with the memory address examples. I never mentioned that pointers themselves are variables that need to be allocated as such to memory. Pointer pointing at a pointer pointing at a pointer... Last edited by Fiendian; 04-21-2012 at 01:31 PM. |
|
![]() |
![]() |
#5
![]() |
![]()
Exactly, sometimes pointers can be a real bitch
![]() Edit: To keep things simple think of a pointer as a link to a web page. Link is just a shortcut to a web page, not an instance of it. If the page exists(link is valid) you can access it and browse its contents. If it doesn't exists, you get the "web page not found", or in your case the program crashes because you are accessing something that isn't there.
__________________
My Showcase Thread - 2D Game Development My Blog - antonior-software.blogspot.com Last edited by AntonioR; 04-21-2012 at 03:13 PM. |
|
![]() |
![]() |
#6
![]() |
![]()
Pointer pointing at a pointer pointing at a pointer... [pointing at a float value]:
[0: ?, 1: ?, 2: ?, 3: ?, 4: ?, 5: ?] //float value: float x = 23.5f; //assigned memory slot 3 [0: ?, 1: ?, 2: ?, 3: x = 23.5f, 4: ?, 5: ?] //pointer to it float *px = &x; //assigned memory slot 5 [0: ?, 1: ?, 2: ?, 3: x = 23.5f, 4: ?, 5: px = 3] //pointer to that float *ppx = &px; //assigned memory slot 0 [0: ppx = 5, 1: ?, 2: ?, 3: x = 23.5f, 4: ?, 5: px = 3] //pointer to that float *pppx = &ppx; //assigned memory slot 2 [0: ppx = 5, 1: ?, 2: pppx = 0, 3: x = 23.5f, 4: dragon = cookies, 5: px = 3]
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#7
![]() |
![]() How about a pointer - pointer #1, pointing to a pointer - pointer #2, which in turn is pointing to another pointer - pointer #3, which is pointing at pointer #1?
|
|
![]() |
![]() |
#8
![]() |
![]() float x = 600;
float *px = &x; *(int*)px |= 0x80000000; x = ?
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#9
![]() |
![]() @evil_Vasile:
void *p1, *p2, *p3; p1 = &p2; p2 = &p3; p3 = &p1;
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#10
![]() |
![]() What's the point?
![]() |
|
![]() |
![]() |
#11
![]() |
![]() Would rather learn this than Matlab right about now...
__________________
"Don't you have to be told to do things to be considered a slave?" |
|
![]() |
![]() |
#12
![]() |
![]() You're learning Matlab too? I've been having tons of classes that involve Matlab for the last couple of semesters - I find it quite interesting/useful, although it is a memory hog...
|
|
![]() |
![]() |
#13
![]() |
![]() Understanding pointers is incredibly useful. Thanks to those I have been able to far exceed what I could do in the past with Quake 2 modding. But I do make mistakes on a regular basis, and some times don't discover them until after a fair bit of hair pulling. However structs are not far behind, after learning those the game's code suddenly makes sense.
Note to self: Compile and test often, don't write shitloads of code into many files and 'then' compile and test. ![]() |
|
![]() |
![]() |
#14
![]() |
![]()
__________________
I'm a cat by the way. I'm also everyone's most hated critic. (Opinions expressed are my own) Nimble Writer - now available on Steam, Itch.io.
CodeCat, my blog that I need to update - I also have a showcase, which I barely update. (I do not respond to PM's about Serious Sam Revolution or Bogus Detour. Please use the appropriate community hubs for that.) |
|
![]() |
![]() |
#15
![]() |
![]() Good point.
|
|
![]() |
![]() |
#16
![]() |
![]() Stop the pointless chatter already!
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#17
![]() |
![]() There is no use in pointing fingers at any one, Shrinker.
|
|
![]() |
![]() |
#18
![]() |
![]() |
|
![]() |
![]() |
#19
![]() |
![]()
Google is your friend. Matlab is a mathematics programming/calculation tool.
http://en.wikipedia.org/wiki/MATLAB
__________________
"Don't you have to be told to do things to be considered a slave?" |
|
![]() |
![]() |
#20
![]() |
![]() |
|
![]() |
![]() |
#21
![]() |
![]() Hey does anyone here know of a good alternative to rand() for C? Its range is really small (0 - 2^15), and its distribution isn't really that good.
I was able to extend it to a twice larger number (in decimal digits) using two calls to rand() and "merging" them together with simple bitwise operation, and when I mass test both methods with a for-loop and calculate the averages from all the random generations summed up / number of loop iterations they do come out pretty close more often than not, but still the rand() function really likes the bigger numbers over the smaller ones when the range is large. There is Boost for C++ which I read about on Stack Overflow but that one's definitely not compatible with classic C. Last edited by Fiendian; 04-25-2012 at 07:29 PM. |
|
![]() |
![]() |
#22
![]() |
![]() http://www.cplusplus.com/reference/c.../cstdlib/rand/
This here says that it prefers lower numbers ![]() Try downscaling it to [0, 1) with the RAND_MAX thingo and then upscaling it to the range you need... works for my engine for years already.
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#23
![]() |
![]() To get a random number in the range min to max I tried this:
((float)(rand ( )) / RAND_MAX) * (max - min) + min This does as advertised but not very well, even worse than my old method. ![]() |
|
![]() |
![]() |
#24
![]() |
![]() The only criticism I can give here is that you should divide by (RAND_MAX+1), because removing the maximum value from the interval is a lot better for all practical applications.
What precisely is your problem with that method now? Are you aware of cycles in pseudorandom numbers (i.e. if you use a bad srand value, you might end up with a too short cycle?)? This is my beloved random function, returning float values within 0 inclusive to 1 exclusive: Code:
float frand() { return rand()/float(RAND_MAX+1); }
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#25
![]() |
![]() Yes, I know of cycles, in the simplest of pseudo-random generators I got to know in uni had a cycle of maybe 5 numbers before repetition began.
![]() There is nothing inherently wrong with this method, I'm just lazy and not writing any more code to further play with the result... EDIT: I added these: float rnd = random ( ); // random ( ) makes a float between 0 and 1. if (rnd < 0.15) result = base; else if (rnd < 0.8 && result/2.0 >= min) result /= 2.0; I added an else if for 4.0 before the 2.0 else if but the results were a tad too low on average so I removed it again. This seems to give me more frequently small-ish numbers while still offering larger numbers as a possibility in line with max. ![]() Last edited by Fiendian; 04-27-2012 at 10:42 AM. |
|
![]() |
![]() |
#26
![]() |
![]() Fiendian, when need only single precision floats (32bit), and not doubles (64bit), always append an "f" to your literals (0.15f), otherwise you convert the whole expression to double precision unnecessarily where it is not necessary.
__________________
Homepage | Twitter The Beautiful Nature Lights | Echoes Arrival Mix | Tree of Life ![]() |
|
![]() |
![]() |
#27
![]() |
![]() Thanks for the tip, I didn't know that.
|
|
![]() |
![]() |
Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | Search this Thread |
Display Modes | Rate This Thread |
|
|