|
Smackbilly posted:pthreads question: You could just keep a few of them around and reuse them, instead of creating new ones over and over again. But don't. Do the thing that makes the code messier. It is a local mess (and only if you code it dumbly), not a global mess, so it is fine.
|
# ¿ May 24, 2008 01:01 |
|
|
# ¿ Apr 27, 2024 16:15 |
|
schnarf posted:
Here's an algorithm that scales better with respect to integer size. I don't think it's a good idea to use a macro. code:
|
# ¿ Jul 4, 2008 06:33 |
|
It might be more kosher to use ULL for unsigned long longs, but they were less than 0x8000000000000000 anyway. The 'even' bits are those in even-numbered positions, starting at zero. Take the number 74 for example, and write it in binary: 01001010. The even-numbered bits are the ones that represent "2 to the Nth power", for even numbers N. code:
|
# ¿ Jul 4, 2008 19:59 |
|
bcrules82 posted:sarehu: do you have a reference/document for this? No.. I just made it up on the spot.
|
# ¿ Jul 7, 2008 14:35 |
|
Smackbilly posted:If RAND_MAX is a power of two minus one (which it usually is), you can generate two random integers, and consider them to be two halves of an integer with twice the number of bits set in RAND_MAX. More generally, generate two numbers x and y, and return (x * (RAND_MAX + 1)) + y. It is like writing a two digit number in the base RAND_MAX+1.
|
# ¿ Jul 8, 2008 01:03 |
|
Insurrectum posted:What's a way to determine the runtime of a program for something like a Project Euler problem? The way you always do. How is it any different?
|
# ¿ Jul 13, 2008 03:56 |
|
Insurrectum posted:The way you always do? Explain to me how I have always determined how long it took a program to run. Oh, I thought you were asking some sort of question specific to Project Euler. code:
|
# ¿ Jul 13, 2008 05:10 |
|
Entheogen posted:hahah, this is a joke, right? That's not true. You can write a program that takes the state of any given computer and tells whether a program on it will halt.
|
# ¿ Jul 13, 2008 21:25 |
|
SuddenExpire posted:
HashArray->size()
|
# ¿ Jul 16, 2008 04:48 |
|
Entheogen posted:I used pointers before to switch between two arrays that contain same types. Yeah, use a function. code:
|
# ¿ Jul 20, 2008 22:07 |
|
Ultimately somebody can always throw you one of these:code:
Smackbilly posted:I haven't done a full complexity analysis on this, but I'm fairly sure this ends up being pseudo-polynomial (polynomial in the size of the largest integer value that you can generate in a subterm). That's right.
|
# ¿ Jul 20, 2008 22:43 |
|
Entheogen posted:I am probably wrong, but I think string allocates itself on stack if you allocate it on stack and just wraps char * inside of it. It deletes itself when it is popped from stack or when you delete it yourself assuming you put it into dynamic memory. Lololololololololololololonononononononononononono! Just imagine that a string is the same thing (internally) as a std::vector<char> -- that's a close enough approximation. sarehu fucked around with this message at 04:28 on Jul 22, 2008 |
# ¿ Jul 22, 2008 04:26 |
|
durtan posted:So let's say I want to read 4 individual bytes 1 at a time and see if each hex value corresponds to a particular byte I'm looking for, is it possible to check for all 4 bytes at once? Is it written as What does "corresponds" mean? Will you have a lookup table or function telling you what values correspond to what? Can you invert this mapping and just check for equality? Also you're saying you want to check for all 4 bytes at once. But you just said you were checking for a particular byte that each hex value corresponds to. Did you mean to say that you wanted to check all 4 bytes at once? If so, whether you can do that depends on what it means for hex values to correspond to one another.
|
# ¿ Sep 5, 2014 08:12 |
|
hooah posted:It seems the man page and g++ are telling me different things. If I try to do .c_str() returns a const char *, and you can't convert that to a char * without being bad. If you want a char *, you need to copy that string into a different array that you can make a char * for.
|
# ¿ Sep 16, 2014 14:00 |
|
hooah posted:Oh, I see. I guess I was just missing a step (or perhaps hoping it was unnecessary). In the class examples he's just been creating the argument array directly, so it was simpler. We're supposed to be learning OS stuff, dammit, not the quirks of C/C++ differences! What C/C++ differences? I don't see anything different here.
|
# ¿ Sep 16, 2014 17:39 |
|
1. I don't know what "'proper' composition" means. 2. Run your code under valgrind (and compile with warnings turned on, if you aren't already). 3. It is broken. s is uninitialized in main and then do_stuff tries to dereference it. There is nothing special about structs. They're just a way to define a type that has fields and they work like other C values such as int, double, char *, and more. You would need to malloc a struct in all the same places you would need to use malloc to create a pointer to another type. If you wrote struct stuff s; do_stuff(&s); then you would not have the bug you're having because &s would be a pointer to an actual object, instead of an uninitialized pointer. If you wrote struct stuff *s = malloc(sizeof(s)); you would also be in better shape. Of course you should have code that handles the case where malloc returns NULL. (You should crash in that case, because handling out of memory errors (or more likely, invalid size errors) is insane.) 4. If you did either of those things your code would still leak memory, which would be bad in a program that doesn't immediately exit. sarehu fucked around with this message at 09:37 on Sep 18, 2014 |
# ¿ Sep 18, 2014 09:34 |
|
Presto posted:Not much because you really want sizeof(*s). Oh. Okay then...
|
# ¿ Sep 19, 2014 09:02 |
|
Schmerm posted:The question being: why does a ""-based literal resolve to a const pointer to a pre-allocated bit of static memory, while the {}-based constant doesn't? Consider the notation code:
code:
Note that the nested array initializer { {1, 2, 3}, {4, 5, 6} } can never take on the meaning of an array of const pointers to static int arrays. I believe that { "foo", "bar" } as an array initializer can only be used to initialize a const char *arr[], not some sort of char arr[][], but I could be wrong.
|
# ¿ Oct 29, 2014 03:09 |
|
Suspicious Dish posted:Memory usage is surprisingly hard to understand. Even when using C and manually doing free();, there's no guarantee that will actually deallocate the underlying pages, because your malloc implementation might want to reuse them, or there might be memory fragmentation in the way. Also because it's a syscall.
|
# ¿ Oct 31, 2014 01:57 |
|
Pixelated posted:What am I missing? Try running a simple program that sets and unsets BIT5 on P1OUT and see what happens.
|
# ¿ Nov 6, 2014 11:51 |
|
Zerilan posted:Trying to relearn some C (my previous background in it was real basic to begin with) because my professor for my math undergrad capstone wants to use the C99 standard for part of it. What is a mathematic function? So Ax, Ay, and Az are functions from R^3 to R (or C^3 to C?). Why do you need to parse them as mathematical expressions in order to evaluate them? Wouldn't they just be C functions, that take a value in R^3 (or C^3) as input and return a value in R (or C)? What is x, y, and z? You didn't mention them before you suddenly said they're arrays of values. What kind of value? Are you saying that the functions Ax, Ay, and Az would have different inputs (they being x, y, and z), because if so I think that's wrong, because they're computing components of the function "Vector-A" which means they'd all take the same input value (a point in R^3 (or C^3)). sarehu fucked around with this message at 10:43 on Nov 9, 2014 |
# ¿ Nov 9, 2014 10:41 |
|
Point of order: You keep saying "vector" in places where you mean "vector field". (e: And I assume by "each R->R component" you mean "each R->R^3 component".) Presumably you would have your function prototype be return_type do_curl_stuff(vec3 (*vectorA)(vec3), ...other parameters...) where vec3 is your 3 dimensional vector type, defined by something such as typedef struct { double x, y, z; } vec3; or typedef struct { double coords[3]; } vec3;. sarehu fucked around with this message at 11:29 on Nov 9, 2014 |
# ¿ Nov 9, 2014 11:26 |
|
Zerilan posted:By each component I think I meant R^3->R Erm.. yeah.
|
# ¿ Nov 10, 2014 19:03 |
|
Risket posted:I'm getting an "expected a declaration" error on this first curly brace. Try adding an equals sign. If that doesn't work, I don't know.
|
# ¿ Nov 25, 2014 19:48 |
|
GrumpyDoctor posted:I'm working in Visual Studio, and I've got a C++ program that yields wildly different results in Debug and Release builds (the Debug results are obviously wrong). I know this screams "undefined behavior", but in my experience, this almost always causes a suspicious warning somewhere or a problem that a run-time Debug build will catch one way or another (like an uninitialized variable). (I'm using VS2013.) Is there some angle to this problem I'm not thinking about? Use git bisect to track down the problem? It could also be due to timing, not undefined behavior, if multiple threads or the OS are somehow involved. Also it could be just caused by debug-only code, not undefined behavior that is compiled differently.
|
# ¿ Dec 2, 2014 20:27 |
|
"the" thread? I assume you mean the only good YOSPOS thread but I think the post in question is http://forums.somethingawful.com/showthread.php?threadid=3481275&pagenumber=704&perpage=40#post421547924
|
# ¿ Dec 3, 2014 07:08 |
|
Vanadium posted:How do you overload the default constructor to detect whether your were instantiated with new T() vs new T? You define one -- and then it always gets called, either way. std::array has an implicitly defined constructor. Edit: You know what? I don't even know why new std::array<int,5>() would produce an array of zeros anymore. C++ initialization is too loving complicated. See http://en.cppreference.com/w/cpp/language/value_initialization, where you've got four different versions of initialization for pre-C++03, pre-C++11, pre-C++14, and post-C++14. Specifically you're doing value initialization, in case 2, which means one thing pre-C++11, another post-C++11, and another post-C++14 but no matter what it gets zeroed out. sarehu fucked around with this message at 19:43 on Dec 3, 2014 |
# ¿ Dec 3, 2014 19:31 |
|
So it assumes the integers are between 1 and n, inclusive? (Or a slightly wider range than that, the specifics depending on what n % 10 is and the behavior of dividing negative integers by 10.) You're passing the pointer 'a' by reference to bucketsort for no reason. Why don't you pass it by value? You're allocating a crapload of arrays on the heap, which will call allocation code that might be slow, a whole bunch of times. I would write code:
Two times five is a weird number. Choose 16, or 15, because that's more hackery. Or you could at least have add() double the size of the array any time it's completely used. (Or you could just use a std::vector<int> in place of Bucket, or write struct Bucket { std::vector<int> a; Bucket()... } which would be practically the same thing.) Then you wouldn't have a limitation on bucket size. Also if you're going to assume the values are from 1 to n, you might as well make an array of length n and just count how many times you see the value i in array[i-1]. Edit: Also, you should not declare variables i and j up top and then reuse them in different for loops. You should redeclare them separately for each use. As for why this is a good idea, that's left as an exercise to the reader. sarehu fucked around with this message at 02:48 on Dec 9, 2014 |
# ¿ Dec 9, 2014 02:45 |
|
You could store the values (j-1) % 10 in Bucket::a instead of storing j. Then you could use a char instead of int there and make the array use less memory. The math might be faster if you use 16 instead of 10, because that's a shift instead of a division, but maybe your compiler can optimize the division-by-constant to some weird multiplication thing.
|
# ¿ Dec 9, 2014 06:57 |
|
Boz0r posted:What are the best unit testing frameworks for C++ out there? I don't have a ranking but I think googletest worked fine for me.
|
# ¿ Dec 10, 2014 17:36 |
|
That downside is just made-up though, that problem never happens.
|
# ¿ Dec 14, 2014 03:31 |
|
GrumpyDoctor posted:It's not just "very obviously bad;" it's actually invalid. So is dereferencing a null pointer. So your point is null.
|
# ¿ Dec 17, 2014 18:56 |
|
LookingGodIntheEye posted:I get a segmentation fault. This happens regardless of which particular node pointer I am using. On what line do you get the segmentation fault? Just add a bunch of printf statements to figure out what's going on. You can print out the memory address of a pointer with %p.
|
# ¿ Dec 23, 2014 10:22 |
|
It's not the file, your code is just wrong. Reason out what your code is doing using logic, or use a bunch of print statements to look at what your code is actually doing, and you'll find out what the problem is.
|
# ¿ Dec 23, 2014 12:53 |
|
Virtue posted:Is my best bet still one of the books in the OP or should I be looking to some online course which might better suit my needs? I'd like the feedback from a course if I get stuck on something but I'm sure I could get feedback from somewhere on the internet if I get stuck. Koenig & Moo is still a good book. I recommend it, because I know it's good and lack experience with the other recommendations in the OP. Virtue posted:Should I start with a windows IDE like Visual Studio or would it be better to start with a linux VM and work in vim and compile with gcc? I've heard conflicting responses about this from some programmer friends. Working in Vim would be stupid because Vim is stupid. You could use a reasonable GUI text editor such as Emacs, or KEdit, or Gedit or whatever, or use some bad Linux IDE. And yeah, working in Linux and compiling with gcc is a fine option. But so is Visual Studio. Developing on Windows annoys me for lots of reasons but not in any way that would hinder your learning of C++. Visual Studio is a fantastic, premier tool for C++ development. Right now I'm developing on Windows, and I'm using Emacs and calling cl from a batch script on the command line, most of the time, though because that's less annoying than figuring out how to configure VS to do indentation as The LORD intended. But then I fired up VS a few times for the debugger. If you're running Windows I'd just go with VS. Edit: Actually VS and Windows can be somewhat annoying when starting out. Here's why. On Linux, when you go to run your program, you just run it from the command line. And then you see the output of your beginner program on the command line. In VS, when you go to run your command line program, it opens up a stupid little window and then your program runs and it's done and the window closes. So you can't see your output. This is why you see so many tutorials online saying to use system("pause") at the end. It's also just annoying, not having a log of your various runs of the program, in a single terminal window. But whatever. Do what you're comfortable with. You could always change your mind midway through. It's not like it'd be hard to switch to Linux, you just run g++ -Wall -Wextra -Werror *.cpp to compile everything. sarehu fucked around with this message at 08:08 on Dec 25, 2014 |
# ¿ Dec 25, 2014 08:02 |
|
Virtue posted:I see a ton of them available like VS Express 2013 for Windows Desktop. Edit: VS has worse error messages than clang or even modern g++, though, so that's another reason you might want to go with them... unless you want the "classic" C++ learning experience.
|
# ¿ Dec 25, 2014 10:18 |
|
xgalaxy posted:Kedit or Gedit over VIM? You are stupid. No. You are the one that is stupid. Also prefer nedit over vim. Prefer nano over vim. Thank you.
|
# ¿ Dec 25, 2014 21:47 |
|
ani47 posted:If you're getting vs get the community edition - it's the free version of the pro version. Oh, wow, yeah, get the community edition. Thanks.
|
# ¿ Dec 25, 2014 23:22 |
|
Well at least now I know about x-macros.
|
# ¿ Jan 29, 2015 10:20 |
|
|
# ¿ Apr 27, 2024 16:15 |
|
Does anybody know where I can find good documentation on windows calling conventions? Most specifically, 32-bit cdecl? This, for example, is a pack of lies. I found this blog article, but it has some wrong information -- for example I have a naturally packed strict of size 3 that's using a hidden return pointer. Maybe the author put a char and a short in a struct and thought it had size 3, I don't know.
|
# ¿ Jan 29, 2015 22:06 |