|
roomforthetuna posted:I think also a member variable x in a superclass too if you want, though I'm not quite so sure about that one. Yep. It's basically just "not twice in the exact same scope" with a few extra restrictions about variables declared in loop/if conditions (because they're almost the exact same scope).
|
# ? Aug 13, 2014 06:36 |
|
|
# ? Jun 8, 2024 11:14 |
|
Also, you should compile with warnings turned on. Maybe not every compiler has it but at least on some, warnings would have caught this.
|
# ? Aug 13, 2014 07:16 |
|
That's what I thought but the compiler didn't say anything and I'm anal about warnings.
|
# ? Aug 13, 2014 14:56 |
|
PS. Love the cabin posted:I had a dumb moment myself rearranging some code to have a local variable be global and wondering why later on it would all of the sudden be NULL. http://en.wikipedia.org/wiki/Variable_shadowing
|
# ? Aug 13, 2014 15:28 |
|
I have just started playing around with c++ creating a simple window and text box and have a few questions: 1) How do I set the window to redraw itself when resized, so it refills in the background colour as it is resized? 2) How do I get the textbox to be a decent size relative to the window? It keeps going off the bottom of the screen if not constrained vertically. 3)I have a few weird warnings that I would like to find out how to eliminate. Can you help me see where they come from? BTW the password box was just practicing with EDIT styles and the background colour was just practicing changing the colour. This is just me dicking around as I learn. Below are the code and build messages: code:
code:
Thanks in advance BioEnchanted fucked around with this message at 18:23 on Aug 13, 2014 |
# ? Aug 13, 2014 18:20 |
|
eXXon posted:I guess I may as well ask if it's true that there's a potentially meaningful performance difference between ++i and i++ in a for loop, if it's not optimized away at least. I would think any C compiler written in the last 30 years will optimize i++ to ++i when it can (e.g., when the result is ignored). Having said that, I have used at least one compiler that didn't, and to this day I prefer the prefix form. (Alcyon C on the Atari ST. Three passes plus assembler, with all intermediate files stored on (floppy!) disk. But it taught me so much... )
|
# ? Aug 13, 2014 18:24 |
|
giogadi posted:This is actually a super interesting question but might be out of the scope of this thread. For what it's worth - I'm certain that fitting an affine transform to a dataset has been done before, but without doing any research, you might try a hybrid approach? I.e., use least squares to get an initial solution, then toss that initial solution into a local nonlinear optimizer like Levenberg-Marquardt (Ceres has this actually) where you use your constraint as your objective function. Research, it turns out, was actually the key to the solution. I have worked in MIT's incremental smoothing and mapping (ISAM) and it's working quite well in real-time for 6-DOF poses and 3-DOF landmarks.
|
# ? Aug 14, 2014 04:18 |
I have a C++ program which needs to call a series of command line tools that someone else has written, with arguments that could depend on user input. I know in python I would handle this by using the subprocess module with SHELL=False; what is the safe way to do this in C++(11)?
|
|
# ? Aug 14, 2014 20:09 |
|
shrughes posted:Also, you should compile with warnings turned on. Maybe not every compiler has it but at least on some, warnings would have caught this. It looks like gcc doesn't warn about variable shadowing even with "-Wall -Wextra" (you need to explicitly specify "-Wshadow"), and clang needs "-Weverything". But yeah, always run with warnings.
|
# ? Aug 14, 2014 21:03 |
|
VikingofRock posted:I have a C++ program which needs to call a series of command line tools that someone else has written, with arguments that could depend on user input. I know in python I would handle this by using the subprocess module with SHELL=False; what is the safe way to do this in C++(11)? The language doesn't support it out of the box, it's platform dependent. What platform are you on?
|
# ? Aug 14, 2014 21:08 |
Dren posted:The language doesn't support it out of the box, it's platform dependent. What platform are you on? It needs to work for OSX and Linux.
|
|
# ? Aug 14, 2014 21:19 |
VikingofRock posted:It needs to work for OSX and Linux. Then it's probably straight execve() you want. It's a Unix standard system call, for executing a program with arguments given as an argv array. Keep in mind that it replaces your current process with the new one, so you generally always want to fork() before exec'ing. Since the arguments are passed as an array you generally won't run the risk of escaping-problems causing arguments to "leak" in unintended ways, but of course that can depend on the actual program called. You'll obviously also want to ensure that any filename arguments that might be based on user input can only reference files the user is actually allowed to operate on.
|
|
# ? Aug 14, 2014 21:25 |
|
VikingofRock posted:I have a C++ program which needs to call a series of command line tools that someone else has written, with arguments that could depend on user input. I know in python I would handle this by using the subprocess module with SHELL=False; what is the safe way to do this in C++(11)? You can define your own shell escaping function and use system(), or you can use fork() and exec*() as god intended. execl* if you have a fixed number of arguments, or execv* if it can vary. exec*p if you want to look up the executable in PATH, exec*e if you need to set environment variables too.
|
# ? Aug 14, 2014 21:31 |
nielsm posted:Then it's probably straight execve() you want. It's a Unix standard system call, for executing a program with arguments given as an argv array. Edison was a dick posted:You can define your own shell escaping function and use system(), or you can use fork() and exec*() as god intended. Okay cool, fork() -> execv() it is! Thanks so much! Edit: Is there an easy way to get the output of the processes? VikingofRock fucked around with this message at 21:35 on Aug 14, 2014 |
|
# ? Aug 14, 2014 21:32 |
|
VikingofRock posted:Okay cool, fork() -> execv() it is! Thanks so much! Nope, there's the difficult way that every other language abstracts over. code:
Edison was a dick fucked around with this message at 22:23 on Aug 14, 2014 |
# ? Aug 14, 2014 21:46 |
|
Edison was a dick posted:Nope, there's the difficult way that every other language abstracts over. but what if the call to pipe fails
|
# ? Aug 14, 2014 22:24 |
|
Dren posted:but what if the call to pipe fails Nothing at all muahahahahaa! Oh, and apparently the pipeline proposal came with code: http://code.google.com/p/google-concurrency-library/source/browse/include/pipeline.h If you're doing bare C, http://libpipeline.nongnu.org/ may be worth looking at.
|
# ? Aug 14, 2014 22:28 |
|
Ignore all this bad advice and just use popen().
|
# ? Aug 15, 2014 00:18 |
|
pseudorandom name posted:Ignore all this bad advice and just use popen(). Sure, if you want to have to escape your command, spawn a shell, have that unescape the command and then do what we just described.
|
# ? Aug 15, 2014 08:54 |
|
I worked out how to neaten up my window and tried adding child windows (textbox and button) but the child button's event handler isn't working. LBUTTONDOWN is not being called when I click the button, however it works fine when moved to parent's event handler. Any help getting the button to click?code:
|
# ? Aug 15, 2014 19:37 |
Your WndProcButton is not hooked up anywhere, and that isn't how you'd hook it up anyway, usually. If you just want to handle the button being clicked normally by the user, you should handle WM_COMMAND in the containing window's wndproc, and check the wParam high word for which BN_* notification the button is sending (BN_CLICKED probably most interesting), and perhaps the wParam low word for control ID.
|
|
# ? Aug 15, 2014 19:52 |
|
Thanks, that worked perfectly. I'll let you know if I have any more questions that I can't figure out on my own.
|
# ? Aug 15, 2014 20:15 |
|
I am trying something different with a new window, trying to create a text box that I can change the size of. First of all though I get a warning when creating the textbox: C:\Users\Chris\Documents\My Projects\C++\Making a Grid\Basic Window.c|11|warning: variable 'Resize_text' set but not used [-Wunused-but-set-variable]| C:\Users\Chris\Documents\My Projects\C++\Making a Grid\Basic Window.c|14|warning: 'Resize' may be used uninitialized in this function [-Wuninitialized]| Here is the code: code:
|
# ? Aug 17, 2014 09:41 |
|
BioEnchanted posted:C:\Users\Chris\Documents\My Projects\C++\Making a Grid\Basic Window.c|11|warning: variable 'Resize_text' set but not used [-Wunused-but-set-variable]| BioEnchanted posted:C:\Users\Chris\Documents\My Projects\C++\Making a Grid\Basic Window.c|14|warning: 'Resize' may be used uninitialized in this function [-Wuninitialized]|
|
# ? Aug 17, 2014 09:47 |
|
I figured out what the problem was. Moving the HINSTANCE declarations all the way out of the loop made them a global variable and it started seeing them from there. EVENT HANDLING LOOP { DECLARE variable; WM_CREATE { use variable; } } results in "where is variable, it's not used and later there is a used version that isn't defined. I can't figure out this riddle:" DECLARE variable; EVENT HANDLING LOOP { WM_CREATE { use variable; } } results in "ohhhh, there it is!"
|
# ? Aug 17, 2014 21:46 |
|
I'm a bit stumped on a part of a program for an intro to C class I'm taking. I'm supposed to make a program using structs that asks the user to enter basic data (address, phone number, name) on 10 different people/accounts. Afterwards, the program prompts the user to enter a state abbreviation (e.g. CO, OR) and returns a list of all accounts that are associated with that state. I've got the first part down, but I can't figure out how I can compare the user-entered state code with the account-associated states since the == equality operator can't be used. No one in the class can figure out what to do and the professor's being very coy. The part I can't figure out starts with 'char stateTemp[3].' code:
|
# ? Aug 17, 2014 22:39 |
|
string.h has a strcmp function I think. Pretty sure that's what you want. You should really be using std::strings instead of chars, if that's allowed.
|
# ? Aug 17, 2014 22:56 |
|
TheGame posted:
memcmp and strcmp are the proper way to do this. I suspect your professor may be suggesting you use a type punning trick though, since the value of the state code that actually matters is two characters, you can type-pun the code to a short int and compare those. Your professor is a bad person if they are suggesting this, since it's much more clear to use memcmp and strcmp, since it shows the intent, they are portable, and if it's sane to implement it that way on your platform, will do the type punning for you. A good reason not to do this yourself, is that it can cause portability issues. You could end up on a platform that doesn't allow unaligned accesses, so the type-punned comparison could end up comparing the wrong data. You're not going to be bitten by it being ill-defined behaviour here, but it's a bad idea to do that in production software, and anyone who has to port it will hate you. Praseodymi posted:string.h has a strcmp function I think. Pretty sure that's what you want. Given he's doing an intro to C, rather than C++, no, he shouldn't.
|
# ? Aug 17, 2014 23:03 |
|
Edison was a dick posted:Given he's doing an intro to C, rather than C++, no, he shouldn't. Duh, that's what I get for trying to be clever.
|
# ? Aug 17, 2014 23:06 |
|
Thanks, strcmp works perfectly! The teacher's usually quite good, but she kind of prefers to let students figure stuff out on their own. As such she links to a ton of YouTube tutorials, I'm guessing I missed strcmp in one of those or something.
|
# ? Aug 17, 2014 23:22 |
|
she might've wanted you to reason that strings are an array of characters so the only way to compare them is to compare the strings byte by byte, then write strcmp on your own btw, strncmp is the one you want
|
# ? Aug 17, 2014 23:59 |
|
Dren posted:btw, strncmp is the one you want Why?
|
# ? Aug 18, 2014 00:21 |
|
Subjunctive posted:Why? hmm yeah i guess strcmp is fine that scanf w/o a width specifier is no good though C code:
code:
|
# ? Aug 18, 2014 01:25 |
|
He'll learn that later. Start with the basics first.
|
# ? Aug 18, 2014 01:26 |
|
humm I forgot that -fstack-protector only triggers if you bust out of the function, it doesn't protect buffers i.e. the code i posted w/ the overrun buffer runs just fine with -fstack-protector but this code C code:
code:
|
# ? Aug 18, 2014 01:39 |
|
Doesn't it bother you at all that you're declaring character arrays (strings) that clearly have a finite size (two characters, plus the terminator), yet the program lets you enter strings much larger than that? Thought much about what happens when you do enter a string longer than two characters? Turns it "bad stuff happens". It's fine for toy programs that can blow up without any risks. But it's also one of the most common class of software vulnerabilities, responsible for a massive number of security issues and lost productivity. Yeah, it's an early exercise in a beginning C course. But doing it in a kind-of right way isn't much more difficult. At least, the fundamental problem deserves some early mention so folks don't get too used to it.
|
# ? Aug 18, 2014 04:56 |
|
I suppose I should've specified that the class in question is for non-CS majors and is a quarter long, so I've been learning C for a little under 2 months and probably won't continue past here. As such most assignments say something to the effect of "You can assume that the user always enters proper data - no input validation is required." The text has discussed overflow, but terms like 'stack canary' and 'core dumped' and ideas like 'reasoning that strcmp is probably a thing because arrays need to be compared byte by byte' are kind of beyond the scope of the class when structures were learned literally 3 days ago. It's not that I don't appreciate the help (I do), or that I don't understand the importance of clean or proper coding, I'm just trying to explain why the assignment text is the way it is.
|
# ? Aug 18, 2014 05:05 |
|
ExcessBLarg! posted:Doesn't it bother you at all that you're declaring character arrays (strings) that clearly have a finite size (two characters, plus the terminator), yet the program lets you enter strings much larger than that? strcmp is not overwriting any memory. shrughes fucked around with this message at 05:43 on Aug 18, 2014 |
# ? Aug 18, 2014 05:32 |
|
TheGame posted:The text has discussed overflow, but terms like 'stack canary' and 'core dumped' and ideas like 'reasoning that strcmp is probably a thing because arrays need to be compared byte by byte' are kind of beyond the scope of the class when structures were learned literally 3 days ago. It's not that I don't appreciate the help (I do), or that I don't understand the importance of clean or proper coding, I'm just trying to explain why the assignment text is the way it is. Just ignore Dren. It's obvious given the assignment that you weren't expected to care about these things yet.
|
# ? Aug 18, 2014 05:39 |
|
|
# ? Jun 8, 2024 11:14 |
|
I'm trying to compare cursor position to rect position so that the screen sees when the mouse overlaps with a child window, but the rect coordinates are so large comparison is worthless. The mouse pos is fine, coming up as (245,302) but no matter what I do the rect.left and rect.right come up as numbers like 45677754, so figuring out whether the mouse in inbetween them is impossible. Is there a function that I'm missing or a way to reconcile this? Also, how can I set childWin to be either of the text boxes? code:
BioEnchanted fucked around with this message at 09:57 on Aug 18, 2014 |
# ? Aug 18, 2014 08:45 |