|
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. |
On April 09 2015 08:04 bardtown wrote: At the moment I do a little bit of C++/Python programming and I think I have potential. I'm pretty good at understanding complicated structures etc, but I am aware that my programming is just functional and I don't really understand what is going on at a very fundamental level. That's pretty disconcerting for me. Even things that should be really basic, like pointers, I'm not comfortable with because I don't know how/why/when they are efficient. Does anybody have a book recommendation that builds up from technical fundamentals? Maybe a computer science textbook is what I need, but I'd prefer something a bit more readable. It doesn't need to be a comprehensive book, obviously I can research things in more detail, but it needs to give me a sense of structure.
Pointers are just what they really are... pointers to memory. So you don't have to copy or work with temporary objects, but reference them instead. That is why they're efficient. If you work with heap memory (one that you have to manually allocate and deallocate, you're not bound to a function/method scope), but pointers can work with stack and heap. Nowadays, C++ uses references instead because in general they're safer (as in they're assumed not to be null unlike pointers).
Good C++ books:
Beginner: C++ Primer Advanced: More Effective C++ (1995), Effective C++ 3rd edition, Effective STL, also the new Effective C++ for C++11/14 (newest standard)
Edit: I'll give you an example of why pointers are efficient. Let's have a function such as this one:
void swap(int a, int b) { int temp = a; a = b; b = temp; }
Two problems:
1. Parameters 'a' and 'b' are copied under the cover. They're not referenced, even if you pass variables. 2. Numbers are swapped, but because of the above (variables are copied), swap is lost once you get out of scope, that is, when you exit execution of the function.
Solution:
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; }
You solve all the two problems above. No extra copies are created. And swap isn't lost after function is executed. But what if you say:
swap(NULL, NULL) ? Undefined behaviour.
So, you can achieve much better syntax with somewhat null safety:
void swap(int& a, int& b) { int temp = a; a = b; b = temp; }
|
Honestly I don't like books and prefer to do projects.
Choose something that sounds cool and figure out how to do it in pure C. Every time you finish and start a new project you'll learn a shitton of new stuff. Like implement linked lists in C using pointers, with push/pop methods. That should challenge your fundamentals hard.
|
I think it is less the language that I have problems with and more the way in which languages in general relate to hardware, etc. Something like C++ Primer goes: here's how you define variables, here's how you create loops, then suddenly it is talking about things like you do above. Stack and heap? What is unsafe about null pointers? What is the difference between references and pointers? I'm definitely missing a lot of knowledge here because I just learnt things as and when I needed them for functional programming. I'm looking at Elements of Programming, but I think that is too advanced for where I am at right now. I may try Introduction to Computation and Programming With Python to start.
Thanks for the example. It does help clarify things for me in terms of usage but I'm still feeling the need for a bottom-up relearning of concepts. It's silly that I've got this far without really thinking at all about values/addresses.
|
On April 09 2015 08:48 bardtown wrote: I think it is less the language that I have problems with and more the way in which languages in general relate to hardware, etc. Something like C++ Primer goes: here's how you define variables, here's how you create loops, then suddenly it is talking about things like you do above. Stack and heap? What is unsafe about null pointers? What is the difference between references and pointers? I'm definitely missing a lot of knowledge here because I just learnt things as and when I needed them for functional programming. I'm looking at Elements of Programming, but I think that is too advanced for where I am at right now. I may try Introduction to Computation and Programming With Python to start.
There are two kinds of memory (actually three if you include static, but forget that for now): heap and stack. Stack is faster, but smaller, yet you should prefer it whenever you can. It's also subject to scope rules. Heap memory is dynamic, slower than stack, but it doesn't care if you exit a method. It remains, but you have to manually delete heap based objects in C++. You don't care about that in Java/C# though.
Further reading: http://stackoverflow.com/questions/5836309/stack-memory-vs-heap-memory
Null pointers are unsafe as they don't point to anything. Example:
int* test = NULL; // or nullptr in C++11
And if you use 'test' before you initialise it, you're in the world of undefined, incorrect behaviour. Anyway, a lot of your questions can be googled. I think Effective C++ answers some of your questions, too. C++ Primer should definitely explain all of what you're asking as far as I remember.
Edit: I think references are just like pointers under the cover, but they're just like 'aliases'. Another name for an existing object, thus you don't have to check for:
if (!object) or
if (object == NULL)
Both checks are the same btw.
On April 09 2015 08:36 Blisse wrote: Honestly I don't like books and prefer to do projects.
Choose something that sounds cool and figure out how to do it in pure C. Every time you finish and start a new project you'll learn a shitton of new stuff. Like implement linked lists in C using pointers, with push/pop methods. That should challenge your fundamentals hard.
You can't start building proper houses before you learn how to do it. You can certainly build one, more likely a bad one though. I think books are a good way to learn stuff without having years of experience. It's all about best practices. One certainly needs practice not just theory, too.
|
Thanks again for explaining. I could find everything online but that is a piecemeal approach which could leave me missing information I didn't even know I needed, hence looking for a structured book. I know of all the books you are recommending, just worry that a) they're very long and dense and b) they often assume basic knowledge that I don't have. May get a few different ones out the library and see if any have the approach I'm after
|
Perhaps see if you can find some sort of rough overview about what the CPU and its assembly language can actually do and how C translates into that. It could be illuminating to see what actually happens when a function in C gets called and when it returns, what the parameters and variables actually are, what happens when there's an "if" or "while".
I tried to find something short but failed, instead found this book here which seems to be good but explains too much, enough to be actually useful if you'd wanted to start learning x86 Assembly: http://www.drpaulcarter.com/pcasm/
|
|
I'd second 'Computer Organization and Design'.
One of the books I've found quite valuable for learning was Silberschatz, Galvin, and Gagne's 'Operating System Concepts'.
I'm on my phone right now, so I can't give a blurb, but I'd say to check them out.
|
|
On April 12 2015 20:13 Nesserev wrote:Hey guys, I'm currently looking into scraping data from websites, and a little bit of front-end development; something that I've never actively done before, so I was wondering... If you go to the following link, or almost any other hotel booking page on expedia.com, hotels.com, etc. a message pops up with 'X amount of persons are watching this page right now'. What would be the best way to extract this information ('X')? http://www.expedia.com/Bruges-Center-Hotels-Hotel-Dukes-Palace.h1853344.Hotel-Information?chkin=12/04/2015&chkout=13/04/2015EDIT: Now that I'm at it; does anyone recommend any books/online tutorials regarding web scraping? First you have to figure out if the data is included in the HTML source or received as an external file. Clicking "View Source" and searching for the term yields, in this case, no results. So the data must be loaded from JS. Using Chrome's (or Opera's in my case) dev tools allows you to capture any files transmitted. By filtering for XHR I found this file:
Remote Address:23.54.3.53:80 Request URL:http://www.expedia.se/Hotels/Offers?[...] Request Method:GET Status Code:200 OK Full link: + Show Spoiler +Request URL:http://www.expedia.se/Hotels/Offers?action=getAndUpdateHotelProductActivity&hotelIds=1853344&durationForViews=1800000&durationForBookings=172800000
Then it's just a matter of extracting the data from JSON:
{ "ViewStatus":0, "BookingStatus":0, "HotelProductActivityList": [ { "ViewCount":8, "BookingCount":22, "HotelId":1853344, "isValidViewCount":true, "isValidBookingCount":true } ] }
This is usually the method I follow.
If it's in the source code, use a html parser or regex to extract the value (regex for small things, html for big). If it's in a json file, find out a way to retrieve the file and then grab the value.
|
|
On April 09 2015 08:31 darkness wrote:Show nested quote +On April 09 2015 08:04 bardtown wrote: At the moment I do a little bit of C++/Python programming and I think I have potential. I'm pretty good at understanding complicated structures etc, but I am aware that my programming is just functional and I don't really understand what is going on at a very fundamental level. That's pretty disconcerting for me. Even things that should be really basic, like pointers, I'm not comfortable with because I don't know how/why/when they are efficient. Does anybody have a book recommendation that builds up from technical fundamentals? Maybe a computer science textbook is what I need, but I'd prefer something a bit more readable. It doesn't need to be a comprehensive book, obviously I can research things in more detail, but it needs to give me a sense of structure. Pointers are just what they really are... pointers to memory. So you don't have to copy or work with temporary objects, but reference them instead. That is why they're efficient. If you work with heap memory (one that you have to manually allocate and deallocate, you're not bound to a function/method scope), but pointers can work with stack and heap. Nowadays, C++ uses references instead because in general they're safer (as in they're assumed not to be null unlike pointers). Good C++ books: Beginner: C++ Primer Advanced: More Effective C++ (1995), Effective C++ 3rd edition, Effective STL, also the new Effective C++ for C++11/14 (newest standard)
For the fundamentals isn't it better to simply read K&R? It's short, easy to understand and should give anyone really good basic knowledge. I know it's C and not C++, but when speaking of the most basic stuff that's pretty much irrelevant.
|
OOP in php is giving me such a fcking headacke
$clients //I make this an array in the constructor
//this will return an array with $somevalue in it public function getClient(){ $this->clients[] = $somevalue; return $this->clients; }
//this returns an empty array, Ill call setClient somewhere first at then Ill call getClient
public function setClient($somevalue){ $this->clients[] = $somevalue; }
public function getClient(){ return $this->clients; }
ps this is just some write up code. but youll get the idea.
|
On April 13 2015 22:02 sabas123 wrote:OOP in php is giving me such a fcking headacke $clients //I make this an array in the constructor
//this will return an array with $somevalue in it public function getClient(){ $this->clients[] = $somevalue; return $this->clients; }
//this returns an empty array, Ill call setClient somewhere first at then Ill call getClient
public function setClient($somevalue){ $this->clients[] = $somevalue; }
public function getClient(){ return $this->clients; }
ps this is just some write up code. but youll get the idea.
class Client { protected $clients; public function __construct() { $this->clients = array(); } public function getClient() { $this->clients[] = $somevalue;
return $this->clients; } }
$clients = new Client;
print_r($clients->getClient());
Result:
Array ( [0] => )
// var_dump instead of print_r
array(1) { [0]=> NULL }
Despite pushing the uninitialized variable to the array everything works as intended.
What exactly was your problem?
Also, if you want full OOP you can always use ArrayObject instead of regular array
|
On April 13 2015 22:02 sabas123 wrote:OOP in php is giving me such a fcking headacke $clients //I make this an array in the constructor
//this will return an array with $somevalue in it public function getClient(){ $this->clients[] = $somevalue; return $this->clients; }
//this returns an empty array, Ill call setClient somewhere first at then Ill call getClient
public function setClient($somevalue){ $this->clients[] = $somevalue; }
public function getClient(){ return $this->clients; }
ps this is just some write up code. but youll get the idea.
What exactly is your problem with it? Other than your first method, which seems both superfluous and wrong: firstly, it is overwritten immediately by the second declaration of getClient() and secondly, $somevalue is not initialized in this bit of code, so presumably it is null, but you definitely don't want to add it to your client array.
Other than that first method, it seems like completely standard getter and setter code, so unless you have a problem in general with getters and setters (which I agree can be quite a tedious design pattern, but makes UPKEEP of the code much easier), what specifically is your problem with OOP in PHP?
|
$somevalue is a value that I will use some where else in the script
the problem is that I apperntly cant set the value or get the setted value back.
|
|
On April 13 2015 23:29 Manit0u wrote: What is the value?
How do you set it?
How do you get it? I set it with
public function setClient($input){ $this->clients[$input]; }
and then I get it with
public function getClient(){ return $this->clients; }
and the value is a string.
|
On April 13 2015 23:32 sabas123 wrote:Show nested quote +On April 13 2015 23:29 Manit0u wrote: What is the value?
How do you set it?
How do you get it? I set it with public function setClient($input){ $this->clients[$input]; }
and then I get it with public function getClient(){ return $this->clients; }
and the value is a string.
$this->clients[$input] does not set anything... it gets the value at the array index $input. Been a while since I programmed php, but pretty sure this throws an error unless you have somehow initialized your array already and there is an actual value at index $input. In that case it will return that value, but your method does not return, so in the end it will simply do nothing.
Use
public function setClient($input){ $this->clients[]=$input; }
To be completely correct, you should probably not call this setClient, but addClient. You should also call your getClient() method getClients(), because it is returning the list of clients as a whole, and not just one specific client. But the naming convention of your methods seems to be a smaller problem than the fact that your basic code is wrong.
EDIT: if what getClient() returns is a string, you are either initializing $this->clients wrong (as a string instead of an array), or you are casting it to a string somewhere along the line.
|
I chose to do a simple tic-tac-toe with an A.I. in Lisp as a project for my programming language class. Since I'm a noob with functional stuff, I just abuse progn make life easy for myself. I just hope my professor doesn't mind that too much >.<
|
|
|
|