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
Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

ShoulderDaemon posted:

No, it's just habit from other languages which require it.
Speaking personally, spending a semester writing a Pascal compiler gets you into positively bizzare syntax habits. It got to the point where I started doing this, myself :)

Adbot
ADBOT LOVES YOU

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

TheSleeper posted:

Uh, C++ follows the proper order of operations. If somebody told you x = 3 * 2 + 1 + 2, would you say that x is 9 or 15? Obviously 9. Why would C++ do it any other way?
To make it easier for compiler writers :downs:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

floWenoL posted:

Are you sure?
well it's random so there's no way to be sure :pseudo:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

hexadecimal posted:

Oh... lol you got me there. %2 would do what i meant to do.
I'm curious, why did you suggest this way rather than permuting an ordered list of the pairs? (to be clear: this isn't meant as a jab at his coding, but is there a facet to the problem that makes the push_front/push_back procedure better?)

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

hexadecimal posted:

Can you show me how to do this?

code:
int[] ret;

for i=0..9 do
  ret[i] = i;
end


for i=0..9 do
  int j = rand() mod n;
  int k = rand() mod n;

  pair temp = ret[j];
  ret[j] = ret[k];
  ret[k] = temp;
end
edit: added array generation just to make it clear, and changed a variable name that I used twice.

Dijkstracula fucked around with this message at 03:40 on Jan 10, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Ugg boots posted:

Ugh, this still isn't a properly shuffled array.
What's missing? Is n swaps too few for an array of size n?


vvv Kluth Shuffle is linear, so I don't think that's it.

Dijkstracula fucked around with this message at 03:49 on Jan 10, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Victor posted:

Genius! I suppose this is a standard way of reasoning if you have formal CS or maths training, but i don't. I had seen this idea before, but I think you just locked it in my slow brain.
Hell, I do have CS/math training and that simple counting argument didn't occur to me. Thanks, floWenoL.

edit: yep, I'm still embarassed.

Dijkstracula fucked around with this message at 03:56 on Jan 10, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

honeymustard posted:

Hmm, I think I understand that. What I originally wanted was one line within the loop, like:

cin >> cardvalue[n]

So each time it would increment the variable name itself, but I couldn't figure out how to use that, or if it is even possible.
While you can't increment based on the name of the variable, there's nothing stopping you from having an array of chars and iterating over that.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Mustach posted:

code:
#define ARRSIZE(a) (sizeof(a)/sizeof(a[0]))
I'm sure there are other uses.
It's not the same thing - the expression he's talking about is the parameter to sizeof().

Also, this directive strikes me as dangerous.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

ShoulderDaemon posted:

...yes? Mustach passed a[0], an expression, as the parameter to sizeof.
Ahh true, of course. I was thinking of expressions beyond a simple identifier.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Good lord, can people not write out "I don't know" anymore?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Ugg boots posted:

What if you use the C shell (csh)?
To be fair, csh scripts aren't in C :)

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

shrughes posted:

That would be a std::map<std::string, T>.
Indeed, but if this is an assignment, going overboard with STL containers and other stuff that he's guaranteed to not have been taught will probably not result in him getting a good grade.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Ledneh posted:

Brief googling says C, and that's what I suspect as well, but I just want to confirm.
Indeed; whatever happens to be on the stack will be what gets returned.

Wow, that's certainly an overlooked compiler warning.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Speaking of string functions, I'm porting a side project that I started working on in OSX to Linux and I see that the strlcpy/strlcat functions don't appear to be in Interpid's glibc...any idea why this might be the case? Is this just an Ubuntu thing, or is it not to be found anywhere in Linuxdom? At school we had "STRLFOO(), NOT STRNFOO(), AND CERTAINLY NOT PLAIN OLD STRFOO(), YOU IDIOTS" beaten into us, and those machines were all running Linux, so maybe the functions were added in after the fact...?

I'd make a disparaging comment about Linux, but I think I'll save that for YOSPOS.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

edit: Bah, missed the pagebreak; functional has said it all already.

Vanadium posted:

Check the declaration of x, then.
Indeed, really, without the compile-time type information, C has no idea whether some address in memory is intended to be, say, the number 84, or the character 'T'. There's no equivilent to Java's "instanceof" in C.

What are you trying to do? It sounds like your design is rather awkward.

edit: Now, are you trying to do something like:
code:
   float list = [3.14f, 4.0f, 2.718f, -2.0f]
   for (i = 0; i < 4; ++i) {
      if (is_float_equivilent_to_an_integer(list[i]) {
         printf("%f is a floating point representation of an integer\n", list[i]);
      }
   }
which would print the 4.0 and the -2.0?

Dijkstracula fucked around with this message at 17:19 on Feb 23, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

I feel kind of bad for Manner Please; my compilers class was taught by a crunchy old Unix greybeard so we had no choice but to use lex and yacc and good old C, with none of this OO design pattern stuff.

Is it typical for compiler classes to involve writing a lexer and parser instead of using preexisting tools? Why re-invent the wheel?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

code:
printf("%d", "diceRolls[35]");
Look carefully at this line. Specifically, what is the type of the second argument, and what is the format character you're specifying in the first argument? Which is the wrong one?

edit: If you make the correction, you'll see that diceRolls[35] is in fact 0. Also, using the debugger is better than sprinkling printfs around your code.

Dijkstracula fucked around with this message at 18:13 on Mar 29, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

If you're serious about wanting to learn the language, you should buy a book instead of relying on lovely and outdated web tutorials.

See the first post for recommendations.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

Also if you're actually going to try to determine where something segfaults, you should be using cerr instead of cout.
If you're trying to determine where something segfaults, why are you not compiling with -g and running it through the debugger? I mean, that'll tell you more or less exactly what the problem is.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Fehler posted:

Yeah, but isn't that pretty limited in its functionality and/or has some weird license restrictions?
It doesn't have every feature that, say, Ultimate Professional Team Edition has, but for hobbists and single-person projects it's just fine. Really, if your project really requires 64-bit support or profilers whatever, plunk down the $99 already. (Also, there are no restrictions on using Express for commercial use )

(also, this is where I tell you that Win32 C++ development is infuriating and you really want to use C# instead :v: )

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

androo posted:

:D I love working with arrays because they're so drat simple (and in terms of tiled 3D space, they're perfect for a beginner like me) but if you can tell me what you started picking up afterwords that would be sweet.
I think what AD is alluding to is

quote:

Or 4-dimensional, if I wanted to store a bunch of data for each cell?
Instead of an N+1-dimensional array, you absolutely want an N-dimensional array of tile structures or objects. There's a tendency for beginners to think of their data like this:

code:
    int world[256][256][17] /*World is 256x256 tiles, where a tile is an array with 17 elements
                              0th entry is the background colour
                              1st entry is the foreground colour
                              2nd entry is a pointer to the enemy in the tile
                                               ...
                              17th entry is whether the tile is visible to the player

                              Eat your heart out, John Carmack
                            */
This becomes unmanageable for many reasons. Thirty seconds after you write this, you will forget what the 11th entry is supposed to be. Sixty seconds after that, you'll swap numbers in your head and use the wrong entry, and you'll never figure out what the bug is. (also, for bonus points, think about the case I gave where you want to store a pointer to something using an int, because your array is only of type int. When will this work? When will it horribly explode?)

What you want is closer to this:
code:
   struct tile {
      int bg_colour, fg_colour;
      struct enemy *curr_enemy;
         ...
      int is_visible;
   }

   struct tile world[256][256];

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

Let's not forget system("pause");
Well, if you know of a better way to set breakpoints I'd love to hear it :colbert:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

code:
guess = rand() % (high - low) + low; // guess based on new max value
Are you sure about this line?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Yikes.

Ledneh posted:

(since you can't put std::strings in a union I guess)
It would require a bit more bookkeeping on your part to ensure doublefrees, memory leaks, segfaults, etc, don't happen, but why not hold a pointer to a malloced newed string?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

slovach, can you post some code? Give an example of a value that you assign, and the garbage that get written in, and the code that does it? There's not much to work with here.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Adahn the nameless posted:

I'm trying to write a program that reads an ASCII file and writes the same data to a binary file. Every time I run the program I'm getting a segmentation error, which I understand occurs when a pointer goes out of scope.
That's not at all what a segfault is.

What is source? What is designation? Are they opened correctly? Are you doing proper error checking when you open the streams/file descriptors? Have you run this through a debugger to pinpoint the line where it's crashing?

edit: ah, didn't catch the missing addressof operator.

Dijkstracula fucked around with this message at 21:51 on Jun 24, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

The last time I looked at C++/CLI it was kind of a clusterfuck. If you're set on using .NET for the rest of the application, using a proper .NET language like C# with P/Invoke is the way I would go.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Adhemar posted:

A while loop you say? I never would have guessed.
I find "#define FOREVER while(1)" makes commenting the loop unnecessary! :downs:

(I've seen this in code more times than I really should)

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

Just FYI, I work on a C++ compiler in my spare time, and I have read precisely one "book" on C++: the ISO standard. All of those other books are completely unnecessary in my opinion. I learned everything by writing code and looking at other people's code.
I agree. It's too easy to fall into the trap of reading a lot about programming but never actaully doing any. The other side of this, though, is you need to know a priori what code is worth reading and worth learning from.

A code reading thread with suggestions could actaully be interesting and enlightening.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.
This is where the wiser among us would pipe in and say, "For instance, I like this program's code (see source files foo.c and butt.c) because of __, __, and __, but I'm not fond of __ and __ (see source files foo.c and butt.c)" to kick-start the discussion.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

The Good Professor posted:

Hello, I hope you guys can help me. I'm learning C# as my first programming language (I have a friend who is helping me a bit, thus my choice) and I'm looking for a good book to get to help me learn it from scratch, especially regarding game programming, as I feel I've about reached the bounds of what I can teach myself without direction.

If this should be in the .Net thread I'll post it over there but it's not clear to me which thread I should be asking in. Thanks.
Yes, the .NET thread is where you should be. That said, whenever people ask this question I automatically refer them to O'Reilly's Learning C# by Liberty.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

does it throw the bus error immediately (ie. the first time through the loop) or does it get part way through the traversal before it crashes? Either way, it sounds like your routine for building your list is maybe stomping on memory somewhere, or something's misalligned, or something like that. (I would venture it's an x86 vs sparc issure as opposed to a Linux vs Solaris one.)

edit: Do me a favour and set a breakpoint and inspect what listptr points to. If it's an invalid region of memory, it should say.

Dijkstracula fucked around with this message at 03:47 on Aug 19, 2009

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

In general, it's not a reasonable expectation, but in your case, it should be okay. On certain architectures, alignment requirements would pad out the array if sizeof(yourtype) wasn't a multiple of the alignment stride. However, since all those datatypes will be naturally 4-byte aligned (which is the only alignment that I've seen in use these days), that shouldn't pose a problem for you.

If you know something about the compiler that will be used, you can use a directive like __attribute__ ((__packed__)) on a union, but I've never actually needed this, so I can't tell you how the behaviour would play on different architectures.

Also scientific code :rolleye:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

BigRedDot posted:

OH you don't even know. I mean, unless you do.
Oh I know. My favourite was a fluid dynamics app that, as part of the build process, had to build its own copy of gcc. Getting that one to comiple with Intel compilers was a comically huge amount of work.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

6174 posted:

Yeah, certainly alignment in general can't be assumed, but for the types in this particular union it seemed like a reasonable expectation.


Unfortunately I've got no clue what compilers will be common in the future. For now I'm using gcc, and trying to keep from using any odd extensions and attempting to make explicit the various assumptions I'm making.
Yeah, what I was trying to say was, by virtue of your types in that particular union, you should be okay, but you shouldn't extrapolate that to an arbitrary union was what I was getting at.

Honestly, if you test this with, say, gcc and icc, in 32-bit x86 and amd64/x86_64 envionments, I would say it's highly likely that it will work for the forseeable future.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

BigRedDot posted:

LOL that's awesome, I hope that was 10 years ago.
Last fall :suicide:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Vanadium posted:

%s is for strings, not for int arrays. I am pretty sure there is no format specifier that lets you read multiple integers at once.
code:
        int a[3];
        scanf("%d %d %d", &a[0], &a[1], &a[2]);
        printf("%d %d %d", a[0], a[1], a[2]);
code:
$ ./a.out
1 3 4
1 3 4
:confused:

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

schnarf posted:

You can read in a single integer, say, 12345, and use the modulus and integer division operators to read out digits.
Or, you know, sprintf.

code:
        int i;
        char buffer[7];

        scanf("%d", &i);
        if (i >= 10e5) {
                printf("Number too large to fit in six bytes (inc \0 terminator)!\n");
                exit(1);
        }

        sprintf(buffer, "%d", i);

        for (i = 0; i < 6; i++) {
                if (!buffer[i]) break;
                printf("%d: %c\n", i, buffer[i]);
        }

        return 0;
code:
$ ./a.out
13254
0: 1
1: 3
2: 2
3: 5
4: 4
I guess we're still not sure about what he's precisely trying to do. Read in multiple, delimited integers? Or extract digits from a integer?

Dijkstracula fucked around with this message at 01:42 on Aug 23, 2009

Adbot
ADBOT LOVES YOU

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

HondaCivet posted:

Is there a way to take in input without causing the program to hang? I'd like the program to check for input each time it runs through a loop but, of course, it waits for input instead of continuing through the loop. I'm using plain old cin (C++ here with Boost). What else could I do?
Since it looks like you're writing a socket program, you most certainly want to separate the network from your UI instead of, say, polling the iostream every time you go through the loop. The three canonical ways of handling multiple connections are:

1) fork() a process to handle the network connection, using signals handlers to inform the child network process to terminate and clean up
2) use a threading library like pthreads or whatever Boost gives you, where you call [fixed]pthread_exit[/fixed} or whatever.

(and the third, comedy option:

3) whatever crazy Boost poo poo there is (Is boost::asio still around? I still have nightmares from it; that poo poo was seriously my Vietnam.) )

I assume the problem you're trying to solve is "I'd like to include a little I/O on this thing so that transfer can be shut off with a text command." Any of those three solutions will work; beej has a pretty good Unix IPC guide if you want to go with 1).

Dijkstracula fucked around with this message at 23:14 on Aug 26, 2009

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