The Big Programming Thread - Page 454
| Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
|
nunez
Norway4003 Posts
| ||
|
RoyGBiv_13
United States1275 Posts
Look at the below four definitions for variables:
The first line, declaring i, reserves a 32-bit space of memory. If it is declared within a function scope, that memory is ontop of the stack which is a memory structure reserved for each thread of execution. As that function enters scope, memory will be reserved for that function's variables, including i, and as that function exits scope, its variables and their associated memory will be removed from the stack. If you have code such as the following:
Both "num" and "x" will get space on the stack. If this function were to be called several times, each copy of those variables would have a place on the stack. In this case, "num" being passed to the function directly onto the stack is called "passing by value". This means that do_fib could change the value of "num" without affecting the value anywhere else in memory, since it has it's own copy. What about if "i" is declared in global scope, where there is no stack? In that case, the linker will reserve a bit of memory somewhere in the addresses of the ".data" section, or ".rodata" section if its declared as a constant. This is one of the ways the same variable can be accessed from different threads of execution, that don't share stack memory with each other. The second line, uint32_t * j;, will do almost the same thing as the first line. The memory reserved, though, will not contain an integer data type, but rather, an address of somewhere in memory that contains a 32-bit integer. The actual size of the memory reserved for this pointer depends on your architecture, but for most cases, it will be 32-bits as well (unless you're compiling for a 64-bit target, or 16-bit target, etc...). A pointer holds a location in memory of a variable called an "address". An address of a variable will look just like a big integer, but it tells the computer where in the memory to look for the variable. Lets look at some code that gets an address of a variable.
Assuming this code was within a function call, it would print out a location somewhere on the stack. If i and j were declared in a global scope, you would see the pointer pointing to somewhere in the .data section. An address could look something like 0x40003572, though that won't mean much until you look at the memory layout of your system to find out where the address is. Classes work the same way that a primitive data type such as integers work. Declaring it within a function reserves memory on the stack, declaring a pointer will only reserve the pointer size, instead of the entire Class's size. Assuming class_type is huge, 1 megabyte or so, putting it on the stack could quickly overflow the stack or crash the program. Especially if you call the same function many times. Instead, its useful to pass classes to functions "by reference":
This will create a copy of "foo" on the stack, as well as a pointer to the memory where my_class is stored. Only 64 bits instead of 1 megabyte was needed! When the function multiplies foo times three and stores it in the same location, it will not affect the "foo" that the calling function passed. The value of the memory where my_class->data is stored will be modified, however, so the calling function would be able to see that. This way, functions in C can effectively modify as many variables as it wants, and functions higher up the stack can see the changes. This has created the commonly used standard in C of using function return values as error codes, rather to give information back to the caller, and instead passing variables intending to be modified by reference. Now lets take a look at the syntax to use pointers: + Show Spoiler + int * x; //putting an asterisk in a variable declaration will make it a pointer to the type just described before the asterisk. x is type: pointer to an int; x = &a //putting an ampersand before a variable will return a reference to that variable. The value at x is the address of a int foo = *x //putting an asterisk before a variable will "dereference" that address held by the variable, returning the value being pointed at. Update the value of foo with the value being pointed at by x (in this case, a). myclass_ptr->data=foo; //The -> pointing arrow means the same thing as (* myclass).data. It means to use the value being pointed to by myclass_ptr, not the value of myclass_ptr itself for the dot operation. Finally, looking back at the problem of where to put my_class if its a huge class, there is another space in memory, outside of .data and the stack, called the "heap". This is where you put variables in memory that you want to stick around, but not forever like a global variable, and that may be large. Calling "malloc", or "new" will assign a space of memory in the heap, and return that address or value. Feel free to ask any questions, however specific. I work at a C/C++ compiler company. Now I just need someone to ask a question regarding threads so I can pick up on how operating systems handle memory between multiple running programs. So you come form a high level language and were curious what this means to you: + Show Spoiler + It depends language to language, but commonly, as in Java, primitive types are passed by value, and Object types are passed by reference. The compiler/interpreter will do all the pointer operations for you when it does a pass by reference, so it looks the same as in pass by value. Arrays are treated as objects, and will be passed by reference. Java also does not manage several separate data areas to store the variables, and has a more advanced garbage collection system to handle cleaning up variables than pushing and popping them off the stack. Exceptions are treated as a separate thread of execution, even in C++, and will often have their own stack. | ||
|
nunez
Norway4003 Posts
i think c++ newbies should take care not to conflate pointer with reference; this was confusing for a while to me, consider: f(int* v); and int* a; f(a) will pass a copy of the pointer a. so v's pointee in a will have the same address as a's pointee, but v itself will have a different address than a. g(a) will pass the reference to pointer, here both v and v's pointee have the same address as a and a's pointee respectively. thus changes to v itself in f(a) will not be visible in a, since v in f(a) is merely a copy. however changes to v itself g(a) is equivalent to changing a itself. in both cases changes to a's pointee will be visible ofc. i think 'alias' makes sense for reference, but not really for pointer (to me). that's sounds like an awesome job btw roygbv. | ||
|
phar
United States1080 Posts
https://www.usenix.org/system/files/1403_02-08_mickens.pdf If you are a frontend programmer, you may need to read it in smaller chunks to avoid dying from a twisted combination of laughter and pain. | ||
|
Manit0u
Poland17494 Posts
On March 11 2014 07:15 RoyGBiv_13 wrote: Feel free to ask any questions, however specific. I work at a C/C++ compiler company. Now I just need someone to ask a question regarding threads so I can pick up on how operating systems handle memory between multiple running programs. I've got a question for you. Albeit it might be a stupid one since I'm just an amateur dabbler in programming for the most part and never learned the "behind the scenes" stuff as I never really needed that much knowledge. Well, now I'm interested in going deeper with my knowledge and want to apply better and more efficient practices when I write the code. My question (please don't laugh if it's stupid):
Is there any real difference if I pass the variable to the function, then assign the variable's value to another variable or if I work with the passed variable directly bar the readability?
Note: I used simple int in the example, but what if that's actually an entire mapping or somesuch bigger data structure? | ||
|
necrosexy
451 Posts
| ||
|
Cyx.
Canada806 Posts
On March 11 2014 11:06 Manit0u wrote: I've got a question for you. Albeit it might be a stupid one since I'm just an amateur dabbler in programming for the most part and never learned the "behind the scenes" stuff as I never really needed that much knowledge. Well, now I'm interested in going deeper with my knowledge and want to apply better and more efficient practices when I write the code. My question (please don't laugh if it's stupid):
Is there any real difference if I pass the variable to the function, then assign the variable's value to another variable or if I work with the passed variable directly bar the readability?
Note: I used simple int in the example, but what if that's actually an entire mapping or somesuch bigger data structure? The only difference is that you make an extra copy in the first one ^^ The compiler makes a copy anyways when you pass by value like that, so unless the parameters are references, all you do is waste time (that the compiler probably optimizes away anyways). EDIT: On March 11 2014 11:32 necrosexy wrote: how do i specify where the java compiler places output? javac -d [pathname] [java file name] | ||
|
SilverSkyLark
Philippines8437 Posts
I have a tableView with custom cells containing the item name, price, and discount (if available). There is also a textField for the quantity and the user requested discount. The user selects the rows of the items he wants and fills up those two textFields. The user then clicks a button and what I want to do is to get the item name, price, discount, quantity, and user requested discount and store those in arrays. Before I go on, here are relevant code: The Mutable Arrays I used: + Show Spoiler +
Since I'm using my custom tableViewCell class, I got this: + Show Spoiler +
Here are my cellForRowAtIndexPath and didSelectRowAtIndexPath methods + Show Spoiler +
And here is the part of the function that is killing me. I have no problem getting the item name, price, and default discount since those were taken from an array I created. My problem is that I could not access the tableRowCell properly and therefore, I can't access the textFields as well. Here is my function so far. What I basically do is get the indexPaths of those rows that have been checked. Using that array of indices, I get the itemName, price, and discount from the arrays I initialised. + Show Spoiler +
And after that, I am pretty clueless on how to proceed. I know I can use the `cellForRowAtIndexPath` method, however, that returns a UITableViewCell object, and that's not he object I used since I had my own custom cell. Any ideas anyone? | ||
|
Blisse
Canada3710 Posts
SearchResultTableViewCell* cell = (SearchResultTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; | ||
|
SilverSkyLark
Philippines8437 Posts
On March 11 2014 15:42 Blisse wrote: You can cast your UITableViewCell to your SearchResultTableViewCell like so SearchResultTableViewCell* cell = (SearchResultTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; Wow. I can't believe I missed that one. Thank you. EDIT Sorry but I'm missing something simple again, how do I convert int to NSIndexPath? My app crashes at SearchResultTableViewCell* cell = (SearchResultTableViewCell*)[searchResults cellForRowAtIndexPath:path]; because I force the int to indexPath conversion here: NSIndexPath *path = [NSIndexPath indexPathWithIndex:i]; I'm looking around on how to convert int to NSUInteger and then NSIndexPath but I've yet to find anything. Fixed with: NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; | ||
|
Blisse
Canada3710 Posts
| ||
|
Manit0u
Poland17494 Posts
On March 11 2014 11:38 Cyx. wrote: The only difference is that you make an extra copy in the first one ^^ The compiler makes a copy anyways when you pass by value like that, so unless the parameters are references, all you do is waste time (that the compiler probably optimizes away anyways). Will it make any difference on a system that compiles stuff 'on the fly'? IE: you create your .c files and they get loaded as needed, which can be done at any time during the system/program uptime, you can add things to or change a working environment anytime you want. | ||
|
Cyx.
Canada806 Posts
On March 11 2014 22:52 Manit0u wrote: Will it make any difference on a system that compiles stuff 'on the fly'? IE: you create your .c files and they get loaded as needed, which can be done at any time during the system/program uptime, you can add things to or change a working environment anytime you want. I'll admit I'm not 100% sure what you mean - are you talking about just-in-time compiling C/C++? As far as I know that's not possible except with C++/CLI (in which case you're barely writing C++ anyways) and to some extent with LLVM (although by all accounts it isn't easy). If you're wondering about other jitted languages, then I'm gonna say the only difference between a fully compiled language and a JIT compiled language in the code sample given was that a JIT compiler is LESS likely to optimize out the useless extra copy (since they're less willing to do optimizations overall) - but other than that, I'm pretty sure they'll try to do the same thing in principle. Don't quote me though. | ||
|
RoyGBiv_13
United States1275 Posts
On March 11 2014 23:42 Cyx. wrote: I'll admit I'm not 100% sure what you mean - are you talking about just-in-time compiling C/C++? As far as I know that's not possible except with C++/CLI (in which case you're barely writing C++ anyways) and to some extent with LLVM (although by all accounts it isn't easy). If you're wondering about other jitted languages, then I'm gonna say the only difference between a fully compiled language and a JIT compiled language in the code sample given was that a JIT compiler is LESS likely to optimize out the useless extra copy (since they're less willing to do optimizations overall) - but other than that, I'm pretty sure they'll try to do the same thing in principle. Don't quote me though. A function such as this would likely be inlined by the compiler, meaning that the compiler would try to remove the function altogether, the stack generation and branch to the function. In its place, anywhere that the function is called would just be replaced with the code generated for (do stuff to num). Of course, this isn't the entire answer. Lets break down the original function and see what a compiler might do:
First off, the compiler will notice that there is no conditions or loops in this, so that the entire function fits within one "basic block". If any code is executed in a basic block, the entire block is guaranteed to be executed. Secondly, the compiler can recognize temporary variables, such as x. Depending on your usage of x, it may try to work backwards from all the points x is used, and tries to fill in num. If that works without any problems, then it will optimize away x entirely. Going through the first pass optimization pass of the compiler, it may look like your second code block:
The compiler might then look at this basic function, and decide that it doesn't need to branch all the way to some other place in memory and setup a new stack in order to find a return value. Instead of generating assembly equating to "branch to some_func, then pop the first value in the stack off into a register as the return value", it will instead replace it with "do some stuff with register where num is, then return that as the value to be used". This, as I mentioned earlier, is called "inlining", and can be explicitly suggested to the compiler using a keyword, usually. In GCC, it's __inline__:
This isn't mandatory, as the compiler will likely inline functions like this anyway. The question about how will functions like this be handled in Just-In-Time compilation depends on the system in which you're running your code. Native C cannot be run JIT, as it is a strictly compiled language, but if its running in a virtualized system, the system could read the C code and execute instructions based on it. The level of optimizations that the virtual system goes through in running the C code depends on the virtual system. Note that it is quite possible to compiler C/C++ code into its object code and then link that against a dynamic setting, such as .dll's for Windows or .so for linux, which can be ran dynamically, but thats a whole different thing than JIT compiling. | ||
|
Manit0u
Poland17494 Posts
I'm not entirely sure about the underlying technology since I'm too newb at it still. Here you can view the gamedriver itself: https://github.com/cotillion/cd-gamedriver Here's some info on how the stack works in it. It appears it's using some form of virtual stacks: https://github.com/cotillion/cd-gamedriver/blob/master/doc/STACKMACHINE | ||
|
Cyx.
Canada806 Posts
On March 12 2014 03:14 Manit0u wrote: I guess I should explain the "on the fly" compilation I mentioned. It's a mudlib gamedriver. It runs perpetually for two weeks non-stop with players running around etc. Every object there is loaded and compiled when required (ex: when a player enters the location object or interacts with other objects - everything is an object there). The coders have dual-acces, both as GM's in the game, where they can view all the code and even edit it from inside the game (it's a bit tricky though as the only editor available there is ed) and regular ftp server acces. The GMs can load/destruct/update all objects from within the game. You can change everything on the fly (when a bug occurs for example and needs to be fixed quickly) without having to pre-compile anything and it will be fully functional. You can add new stuff or modify existing stuff in the game even while it's running simply by adding/editing .c files. I'm not entirely sure about the underlying technology since I'm too newb at it still. Here you can view the gamedriver itself: https://github.com/cotillion/cd-gamedriver Here's some info on how the stack works in it. It appears it's using some form of virtual stacks: https://github.com/cotillion/cd-gamedriver/blob/master/doc/STACKMACHINE Sounds basically like just-in-time compilation, which is the same thing that you can use in some scripting languages (Lua, Python etc...) when you don't want the overhead of running your code through an interpreter all the time ^^ e: so I guess I should tell you guys how my 'command line interview' went... it was not as complex as previously thought, I just had to write a couple of scripts and fix some bugs in some stuff. It was on the command line on Ubuntu so all the stuff I had been worrying about for the last couple days was still pretty useful, but I didn't have to know how to grep anything or anything dumb like that ^^ | ||
|
Shield
Bulgaria4824 Posts
| ||
|
Manit0u
Poland17494 Posts
On March 12 2014 08:47 Cyx. wrote: Sounds basically like just-in-time compilation, which is the same thing that you can use in some scripting languages (Lua, Python etc...) when you don't want the overhead of running your code through an interpreter all the time ^^ e: so I guess I should tell you guys how my 'command line interview' went... it was not as complex as previously thought, I just had to write a couple of scripts and fix some bugs in some stuff. It was on the command line on Ubuntu so all the stuff I had been worrying about for the last couple days was still pretty useful, but I didn't have to know how to grep anything or anything dumb like that ^^ But grep is awesome... I have no idea how people can do anything other than gaming on Windows. I feel lost without console and I barely ever use a mouse aside from gaming. And the fact that you can combine commands in Linux terminal (especially with grep) is also amazing. You can for example get through entire file and write lines that contain certain pattern into another file all with one command. Hell, you can even get entire file sections thrown out onto your screen/written to another file or get a listing of all files containing the pattern etc. On March 12 2014 08:47 darkness wrote: I am looking to learn some good Java coding practices. Has anyone here published their source code publicly? If yes, can I have a link please to have a look. Objective-C/OOP C++ may also help although I am not that familiar with C++'s syntax. I know a bit of C though. http://cc2e.com/Default.aspx Get this book. | ||
|
klo8
Austria1960 Posts
On March 12 2014 08:47 darkness wrote: I am looking to learn some good Java coding practices. Has anyone here published their source code publicly? If yes, can I have a link please to have a look. Objective-C/OOP C++ may also help although I am not that familiar with C++'s syntax. I know a bit of C though. If you're willing to read a book, this is the one to get: http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683 It does not address the changes made in Java 7 or 8 but the fundamentals of the language have not changed since the book was published. I'm reluctant to post code I wrote because I'm not a great Java programmer, but if you really want to, you can take a look at this emulator for a toy processor architecture (Micro16) I wrote last week. It does not showcase advanced Java features very much, no concurrency or parallelism or anything of the sort. https://github.com/GyrosOfWar/Micro16 edit: Guy above me recommended Clean Code, which I haven't read yet, but from what I've heard it's a really good book. Effective Java is obviously a lot more specific to Java and OOP, but I think it teaches good coding practices in general as well. | ||
|
berated-
United States1134 Posts
On March 12 2014 09:49 klo8 wrote: If you're willing to read a book, this is the one to get: http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683 It does not address the changes made in Java 7 or 8 but the fundamentals of the language have not changed since the book was published. I'm reluctant to post code I wrote because I'm not a great Java programmer, but if you really want to, you can take a look at this emulator for a toy processor architecture (Micro16) I wrote last week. It does not showcase advanced Java features very much, no concurrency or parallelism or anything of the sort. https://github.com/GyrosOfWar/Micro16 edit: Guy above me recommended Clean Code, which I haven't read yet, but from what I've heard it's a really good book. Effective Java is obviously a lot more specific to Java and OOP, but I think it teaches good coding practices in general as well. +1 for clean code. its also java oriented but can be applied to other languages. I've been a software dev as a professional for 6 years almost and just read the book this weekend. I feel fortunate I was able to grow and experience a lot of things organically but I wish I would have read the book when I started. I read it on kindle but went and bought 2 hard copies to take to my other colleagues before I was even halfway through the book. thanks to everyone in this thread who recommended it. | ||
| ||