The Big Programming Thread - Page 19
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. | ||
catamorphist
United States297 Posts
| ||
Adeny
Norway1233 Posts
| ||
57 Corvette
Canada5941 Posts
http://en.wikipedia.org/wiki/Turing_(programming_language) I've made a few interesting things with this, despite it being a beginner language. Managed to make a sort of "Space invaders" game using an interfacing device i made from scratch. Can't do alot of amazing stuff with it, but meh. | ||
cowsrule
United States80 Posts
On July 31 2010 14:51 catamorphist wrote: Wow, it's really gay that you don't get a compiler warning from that. Chalk another one up in the "C++ fucks you again" book. You should compile with "Treat warnings as errors" and set the highest warning level. This should give you a warning something along the lines of "Performance warning: Converting bool to int". By default you won't get a warning about this because it is a valid expression. | ||
catamorphist
United States297 Posts
On August 04 2010 06:19 cowsrule wrote: You should compile with "Treat warnings as errors" and set the highest warning level. This should give you a warning something along the lines of "Performance warning: Converting bool to int". By default you won't get a warning about this because it is a valid expression. Joke's on the OP then -- gotta -Wall. | ||
NinjA246
United States4 Posts
On August 03 2010 09:35 57 Corvette wrote: Has anyone ever used Turing? http://en.wikipedia.org/wiki/Turing_(programming_language) I've made a few interesting things with this, despite it being a beginner language. Managed to make a sort of "Space invaders" game using an interfacing device i made from scratch. Can't do alot of amazing stuff with it, but meh. You sure you can't do a lot of amazing stuff with it? | ||
![]()
tofucake
Hyrule18968 Posts
How is it a joke on me? | ||
catamorphist
United States297 Posts
Sorry, I meant the guy with the C++ bug -- I shouldn't have said "OP." | ||
tec27
United States3690 Posts
http://pastebin.com/YNgbyK0K | ||
Loser777
1931 Posts
here is the problem code: + Show Spoiler + int main() { BankAccount* bankAccounts[10] = {new BankAccount()}; bool run = true; int inputAccountNumber; string curNum; string curName; string curBal; fstream input("C:\\bankAccounts.txt", ios::in); for(int i = 0; !input.eof(); i++) { getline(input, curNum); getline(input, curName); getline(input, curBal); int num = atoi(curNum.c_str()); double bal = atof(curBal.c_str()); bankAccounts[i] = new BankAccount(num, curName, bal, "GA"); } input.close(); [...] for(int i = 0; i < 10; i++) { delete bankAccounts[i]; } delete [] bankAccounts; Basically, anywhere I execute [delete [] bankAccounts] I get _BLOCK_TYPE_IS_VALID -though I don't think I'm deleting anything twice... and If I remove the delete [] bankAccounts I get stack around the variable bankAccounts was corrupted... I'm sure this is an easy problem to solve, but in Java I never had to deal with this. | ||
Yukidasu
Australia125 Posts
BankAccount* bankAccounts[10] = {new BankAccount()}; This leaks a BankAccount, you don't need it. Your main problem here is that your array is on the stack, meaning that it is created and destroyed with your function like normal variables. That's what the syntax Class* variable[number]; means. Basically, get rid of the = and the stuff after it on that line, and remove the last delete[] statement because you don't need it. Or replace the first line with: + Show Spoiler + BankAccount** bankAccounts = new BankAccount*[10]; | ||
Loser777
1931 Posts
BankAccount* bankAccounts[10]; for(int i = 0; i < 10; i++) { delete bankAccounts[i]; bankAccounts[i] = NULL; } I get "stack around the variable bankAccounts was corrupted"... EDIT: when I moved the array outside of main (as a global variable), the problem was fixed... any reasons why? | ||
Badjas
Netherlands2038 Posts
C++ language specs do not mandate initializing variables, so you can't test on NULL if you don't initialize them to be NULL first, so add an extra loop for that right after your declaration of bankAccounts. | ||
Adeny
Norway1233 Posts
for(int i = 0; !input.eof(); i++) because every iteration it writes to an array of a finite size, so if the file grows, it'll overflow. You probably know though, just pointing it out. What would probably be better is if you manually keep track of how many instances are created, pseudo code inc:
Oh and I guess bankAccounts[] needs to be self-expanding, or validate if it has reached its upper bound before you initialize a new account. | ||
mmp
United States2130 Posts
Web Programming + Show Spoiler + HTML5: http://www.whatwg.org/specs/web-apps/current-work/multipage/index.html HTML5 Canvas Element: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html HTML5 Video Element: http://www.w3.org/TR/html5/video.html DOM: http://www.w3.org/DOM/ (what's going on inside the browser, if you dare to peek) JavaScript: Douglas Crockford's "The Good Parts" Talk: http://www.crockford.com/javascript/ http://www.jslint.com/ http://www.json.org/ http://jquery.org (look into QUnit test suite and JQueryUI also, very handy stuff) | ||
dvide
United Kingdom287 Posts
http://stackoverflow.com/questions/1039667/why-does-stdfstream-set-the-eof-bit-the-way-it-does http://stackoverflow.com/questions/730910/ifstream-object-eof-not-working Secondly you should probably use: std::vector<BankAccount*> bankAccounts; This will save you a lot of trouble. It uses a dynamic array internally, meaning you can insert any number of elements into the vector (using the push_back member function) and it will resize itself automatically at runtime to fit. In your case, if there is not exactly 10 bank accounts stored inside your file then there will be a problem here. For example, if there are 11 bank accounts in the file then your code will insert the last one into the 11th element of the array, which is undefined behaviour. If there are less than 10 bank accounts stored inside your file, during clean-up you will be deleting a bank account that was never created. You will be calling delete on an uninitialised pointer, which again is undefined behaviour. You could initialise all bankAccount elements to NULL before you read the file, in which case your code would be fine so long as there are always <= 10 bank accounts in your file. Calling the delete operator on a NULL pointer is safe. But again a normal C++ programmer wouldn't do it this way. Infact, a normal C++ wouldn't even use std::vector if they were able. They would rather use something like Boost's ptr_vector for this task, which is a class that owns the pointers internally and handles clean-up for you in its destructor (a popular C++ idiom called RAII, if you're interested). It also provides a nicer interface to your elements than an std::vector of pointers. This is probably not something you should focus on though until you are comfortable with what is actually going on. But just know that it's not always this difficult in C++ once you start using the proper tools for the job at hand ![]() EDIT: How a C++ programmer would write this: http://codepad.org/Qs7nNq3R Also, if you don't want to use stringstream I would recommend sscanf instead of atoi. At least that way you can check for formatting errors properly. atoi simply returns 0 on error, so if 0 is a valid value you won't know if it's a true error or not. However, when you gain more knowledge, you should be aware of the security problems with scanf (namely buffer overflows). Hope this helps | ||
tec27
United States3690 Posts
Its pretty messy, but if you're interested in how Direct3D hooking works when you don't control the process starting, its a pretty good reference (I think ![]() | ||
alexpnd
Canada1857 Posts
http://www.teamliquid.net/blogs/viewblog.php?topic_id=143169 It's essentially a lean image browser for threads and random sites (4 chan etc.) | ||
Badjas
Netherlands2038 Posts
![]() ![]() | ||
stafu
Australia1196 Posts
I'm taking an AI unit this semester and we get to do a self-directed project, so naturally my first thought was using BWAPI to make a BroodWar bot. We're only in second week but I'm just trying to scope out how long this would take me and if it's going to be feasible. I've got another game-based/OpenGL programming unit this semester as well which is definitely going to take up a lot of time so I'm a little wary. I'm pretty comfortable with C++ but only just learning the basics of AI theory now. So does anyone know given a fairly limited scope, could I complete a decent AI within 11-12 weeks? | ||
| ||