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
Scaevolus
Apr 16, 2007

fret logic posted:

Only returns the first character you enter. I thought that with the while loop, it does the first statement, you hit enter, then it does "putchar(c)", but I don't understand why it's holding the entire string of characters. Sorry if this is incredibly obvious

Use Control-D to send an EOF character. (In Linux, at least)

The reason it looks like it's holding the entire string in memory is because stdin is line-based -- nothing is sent to the program until you press Enter. You type in the line of text, and the program then processes that line, one character at a time.

Scaevolus fucked around with this message at 06:24 on Feb 28, 2008

Adbot
ADBOT LOVES YOU

Scaevolus
Apr 16, 2007

fret logic posted:

How is this different from the one without the loop? It'll let you type in an entire string, but only returns the first character. Are you saying that once you hit enter, it repeats the loop for each character in the line? I guess I'm not understanding how the loop affects the process.

Here's how it works:

You: Start program
Program: Initialize, getchar()
[Now the program is waiting for input, and is idle]
You: Input text, [enter]
[Now a line of input has been entered, and the program's getchar() finally returns a value]
Loop version:
--Program: Print character, getchar()
Without loop:
--Program: Print character, exit

Scaevolus
Apr 16, 2007

illamint posted:

the regular expression "^([A-Za-z0-9]+),([A-Za-z0-9]*),([A-Za-z0-9]*),(s?f?|f?s?);$"),

Do all the lines in the file match this? If so, a regex is overkill.

vv also overkill. Writing a DFA for this would be very easy.

Scaevolus fucked around with this message at 02:19 on Mar 7, 2008

Scaevolus
Apr 16, 2007

Thug Bonnet posted:

No, that's what I'm asking! :) I'm just kind of curious, honestly. But how would the program know "where it is" in memory, how large it is, etc. Also, I assume it's not necessarily contiguous..

It depends on the OS. On Linux, /proc/<pid>/ contains a lot of this information, with the files maps and smaps being particularly interesting.

Scaevolus
Apr 16, 2007

Thug Bonnet posted:

No, that's what I'm asking! :) I'm just kind of curious, honestly. But how would the program know "where it is" in memory, how large it is, etc. Also, I assume it's not necessarily contiguous..

It depends on the OS. On Linux, /proc/<pid>/ contains a lot of this information, with the files maps and smaps being particularly interesting.

Example /proc/.../maps file:
code:
00400000-0048e000 r-xp 00000000 08:06 704516               /bin/bash
0068e000-00693000 rw-p 0008e000 08:06 704516               /bin/bash
00693000-0071e000 rw-p 00693000 00:00 0                    [heap]
2b5398859000-2b5398874000 r-xp 00000000 08:06 1146887      /lib/ld-2.7.so
2b5398874000-2b5398875000 rw-p 2b5398874000 00:00 0 
2b5398896000-2b5398897000 rw-p 2b5398896000 00:00 0 
2b5398a73000-2b5398a75000 rw-p 0001a000 08:06 1146887      /lib/ld-2.7.so
2b5398a75000-2b5398aab000 r-xp 00000000 08:06 1148901      /lib/libreadline.so.5.2
2b5398aab000-2b5398cab000 ---p 00036000 08:06 1148901      /lib/libreadline.so.5.2
2b5398cab000-2b5398cb3000 rw-p 00036000 08:06 1148901      /lib/libreadline.so.5.2
2b5398cb3000-2b5398cb4000 rw-p 2b5398cb3000 00:00 0 
2b5398cb4000-2b5398cbc000 r-xp 00000000 08:06 1146884      /lib/libhistory.so.5.2
2b5398cbc000-2b5398ebb000 ---p 00008000 08:06 1146884      /lib/libhistory.so.5.2
2b5398ebb000-2b5398ebc000 rw-p 00007000 08:06 1146884      /lib/libhistory.so.5.2
2b5398ebc000-2b5398f0a000 r-xp 00000000 08:06 1148900      /lib/libncurses.so.5.6
2b5398f0a000-2b5399109000 ---p 0004e000 08:06 1148900      /lib/libncurses.so.5.6
2b5399109000-2b5399118000 rw-p 0004d000 08:06 1148900      /lib/libncurses.so.5.6
2b5399118000-2b5399119000 rw-p 2b5399118000 00:00 0 
2b5399119000-2b539911b000 r-xp 00000000 08:06 1146922      /lib/libdl-2.7.so
2b539911b000-2b539931b000 ---p 00002000 08:06 1146922      /lib/libdl-2.7.so
2b539931b000-2b539931d000 rw-p 00002000 08:06 1146922      /lib/libdl-2.7.so
2b539931d000-2b539945a000 r-xp 00000000 08:06 1148905      /lib/libc-2.7.so
2b539945a000-2b539965a000 ---p 0013d000 08:06 1148905      /lib/libc-2.7.so
2b539965a000-2b539965d000 r--p 0013d000 08:06 1148905      /lib/libc-2.7.so
2b539965d000-2b539965f000 rw-p 00140000 08:06 1148905      /lib/libc-2.7.so
2b539965f000-2b5399666000 rw-p 2b539965f000 00:00 0 
2b5399666000-2b5399b79000 r--p 00000000 08:06 1638711      /usr/lib/locale/locale-archive
2b5399b79000-2b5399bae000 r--s 00000000 08:06 1591741      /var/db/nscd/passwd
2b5399bae000-2b5399bf1000 rw-p 2b5399bae000 00:00 0 
7fff1223b000-7fff12250000 rw-p 7ffffffea000 00:00 0        [stack]
7fff123fe000-7fff12400000 r-xp 7fff123fe000 00:00 0        [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0    [vsyscall]

Scaevolus fucked around with this message at 23:10 on Mar 10, 2008

Scaevolus
Apr 16, 2007

floWenoL posted:

What is this "typing-time"?

typing-time:
code:
char c[6] = "hello";
compile-time:
code:
char c[] = "hello";
typing-time is when you are editing the code

Scaevolus
Apr 16, 2007

elevatordeadline posted:

Teach me about 64-bit integers in C.

In Java, int is four bytes and long is eight bytes. That's not the case in C, where, if I understand correctly, sizeof(long) only has to be at least equal to sizeof(int). So what should I do when I need a 64-bit integer?

#include <stdint.h>

This gives typedefs that include number of bits, like uint8_t for unsigned 8-bit integers, and int32_t for signed 32-bit integers. Only up to 32 bits are required, but your implementation might have int64_t and uint64_t.

Scaevolus
Apr 16, 2007

elevatordeadline posted:

Those numbers are not the same numbers at all. :(

Works for me on x86_64 (:woop: native 64-bit integers), gcc version 4.2.3
code:
8 bytes.
314 31415926535897 6469693230
x = 314,
y = 31415926535897,
z = 6469693230.

Scaevolus
Apr 16, 2007

fret logic posted:

Yeah I know that much, I just haven't found the basic idea behind the graphics in that game, graphics that for some odd reason I really enjoy.
Nethack uses CURSES for terminal text graphics, in this case, probably ncurses. (named for the difficulty of writing such a library).

If you want to examine a simpler game using ncurses, read the source code for bombardier.

If you want something shinier, SDL is probably a better choice than GLUT.

Scaevolus
Apr 16, 2007

TurtleBoy posted:

this sounds like what I am looking for.
Zlib is what Quake 3 does, "assets0.pk3" is just a zip file containing a bunch of data. If they don't think that's enough, try just having the archives password-protected with the password hardcoded in the source code (not effective at all, but the artists don't need to know that).

Scaevolus fucked around with this message at 05:07 on Apr 2, 2008

Scaevolus
Apr 16, 2007

Morgen posted:

attributes.txt
code:
0.40   0.20   0.30   0.60 
0.00030000   0.00020000   0.00010000   0.00010000   0.00006366 
40 20 30 60 
10 
If that's all your attributes.txt is, then try:
code:
int main()
{
	FILE *fp;
	int n;
	char c;
	float L[4], R[5];
        int N[4], Nm;
	int i;

	fp = fopen("attributes.txt", "r");

	fscanf(fp, "%f %f %f %f %f %f %f %f %f %d %d %d %d %d", 
		&L[0], &L[1], &L[2], &L[3],
		&R[0], &R[1], &R[2], &R[3], &R[4],
		&N[0], &N[1], &N[2], &N[3],
		&Nm);

	fclose(fp);

        printf("%.2f %.2f %.2f %.2f\n%.8f %.8f %.8f %.8f %.8f\n%d %d %d %d\n%d\n", 
		L[0], L[1], L[2], L[3],
		R[0], R[1], R[2], R[3], R[4],
		N[0], N[1], N[2], N[3],
		Nm);

        return 0;
}
You can split those fscanf and printf calls into multiple parts, but I think this is clear enough.

&L[2] is the same thing as L+2, but I think the first is clearer.

Scaevolus fucked around with this message at 19:23 on Apr 6, 2008

Scaevolus
Apr 16, 2007

Morgen posted:

I can't seem to make it work, it gives me a runtime error. Am I missing something important?

Well, I did omit "#include <stdio.h>" from the first line.

Scaevolus
Apr 16, 2007

Citizen Erased posted:

Does anyone know of a faster alternative to the standard library vector container? A year or so ago I made a 3D application which relied very heavily on the stl vector...
This is a dumb question, but were most of these vectors representing triples? (<x,y,z>)

If so, it's trivial to implement your own vector class that has three elements. Liberal use of "const" in function prototypes for it will help your compiler optimize it.

Scaevolus
Apr 16, 2007

ruden posted:

As to how I'm drawing it, basically it's nested for loops with graphics.DrawLine() functions. I'm not sure how C++ interprets that exactly, but I'm not drawing pixel for pixel. I'm only using black and white, so color is a minimum too.
You mean grayscale?

And is it impossible for this to be done in sections because parts of the image reference other parts? (Which fractal are you drawing?)

crazypenguin posted:

I *think* gimp handles large files pretty well...

No, it doesn't. Try creating a 20,000x20,000 image if you don't believe me.

Scaevolus fucked around with this message at 18:21 on Apr 28, 2008

Scaevolus
Apr 16, 2007

ruden posted:

Ok, I made the PS file of the image. This is the situation as it stands:

a) small images are made perfectly, the numbers work out and everything and the image is perfect
b) the large image does not display correctly at all, it's cut short on the top and bottom, presumably because I'm doing something on the order of "newpath 10 10 moveto 33600 336000 lineto stroke" and the numbers are just too big to display correctly in a PS viewer
c) the file is not readable in any Adobe reader, it says it is corrupted and unreadable, but various PS readers like GSView etc, can read it fine. If I hand this PS file to the printer, and they can't read it, I'm out of luck, do I need some kind of special header for Adobe to read it?

Thanks.

Look up line-drawing algorithms. You could initialize a 12GB raw image file with zeros, then draw lines by doing file seeks and writes. It wouldn't be pretty, and it might take a very long time, but with good filesystem caching it might be manageable.

Scaevolus
Apr 16, 2007

Assuming each single byte write takes 1/10,000 of a second, and other processing time is negligible, it would only take a week, if 50% of the pixels are filled.

This is the sacrifice one must make for 1400 DPI.

Scaevolus
Apr 16, 2007

Sarah Sherman posted:

Birthday Paradox
Instead of having int birthdays[50], and then doing a linear search to see if a birthday is a repeat, why not have an array int days[365], and increment cells when a day is chosen?

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

code:
#define SQUARE_REAL(x) (x)*(x)

code:
#define SQUARE_REAL(x) ((x)*(x))
:eng101:

Macros are annoying.

Scaevolus
Apr 16, 2007

Evil Robot posted:

Although anyone who argues against this style of function definition or loop formatting can go straight to hell, the fuckers:
code:
void foo(void* args)
{
    for (size_t i(0); i < 5; ++i)
    {
        blah blah blah
    }
}
I think K&R are better programmers than you. :colbert:

Scaevolus
Apr 16, 2007

litghost posted:

Software I worked on knows if endian-ness is swapped by checking known fields. Example, the month as a 32-bit integer. It should never be 0, and will not be greater than 12. If you read it as 213134142 or something, swapped endian-ness. This will only work if you can find a piece of data like this. We just fixed a bug where the date was set to 0/0/0 and it broke the endian swap code, so this is fragile. However, it is better than nothing.

Why not just include a flag in the binary data that defines endianness?

edit: maybe I should refresh before posting

Scaevolus
Apr 16, 2007

ehnus posted:

I'd be willing to bet that due to member padding within data types that the savings you see by narrowing those types won't actually be realized
This.

If your "optimization" causes a bunch of warnings, saves less than 1KB of RAM, and is probably slower, it's not worth it.

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

I was messing around today and I realized that you can make template unions. :3:

C++: "I can't think of a use case right now, but maybe it will be helpful later. Put it in the standard."

Scaevolus
Apr 16, 2007

ShinAli posted:

Got a quick question about pointers in C.

Right now I got a struct with a growing character array which would contain multiple strings. Every time I run out of space (when adding strings), I realloc the struct. This keeps it in one memory block.

I also have a string (or character pointer) array which the elements point to individual strings in the character array of the struct. The obvious problem being is that when the character array struct gets reallocated, the pointers in the string array get outdated.

To update them, I search the outdated string array for the base pointer of the old struct (I just compare all pointers via ptr1 < ptr2 in a loop and which ever is the smallest, I figure is the base pointer). The idea I have is to subtract the old base pointer with every char pointer in the array, then add the base pointer of the realloc'ed struct character array to every element.

I'm not sure if I'm allowed to do that, though. Do I cast the pointers to some other type, do what I need to do, and cast it back to a pointer?
This sounds a lot like premature optimization.

Scaevolus
Apr 16, 2007

pintle posted:

At about iSize at 700, it started to crash. When I debugged, it gave me an "Out of Memory" on the 4th call of that function. I am, however, calling and passing these structs around. Could it be something about the STACK size?

This probably won't fix your problem, but are you remembering to free the arrays before allocating new ones?

Scaevolus
Apr 16, 2007

Avenging Dentist posted:

Especially since


So your teacher is so lazy that he's actually typing more to do the same thing.

He probably got confused by the warnings compilers emit when there's a non-void function without a return statement.

Scaevolus
Apr 16, 2007

Probably to thwart EVIL THIRD-PARTY COMMERCIAL TOOLS.

Scaevolus
Apr 16, 2007

Kreez posted:

%f means float, not double. (use %lf)

Scaevolus
Apr 16, 2007

Kreez posted:

Cool, %lf works great, thanks.

Odd that something basic like that would be left out of even the basic C syntax overview that exists in my textbook.
K&R is way better than your textbook, please read it.

Scaevolus
Apr 16, 2007

You need to implement the assignment operator.

Scaevolus
Apr 16, 2007

Safe and Secure! posted:

Is anyone here familiar with all of these books? C++PP just seems like such a huge book compared to The C Programming Language, but The C++ Programming Language isn't any better in that regard. Is C++ just an incredibly "large" language or what?
Yes, C++ is an incredibly "large" language.

Accelerated C++ is another commonly suggested introductory book, followed by Effective C++.

Scaevolus
Apr 16, 2007

CapnBry posted:

Here is something to test your pedantic C knowledge:
code:
char buffer[10];
size_t x = sizeof(buffer-1);
What does this code do? The intent was to get sizeof(buffer)-1 but I have known C for 20 years and have no idea what you're actually getting the sizeof.
It's doing pointer arithmetic. The type of (buffer - 1) is char*, so it's equivalent to sizeof(char*).

Scaevolus
Apr 16, 2007

Paniolo posted:

I would guess because their use case for protocol buffers involves data transfer via HTTP or other text-based network protocols where the data needs to be encoded in a way that allows it to be represented as a string (i.e. no null bytes.)

http://stackoverflow.com/questions/2037155/stdstring-as-c-byte-array posted:

Google's Protocol buffer uses the C++ standard string class std::string as variable size byte array (see here) similar to Python where the string class is also used as byte array (at least until Python 3.0).

This approach seems to be good:

* It allows fast assignment via assign and fast direct access via data that is not allowed with vector<byte>
* It allows easier memory management and const references, unlike using byte*.
Realistically, it's probably because protocol buffers can encode both ASCII/UTF-8 strings and arbitrary bytes, and you'd have to needlessly duplicate functionality if one were std::string and the other were std::vector.

Scaevolus fucked around with this message at 19:12 on Feb 6, 2011

Scaevolus
Apr 16, 2007

shrughes posted:

Since 2016 doesn't fit in a byte, this takes a longer instruction, and probably a more expensive instruction, than one that writes to %rax+32. It's certainly a different instruction.

Wouldn't they probably be the same size when converted to uOps?

Scaevolus
Apr 16, 2007

Speculating about a test like this without actually reading the assembly is pointless.

Scaevolus
Apr 16, 2007

Otto Skorzeny posted:

So you learned your lesson from the minecraft project :v:

I was running tests, and reading the assembly. :colbert:

Scaevolus
Apr 16, 2007

roomforthetuna posted:

The idea is that a single instruction is functionally treated the same as a squiggly-brackets-enclosed block containing a single instruction. An "if" is basically a single instruction to conditionally execute another block.

ie. "else if" is not a special instruction in any way, it's treated the same as "else while" or "else for" or "else switch".

Why did I not realize this before? :downs:

Scaevolus
Apr 16, 2007

Unless you're reading/writing the matrix to the disk (a lot), you probably don't want to use mmap.

Quadtrees might be useful for implementing sparse grids.

Scaevolus
Apr 16, 2007

OpenGL contexts can only be used in the thread where they were created. If that destructor is doing something like freeing texture bindings from a thread other than where they were initialized, you might be seeing errors.

Scaevolus
Apr 16, 2007

shodanjr_gr posted:

I'm involved in a project that requires targeting Android and x86 Windows. The application would need OpenGL support with shaders and multitouch functionality. Other niceties (like GUI widgets) would also be nice.

Does such a pipe-dream of a framework exist?

MoSync seems to at least be an attempt at what you're describing.

Adbot
ADBOT LOVES YOU

Scaevolus
Apr 16, 2007

pseudorandom name posted:

Nope. volatile is for memory mapped IO, it isn't sufficient for inter-thread communication.
It depends on the architecture. It would work probably work on x86, but you might get unexpected behaviors from pretending something that isn't a barrier is.

Scaevolus fucked around with this message at 11:31 on Feb 15, 2013

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