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
Subjunctive
Sep 12, 2006

✨sparkle and shine✨

duck monster posted:

Whats the [] square brackets do? (Excuse the dumb questions, I'm reteaching myself C++ after nearly a couple of decades away from the language, and C++11 seems to introduce quite a few new ideas. (I'm in love with the iiterator based for syntax!)

The square brackets indicate what variables are captured by the lambda, and whether by value or reference. Wikipedia with examples.

I found the Wikipedia C++11 page to be a great summary of stuff that changed since I last rode that bike.

Adbot
ADBOT LOVES YOU

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

UnfurledSails posted:

I'm an undergrad on a systems track and I've never done anything besides assignments and projects for classes (all of them in either C or C++11). I'm about to finish a class on operating systems implementing Pintos, and while it's fun and challenging, I have zero ideas about what I can do to feel less worthless as a programmer in the real world. Any advice?

Find something interesting on GitHub and fix an outstanding task. Working in other people's code is an important skill, as is going through code review. The Awful iOS app could be a good place if you're into that.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

FamDav posted:

tbf if you want to work on a good app you should probably avoid awful

Working on good code isn't really the norm, honestly even for me dealing with my own previous stuff. Builds character, or something.

Tip, though: if you see pthread_* just back away. Life's too short for raw shared-state multithreading.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

shrughes posted:

code:
$ ack pthread_ src | wc -l
102
Yikes.

Yeah, it's probably too late for you, and someone has to do the dirty work, but if you're looking for something to get started on raw POSIX threading is not a sign of joy. Don't get me wrong, I've done my time (even with linuxthreads, may it rest in agony), I just don't wish it on people. Too many guns for so few feet.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

duck monster posted:

Why not use an S-Expression parser for this? Theres a number of decent implementations out on the net.

That seems sort of overkill to avoid having to type some nested literals, no? His big problem isn't parsing, it's manipulating the resulting tree.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Suspicious Dish posted:

Pattern matching is a very common operation in Lisp-likes, and usually part of the parser.

You can probably do his simplification stuff with template metaprogramming too, but I thought he was a little more constrained that that in terms of what he had to implement Visitor-wise for the assignment.

I've written optimizing compilers, but I don't think I've ever seen a parser do meaningful Boolean expression simplification; it's a lot easier to walk the resulting AST, and that lets you use constant propagation and CSE and such to do a more complete job. If I pulled in a s-expr processing library and it started "simplifying" the resulting tree for me, I would make unpleasant sounds. I guess if his syntax exactly matches Lisp, and he wants to hook the reader somehow, but...a lot of work to avoid typing some structure literals and a few pattern matches. :shobon:

Edit: yeah, he inherited an existing class hierarchy Expression representation, so transcoding through a Lisp parser seems unlikely to be a suitable approach. That said, "just use sexps"/"embed a Lisp" are underused approaches to problem solving for sure.

Subjunctive fucked around with this message at 04:36 on Mar 18, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Nippashish posted:

Literally the only place this is relevant is if you are processing transactions and your numbers represent actual values of currency that belong to someone. Even banks use floating point numbers for simulations/forecasting.

Using double representation also means you really can't use == without epsilon ranges, which is often a source of annoying and hard-to-diagnose bugs. (C.f. the perennial 0.1 + 0.2 == 0.30000000000000004 confusion from JS programmers.) For a lot of stuff, including statistical analysis, there's enough "fuzz" to hide this stuff, but if you're dealing with a small number of inputs then it can be frustrating to manage and display correctly.

I'd grab a decimal-arithmetic library and let it do the hard work, personally.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

hooah posted:

I want to iterate over each Vertex in the inner vector except the first one. I tried using a for loop, but the condition needs to be a constant expression, so I can't say "go from 1 to [inner vector].size()". How else can I go about this?

Go from [inner vector].size() backwards to 1? I may not be understanding the constraint correctly. Or go from begin()+1 to end(), which is more idiomatic I guess.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Paul MaudDib posted:

It's valid to treat the output of an RNG as "N random bits of output", right? So if I have a RNG that outputs a long (64 bits), I can treat that as 2x32 bits?

The syntax on this is working fine (a union), I just want to be sure there's not something theoretical I'm missing.

A good PRNG is equally random across all bits (or you would be able to partially predict the bits that are less likely to change). A bad PRNG is probably less balanced, but there are lots of reasons not to use a bad one. "True" randomness like /dev/random will also be equally distributed.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

hooah posted:

Since this is due soon, I'd rather not do a major re-write if I don't have to, but I'll keep this in mind.

You might also want to just write a routine to print the graph (could use gnuplot format), so that you can verify it, or call it from the debugger when things get weird. Also makes for good record-and-compare for regression testing.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

We just released our custom C/C++ preprocessor, Warp. It had a surprisingly large impact on our build times.

https://code.facebook.com/posts/476987592402291/under-the-hood-warp-a-fast-c-and-c-preprocessor/

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

GrumpyDoctor posted:

People actually use D for things??

Well Andrei obviously does.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Bonfire Lit posted:

printf's %s format specifier also expects a narrow string (aka char*). So when it gets a wide string as its parameter (which is UTF16LE on Windows), it'll output a byte from the string (which will be the least significant byte of the first wide character), and then, since you pass in UTF-16 encoded characters from the ASCII range, the next byte will be a 0x00, at which point printf stops. If you want to print wide strings with printf, you need to use %ls instead of %s. Alternatively, if you want to use iostreams, use std::wcout and drop the asterisk from your argv access.

This is such a common bug, it's too bad MSVC doesn't warn for it. :(

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Tusen Takk posted:

Anyone have any idea why it wouldn't be recognising Alumni but it recognises Student?

Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.

Because you're including Student.h and not Alumni.h (Alumnus, ahem).

Unsigned ints are often the right thing; array indices for example tend to fit them. What did you think was wrong with them?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

A stack of things bigger than what you can fit in an unsigned int would be pretty impressive.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

shrughes posted:

Unsigned int is the maximal code smell as far as unsignedness goes.

I'm curious, why don't you like unsigned int? I use it a fair bit for "unsigned, natural machine word size". I like the "no negatives" documentation element, and using the natural size would keep the compiler from having to generate masking or whatever like you could see with short or whatever (perhaps more historically than now).

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

shrughes posted:

Or it's being used in places where a size_t would be the correct choice, or a specific choice of uintN_t.

Yeah, that's true. Guess I have a decade of habit to reform!

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

ExcessBLarg! posted:

If the array is hardcoded, I typically compute the number of elements as "sizeof(array)/sizeof(array[0])". Since the result type of the sizeof operator is size_t, then size_t would be the appropriate index type.

I think he's asking about the storage type rather than the index type; the latter is size_t all the way IMO.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

shrughes posted:

What makes you think char is a signed type?

IIRC it's signed on x86 (gcc/llvm/MSVC) and unsigned on ARM by default, because ARM didn't have a sign-extending byte load instruction. Regardless of char's signedness, though, char, unsigned char and signed char are distinct, and char isn't compatible with the other two.

Yay C.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

ExcessBLarg! posted:

What makes you think that shrughes thinks char is an unsigned type?

Only shrughes can settle this burning debate!

Edison was a dick posted:

It's got the wrong method signature
code:
GETLINE(3)               Linux Programmer's Manual               GETLINE(3)

NAME
       getline, getdelim - delimited string input

SYNOPSIS
       #include <stdio.h>

       ssize_t getline(char **lineptr, size_t *n, FILE *stream);

Wrong getline.

http://www.cplusplus.com/reference/string/string/getline/

It does consume the delimiter, FWIW. (I'm not sure what's wrong with the code at first glance.)

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Zopotantor posted:

Actually, the index type in C++ is ptrdiff_t (13.6.13). :science:

Touché.

Edison was a dick posted:

Yep, hence the comment about being sarcastic.

I'm not a fan of C++'s IO APIs, I'd prefer standardising fopencookie and coming up with a better way to extend printf and scanf than this.

I missed that as sarcasm. But there are no good I/O APIs. It may be impossible.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

It's totally defined behaviour. rooster[100] refers to the address 100 * sizeof int past rooster. The program has to store there.

Which is almost certainly where i and j are, on the stack...

Edit: what warning does the compiler emit? It's entirely legal to index an array beyond its declared size. You see it commonly in structures with variable-length components at the end.

code:

struct message {
  int length;
  char buf[0]; // allocated contiguously following this header
};

Subjunctive fucked around with this message at 00:09 on Apr 12, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

pseudorandom name posted:

Accessing beyond the end of an array is only defined for flexible array members, which can only occur at the end of a struct.

Ah, right. Though actually in this specific case it's defined, I believe:

C99 sec 5.7 posted:

If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

Edit: bleh, right, not to dereference.

I'll get my coat. (I didn't mean to imply that the aliasing of i and j was defined, by any means.)

Subjunctive fucked around with this message at 00:34 on Apr 12, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Tusen Takk posted:

I have to write a function template max_generic() that takes as input an array of either int, float, or double, and returns the maximum element in the array. My professor's ppt slides are less than clear on this, and so far everything I've found on the internet is way over my head. I know how to find the max value of an array, but I don't know how to make it so that it can take multiple types. Going by what I read, I guess if you go

C++ code:

#include "Template.h"
#include &lt;iostream&gt;
using namespace std;

template &lt;typename T&gt;

void print+max( const T array, int count )
{
    for(int i=0; i &lt; count; i++)
    {
        if( array[i]&gt;temp )
        {
            temp = array[i];
        }
    }
    cout &lt;&lt; "The maximum value of the array is: " &lt;&lt; temp &lt;&lt; endl;
    return 0;
    
}

then take user input from the main class and pass it to the array that should populate the array? Should I make the user input from type auto or should I make user input of type T like the array is of type T?

You want to

- declare and initialize temp (be careful how you choose the initial value; imagine an array with all negative values)
- make const T[] array

and then it looks like it should work.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Tusen Takk posted:

Is there a reason why you replaced &lt; and &gt; with &lt and &gt?

No, it's a bug in the latest Awful.app. Sorry!

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Tusen Takk posted:

I wanted to use a vector but the professor wants an array, and the professor has ppt slides that say you can use -> to insert things into arrays. I have no idea why I thought taking this class would be a good idea, honestly :saddowns:

edit: gently caress it i'm switching to vectors

Are you sure that -> thing isn't just pseudocode?

Sorry about the array-decl error, got distracted by a bug I introduced into Awful that the [] tripped, and stopped paying attention. :downs:

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

roomforthetuna posted:

Can they be overloaded separately from the regular operators? That could make them A. kind of useful, and B. hilariously confusing.

No, they're just alternate spellings, like trigraphs, in case you have a weird character set missing & or { or whatever.

e;FB

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Hammerite posted:

But assuming you have an editor that does syntax highlighting, there is no issue with "operators looking like identifiers".

What editor do you use that highlights the alternative tokens? They're just macros, so either it special-cases things that transitively include iso646.h and handles redefinition properly, or it's expanding macros to find those that work out to single operator tokens. Those both sound like hanging offenses.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

xgalaxy posted:

In Xcode or other standard compliant IDE's they are treated like regular operators, not macros, and are highlighted as such.

Is that the case only if iso646 is included transitively and nothing is redefined? (In the C case; they're part of the language in C++ but not C IIRC.)

Edit:

Per http://en.wikipedia.org/wiki/C_alternative_tokens#C.2B.2B

Wikipedia posted:

The abovementioned identifiers are operator keywords in the ISO C++ programming language and do not require the inclusion of a header file. For consistency, the C++98 standard provides the header <ciso646>. However the latter file has no effect, being empty. Notwithstanding some compilers, such as Microsoft Visual C++, do require the header to be included in order to use these identifiers.

Hmm.

Subjunctive fucked around with this message at 19:00 on Apr 16, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Plorkyeran posted:

Or it could just have them in the syntax highlighter's hardcoded list of things to highlight and just accept that it'll sometimes highlight things it shouldn't. Syntax highlighters getting edge cases wrong is not at all unusual or a big deal.

Yeah, and I guess it can tell identifier from operator most of the time based on context anyway. This bothers me more than it should, I'll do some breathing exercises.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Suspicious Dish posted:

Yes. But if you have a variable named "not", you deserve to get your code broken.

Not that uncommon if you're building an AST or similar, in my limited experience. (And "compl" is out of bounds too?)

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

fankey posted:

I can receive multicast UDP, send/receive unicast UDP and make TCP/HTTP connections without any issues. The only thing that doesn't work is sending multicast UDP.

Is this because Bonjour coopts multicast UDP? I remember having weird interactions between DNS and mcast UDP on OS X, but it was a long time ago. (mcast routes weren't set up until Bonjour initialized, and with broken DNS it failed, or something.)

I think this is probably an Apple-related thing, but I'm not certain.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Generally, my best experiences have been:

- use VS on Windows, because you get a debugger that work
- build from the command line either with nmake or mingw-make
- package everything you depend on, including the CRT redist and DirectX installer

The mozilla-build package has a pile of stuff all bundled up that can help a lot; it's what Mozilla uses for the Win32 development side of Firefox and friends, and I always start with it now because I get bash and python and the paths all set up etc etc.

https://wiki.mozilla.org/MozillaBuild

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Edison was a dick posted:

I don't program for windows, but I understand you can cross-compile from Linux to Windows with MinGW.
If I were to do windows programming, this is what I would do, but that's because I prefer programming in a Linux environment.

In my experience this is the falsest of economies. Even if you do all your development on Linux, compile on Windows to test and distribute. Please. For the children.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

GrumpyDoctor posted:

Yeah, any function that accepts a non-const reference (especially a constructor) is doing something weird, and if its weird behavior is not clearly documented somewhere then you should be very wary.

Would this be any better if the reference were const?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Second-guessing the compiler's register allocation is a trail of tears, IMO. See what the profiler says, I doubt it'll consider the extra caller-saves unused register pressure to be a big issue.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Lemon King posted:

I've been working with C/C++ for a few days now and I'm kinda curious about what compiler I should be using.
I started off with MinGW that came stock with Code::Blocks until I found out there were issues and now I'm using MinGW from Sourceforge.

All my experiences with MinGW have ended in tears, and then when I had to use a debugger the tears were of blood. Use MSVC express, drive it from nmake/CMake/gmake/gyp as you choose.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

nielsm posted:

x<int> is a function, not a type, so yes you can't typedef it to something.

But I think you should be able to "using" it, at least with a recent compiler:
C++ code:

using xint = x<int>;

Or just

code:
inline int xint(int i) { return x<int>(i); }

which should work everywhere?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Yes.

Adbot
ADBOT LOVES YOU

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Vanadium posted:

This is why exceptions are terrible~

longjmp or go home.

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