Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
sarehu
Apr 20, 2007

(call/cc call/cc)

Smackbilly posted:

pthreads question:

Is there any disadvantage to creating lots and lots of threads over the course of a program's execution, if only a couple will exist at any given time?

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.

Adbot
ADBOT LOVES YOU

sarehu
Apr 20, 2007

(call/cc call/cc)

schnarf posted:

code:
#define EVENBITS(a) (a & 1) | ((a & (1<<2)) >> 1) | ((a & (1<<4)) >> 2) | ((a & (1<<6)) >> 3)

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:
uint32_t evenbits(uint64_t x) {
  x = (x & 0x1111111111111111LL) | ((x & 0x4444444444444444LL) >> 1);
  x = (x & 0x0303030303030303LL) | ((x & 0x3030303030303030LL) >> 2);
  x = (x & 0x000f000f000f000fLL) | ((x & 0x0f000f000f000f00LL) >> 4);
  x = (x & 0x000000ff000000ffLL) | ((x & 0x00ff000000ff0000LL) >> 8);
  x = (x & 0x000000000000ffffLL) | ((x & 0x0000ffff00000000LL) >> 16);
  return (uint32_t) x;
}

sarehu
Apr 20, 2007

(call/cc call/cc)
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:
01001010
 ^ ^ ^ ^  the even bits
The expression evenbits(74) should return the number 8, which has binary representation 1000.

sarehu
Apr 20, 2007

(call/cc call/cc)

bcrules82 posted:

sarehu: do you have a reference/document for this?

No.. I just made it up on the spot.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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?

sarehu
Apr 20, 2007

(call/cc call/cc)

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:
$ time ./foo
foobar foo

real    0m0.026s
user    0m0.002s
sys     0m0.005s

sarehu
Apr 20, 2007

(call/cc call/cc)

Entheogen posted:

hahah, this is a joke, right?


If you are asking this seriously, then the answer is no. There is no general algorithm that will take another arbitrary algorithm and determine if it will ever halt or not. Look up halting problem on google if you don't believe me.

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

SuddenExpire posted:

code:
int testint = HashArray.size();
Can anybody spot what I'm doing wrong?

HashArray->size()

sarehu
Apr 20, 2007

(call/cc call/cc)

Entheogen posted:

I used pointers before to switch between two arrays that contain same types.

such as
code:
vector< int > A, B;
vector<int> * curr = & A;
while( true )
{
   // do something 
   if( curr == & A ) curr = &B;
   else curr = &A;
}
Is there a better way of doing this without pointers?

Yeah, use a function.

code:
vector<int> A, B;
while(true) {
  f(A);
  f(B);
}

sarehu
Apr 20, 2007

(call/cc call/cc)
Ultimately somebody can always throw you one of these:

code:
0 - 0 + 1 - 0 + 3 - 0 + 27 - 0 + 81 - 0 + 243 - 0 + 729 - 0 + 2187
and there's nothing you can do about it.

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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

sarehu
Apr 20, 2007

(call/cc call/cc)

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
code:
0x12 0x13 0x14 0x15
or
code:
0x12131415
Google search isn't helping me on this.

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

hooah posted:

It seems the man page and g++ are telling me different things. If I try to do
C++ code:
char *test = parameters[i].c_str();
I get an error "invalid conversion from 'const char* to char*'", with no mention of some deeper array level.

.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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)
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

sarehu
Apr 20, 2007

(call/cc call/cc)

Presto posted:

Not much because you really want sizeof(*s). :)

Oh.

Okay then...

:smithicide:

sarehu
Apr 20, 2007

(call/cc call/cc)

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:
char p[] = "abc";
to be a special case syntax that means
code:
char p[] = {'a', 'b', 'c', 0};
And the normal meaning of "abc" is just syntax for a const pointer to statically allocated char array.

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

Pixelated posted:

What am I missing?

Try running a simple program that sets and unsets BIT5 on P1OUT and see what happens.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

I'm trying to make a C function that can take a mathematical vector function as the 3 separate components, ie: Vector-A = <Ax,Ay,Az> where each of those 3 are 3-variable mathematic functions that can get pretty complex. I don't really know though how I should handle the function inputs so that they can be parsed as a mathematical expression that calculations can be done on where x, y, z are all arrays of values.

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

sarehu
Apr 20, 2007

(call/cc call/cc)
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

sarehu
Apr 20, 2007

(call/cc call/cc)

Zerilan posted:

By each component I think I meant R^3->R

Erm.. yeah.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)
"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

sarehu
Apr 20, 2007

(call/cc call/cc)

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

sarehu
Apr 20, 2007

(call/cc call/cc)
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:
struct Bucket {
    int size;
    int a[10];
    ...
as a clear improvement to what you have, with the limitations that you have.

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

sarehu
Apr 20, 2007

(call/cc call/cc)
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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)
That downside is just made-up though, that problem never happens.

sarehu
Apr 20, 2007

(call/cc call/cc)

GrumpyDoctor posted:

It's not just "very obviously bad;" it's actually invalid.

So is dereferencing a null pointer. So your point is null.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)
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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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

sarehu
Apr 20, 2007

(call/cc call/cc)

Virtue posted:

I see a ton of them available like
I don't understand a chunk of this so I guess I'll start with VS. Which version should I start with? On the dreamspark website I see VS Pro 2013 with Update 4, VS Express 2013 for Windows Desktop Update 4, VS Express 2013 for Web with Update 4, and like 15 others.

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)

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.

sarehu
Apr 20, 2007

(call/cc call/cc)
Well at least now I know about x-macros.

Adbot
ADBOT LOVES YOU

sarehu
Apr 20, 2007

(call/cc call/cc)
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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply