Seriously!
Go Back   Seriously! Forums > General > Off Topic Forum

Off Topic Forum Discuss anything and everything in this forum.

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 04-21-2012, 04:55 AM   #1  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Shoutbox - Fiendian and viper45

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 | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life


Last edited by Shrinker; 04-21-2012 at 05:06 AM.
  Reply With Quote
Old 04-21-2012, 10:18 AM   #2  
...
 
AntonioR's Avatar
 
Joined: May 2011
 
AntonioR is offline Steam ID: AntonioR Facebook: AntonioR.Software Youtube: antoniorsoftware
Default Re: Shoutbox - Fiendian and viper45

Ahh, pointers... it's hard with them, and even harder without them.
  Reply With Quote
Old 04-21-2012, 02:06 PM   #3  
Praise the Sun!
 
evil_Vasile's Avatar
 
Joined: May 2011
Location: The Internet
 
evil_Vasile is offline
Default Re: Shoutbox - Fiendian and viper45

So what you're saying, pointers are a lot like women?...
  Reply With Quote
Old 04-21-2012, 02:21 PM   #4  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

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 02:31 PM.
  Reply With Quote
Old 04-21-2012, 04:02 PM   #5  
...
 
AntonioR's Avatar
 
Joined: May 2011
 
AntonioR is offline Steam ID: AntonioR Facebook: AntonioR.Software Youtube: antoniorsoftware
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by evil_Vasile View Post
So what you're saying, pointers are a lot like women?...
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.

Last edited by AntonioR; 04-21-2012 at 04:13 PM.
  Reply With Quote
Old 04-22-2012, 04:08 AM   #6  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by Fiendian View Post
Pointer pointing at a pointer pointing at a pointer...
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 | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-22-2012, 04:15 AM   #7  
Praise the Sun!
 
evil_Vasile's Avatar
 
Joined: May 2011
Location: The Internet
 
evil_Vasile is offline
Default Re: Shoutbox - Fiendian and viper45

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?
  Reply With Quote
Old 04-22-2012, 04:26 AM   #8  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

float x = 600;

float *px = &x;

*(int*)px |= 0x80000000;

x = ?
__________________
Homepage | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-22-2012, 04:28 AM   #9  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

@evil_Vasile:
void *p1, *p2, *p3;
p1 = &p2;
p2 = &p3;
p3 = &p1;
__________________
Homepage | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-22-2012, 02:41 PM   #10  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

What's the point?
  Reply With Quote
Old 04-23-2012, 04:51 AM   #11  
Pan
Bropan
 
Pan's Avatar
 
Joined: May 2007
Location: Looking for Snake
 
Pan is offline Gamertag: SeriousPan PSN ID: YueStrife Steam ID: Yuethetmerchant
Default Re: Shoutbox - Fiendian and viper45

Would rather learn this than Matlab right about now...
__________________

Quote:
Originally Posted by Viper45
I was thinking the other day how weird it was that we have a mod, where Snakes from MGS can stab snakes that were animated by... Viper. :P
SSHD & SS3 Multiplayer Models --- Serious Box Solid VS Mod --- Serious Fantasy: Legends
Complete Video Tutorial for SED3/3.5 Player Model Creation --- Serious Marine 3: Battle for Terra
  Reply With Quote
Old 04-23-2012, 11:15 AM   #12  
Praise the Sun!
 
evil_Vasile's Avatar
 
Joined: May 2011
Location: The Internet
 
evil_Vasile is offline
Default Re: Shoutbox - Fiendian and viper45

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...
  Reply With Quote
Old 04-23-2012, 11:50 AM   #13  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

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.
  Reply With Quote
Old 04-23-2012, 03:57 PM   #14  
🐱 Kitty 💘💙💚💛💜
 
Scratch's Avatar
 
Joined: May 2007
Location: Heiloo, Netherlands
 
Scratch is offline PSN ID: NimbleCatt Steam ID: ansjh Twitter: ansjh Youtube: youMrAG LinkedIn: 77325926
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by Fiendian View Post
What's the point?
I could point you in the right direction..
__________________
I'm a cat by the way. I'm also everyone's most hated critic.


CodeCat, my blog! - Angelo's stuff - I also have a showcase, which I barely update.
(I do not respond to PM's about Serious Sam Revolution. Please use our bugreporter for that.)
  Reply With Quote
Old 04-23-2012, 03:59 PM   #15  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

Good point.
  Reply With Quote
Old 04-23-2012, 03:59 PM   #16  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

Stop the pointless chatter already!
__________________
Homepage | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-23-2012, 04:19 PM   #17  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

There is no use in pointing fingers at any one, Shrinker.
  Reply With Quote
Old 04-23-2012, 10:59 PM   #18  
Zilla has a Flower
 
*Serious Godzilla*'s Avatar
 
Joined: Jun 2011
Location: Now in IMAX 3D
 
*Serious Godzilla* is offline PSN ID: Comrade-Devidra Steam ID: seriousgodzilla
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by Pan View Post
Would rather learn this than Matlab right about now...
Matlab?
  Reply With Quote
Old 04-24-2012, 12:07 AM   #19  
Pan
Bropan
 
Pan's Avatar
 
Joined: May 2007
Location: Looking for Snake
 
Pan is offline Gamertag: SeriousPan PSN ID: YueStrife Steam ID: Yuethetmerchant
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by *Serious Godzilla* View Post
Matlab?
Google is your friend. Matlab is a mathematics programming/calculation tool.
http://en.wikipedia.org/wiki/MATLAB
__________________

Quote:
Originally Posted by Viper45
I was thinking the other day how weird it was that we have a mod, where Snakes from MGS can stab snakes that were animated by... Viper. :P
SSHD & SS3 Multiplayer Models --- Serious Box Solid VS Mod --- Serious Fantasy: Legends
Complete Video Tutorial for SED3/3.5 Player Model Creation --- Serious Marine 3: Battle for Terra
  Reply With Quote
Old 04-24-2012, 01:51 PM   #20  
Praise the Sun!
 
evil_Vasile's Avatar
 
Joined: May 2011
Location: The Internet
 
evil_Vasile is offline
Default Re: Shoutbox - Fiendian and viper45

Quote:
Originally Posted by Shrinker View Post
Stop the pointless chatter already!
You're just missing the point.
  Reply With Quote
Old 04-25-2012, 08:24 PM   #21  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

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 08:29 PM.
  Reply With Quote
Old 04-26-2012, 02:55 AM   #22  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

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 | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-26-2012, 04:47 PM   #23  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

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.
  Reply With Quote
Old 04-27-2012, 04:05 AM   #24  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

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 | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-27-2012, 10:40 AM   #25  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

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 11:42 AM.
  Reply With Quote
Old 04-27-2012, 12:23 PM   #26  
λ
 
Shrinker's Avatar
 
Joined: Mar 2001
Location: Germany
 
Shrinker is offline Send a message via ICQ to Shrinker Send a message via AIM to Shrinker Send a message via MSN to Shrinker
Default Re: Shoutbox - Fiendian and viper45

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 | Mirror I | Twitter
 The Beautiful Nature
  Lights | Echoes
   Arrival Mix | Tree of Life

  Reply With Quote
Old 04-27-2012, 12:41 PM   #27  
Hey ladies
 
Fiendian's Avatar
 
Joined: Nov 2002
Location: Iceland
 
Fiendian is offline Steam ID: BjossiAlfreds Send a message via ICQ to Fiendian Send a message via MSN to Fiendian
Default Re: Shoutbox - Fiendian and viper45

Thanks for the tip, I didn't know that.
  Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:47 PM.