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
Brecht
Nov 7, 2009

Avenging Dentist posted:

2) he's already said this is just for debugging, which is a totally legit reason to do something like this.
He said that after my post. I agree, for debugging, any ol' hack will work.

Adbot
ADBOT LOVES YOU

Fedaykin
Mar 24, 2004

Avenging Dentist posted:

From your link: http://www.codeproject.com/KB/cs/Mandelbrot_set.aspx#premain1 (not how I'd do it)

Ok I sort of got it working now. places on the perimeter are colored but most all the exterior is black. Is it the equation for mu or is it the way it assigns color values?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Fedaykin posted:

Ok I sort of got it working now. places on the perimeter are colored but most all the exterior is black. Is it the equation for mu or is it the way it assigns color values?

Look at the screenshot in the article. It's the color gradient. An easy way to get different colors would be to use HSL and then convert it to RGB.

sklnd
Nov 26, 2007

NOT A TRACTOR

defmacro posted:

EDIT: I guess to clarify: I want a kernel-space interface to the iptables rules in the kernel. This interface should create rules that can ONLY be removed by the kernel-space interface.

You could write a kernel module using netfilter.h that did some iptables-ish processing of packets. Other than that, I don't know of a way to do what you want.

raminasi
Jan 25, 2005

a last drink with no ice
I'm working on a project that involves, among other things, reading and comparing a bunch of floating point numbers from some files. This presents some obvious difficulties, especially because the input format specification implicitly rejects the transitivity of equality of floating point numbers.

I'm not sure of the best strategy for dealing with this. Discarding float-point equality transitivity sounds like a terrible idea, but maybe I'm thinking about it the wrong way. I had the idea of doing fp comparisons using an epsilon while also keeping track of every fp number I read and, if a transitivity violation is detected, declaring the entire conversion (the program converts files) undefined and bailing, but I don't really know what would work best. I'm sure this isn't a novel problem but I don't know where to look for solutions.

litghost
May 26, 2004
Builder

GrumpyDoctor posted:

I'm working on a project that involves, among other things, reading and comparing a bunch of floating point numbers from some files. This presents some obvious difficulties, especially because the input format specification implicitly rejects the transitivity of equality of floating point numbers.

I'm not sure of the best strategy for dealing with this. Discarding float-point equality transitivity sounds like a terrible idea, but maybe I'm thinking about it the wrong way. I had the idea of doing fp comparisons using an epsilon while also keeping track of every fp number I read and, if a transitivity violation is detected, declaring the entire conversion (the program converts files) undefined and bailing, but I don't really know what would work best. I'm sure this isn't a novel problem but I don't know where to look for solutions.

Can you use fixed point in this situation?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

GrumpyDoctor posted:

I'm working on a project that involves, among other things, reading and comparing a bunch of floating point numbers from some files. This presents some obvious difficulties, especially because the input format specification implicitly rejects the transitivity of equality of floating point numbers.

Is this right after it denies the fundamental theorem of algebra, but before it gets into its discussion of the continuum hypothesis?

EDIT: No, I guess those would be explicit rejections.

OBLIGATORY EDIT 2: Which I assume you would know something about.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Anyone have a link to the 'Cantor did wtc' screed

6174
Dec 4, 2004

rjmccall posted:

Is this right after it denies the fundamental theorem of algebra, but before it gets into its discussion of the continuum hypothesis?

For what it is worth, the relation GrumpyDoctor described (two floating point numbers are related if and only if they are within some epsilon of each other) is not an equivalence relation because that relation is not transitive.

GrumptyDoctor, I'd have to know more about what you are doing to suggest a solution, but it is possible (likely for an appropriate epsilon) that your data set won't violate transitivity. I'd start by writing a script that checks for such violations (sort your data then check). Only if you found violations would I worry about it.

raminasi
Jan 25, 2005

a last drink with no ice

litghost posted:

Can you use fixed point in this situation?

It wouldn't solve my problem - even if I can trust my own representations of the data, I can't trust their input representations.

6174 posted:

For what it is worth, the relation GrumpyDoctor described (two floating point numbers are related if and only if they are within some epsilon of each other) is not an equivalence relation because that relation is not transitive.

GrumptyDoctor, I'd have to know more about what you are doing to suggest a solution, but it is possible (likely for an appropriate epsilon) that your data set won't violate transitivity. I'd start by writing a script that checks for such violations (sort your data then check). Only if you found violations would I worry about it.

This is basically what I was thinking, so it's probably what I'll go with. If I find a violation I can bitch at the upstream guy. Internally I was thinking about using Boost::Rational - anyone have any thoughts?

Jose Cuervo
Aug 25, 2004
A friend of mine has written a bunch of code in C++ which takes about an hour to run each time he runs it. Unfortunately it is not working correctly. He knows that the problems are occurring towards the end of the run and would like to start debugging at that point. Is there any way for him to save the status of the code about 50 minutes in and start debugging from that point each time? Hopefully my question makes sense.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Jose Cuervo posted:

A friend of mine has written a bunch of code in C++ which takes about an hour to run each time he runs it. Unfortunately it is not working correctly. He knows that the problems are occurring towards the end of the run and would like to start debugging at that point. Is there any way for him to save the status of the code about 50 minutes in and start debugging from that point each time? Hopefully my question makes sense.

He can generate a core dump at that point in the program, and tell his debugger to launch that instead of the executeable itself, see gdb(1) gcore(1) core(5). If the program does file IO or network IO after that point on handles that were opened prior to that point, you may need to build a wrapper to get its filehandles setup as it expects, but that's an edge case.

Dijkstracula
Mar 18, 2003

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

You may also find gdb 7's support for reverse debugging useful, though I've not tried it yet so I don't know how useful it really is.

nightchild12
Jan 8, 2005
hi i'm sexy

I am writing a program in C which needs to translate network values (big-endian for every input to this program) to host values (little-endian on my machine) in a number of places. I use ntohl and ntohs for doing so. Unfortunately, in one place I have to take in a big-endian value which is 3 bytes long and turn it into an integer in the host's endianness. I can't find any library functions which do this, so I've written my own (below).

This function gets passed a 3-byte long character array, puts them in the proper order in an integer and returns that value. The return value should be padded with 0x00 in the most significant byte.

I know this works on little-endian machines, as I've tested it. I was wondering if this code will work on big-endian machines as well, but I have none available to me to test it on.

code:
int ntoh24bit(char *input)
{
 int ordered = 0;
 char *pointer;
 pointer = (char *)&ordered;

 if( fLittleEndian )
 {
  pointer[0] = input[2];
  pointer[1] = input[1];
  pointer[2] = input[0];
 }
 else
 {
  pointer[1] = input[0];
  pointer[2] = input[1];
  pointer[3] = input[2];
 }

 return ordered;
}
So - will this work on Big-Endian machines? I think gdb is screwing me up, since it appears to display any given 4-byte value with the highest memory address at the left and lowest at the right, which is useful for being able to read it but is killing my ability to visualise what is happening in memory.

Also, is there a better way to do this?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

nightchild12 posted:

Also, is there a better way to do this?

It's somewhat clearer if you abuse aliasing. This is arguably unsafe, but I can't think of any compilers that won't do the right thing.

code:
uint32_t ntoh24( const char *input ) {
  char padded[4];
  padded[0] = 0;
  memcpy( padded + 1, input, 3 );
  return ntohl( *(uint32_t *)padded );
}

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

nightchild12 posted:

Also, is there a better way to do this?

code:
uint32_t ntoh24(const uint8_t *input) {
  return (input[0] << 16) + (input[1] << 8) + input[2];
}
Works as long as sizeof(int) >= sizeof(uint32_t); otherwise you need some more casts.

Commander Keen
Oct 6, 2003

The doomed voyage of the Obsessivo-Compulsivo will haunt me forever
For those not following the blog, Clang now self-hosts.

http://blog.llvm.org/2010/02/clang-successfully-self-hosts.html

sonic bed head
Dec 18, 2003

this is naturual, baby!
What happens when an int pointer is cast to an int? I am trying to understand this bit of code. Before I saw this, I thought that an int pointer was just an int that happened to be a memory address, but I am finding that this is not the case.

code:

int i;
int j[100];
int *p, *q;
j[50] = 50;
j[66] = 66;

p = &(j[50]);       
q = p+16;            cout << *q << "\n";
i = ((int) p) + 16;
q = (int *) i;       cout << *q << "\n";

In this example, I was expecting the two printed dereferenced memory addresses to have the same value. I would like to know what is happening here. I now know that a int* and an int are very different, but how is an int* actually stored? I thought it was just a regular integer that you could cast back to int and add to. Thanks for your help.

digibawb
Dec 15, 2004
got moo?
If you add to a pointer, you offset it by (a multiple of) the size of the type it points to, not by the value you added. So q + 16 will be a memory address 16 * sizeof( int ) bytes further along, not 16 bytes further along. When you shove the pointer into an int, and add to the int instead, all you are doing is increasing the value of the int.

(Ignoring the fact that shoving a pointer into an int isn't the best thing in the world to be doing.)

digibawb fucked around with this message at 18:35 on Feb 6, 2010

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Pointer arithmetic is done in increments of the size of the type the pointer points to, cf. this slight modification to your code http://codepad.org/SvWHBqWY

Vanadium
Jan 8, 2005

use intptr_t yo

sonic bed head
Dec 18, 2003

this is naturual, baby!
Thanks! That's awesome. I had no idea that's how that worked. I guess this is why operator overloading is confusing? :)

edit: By the way, how would I have found that out if it weren't for the forums? I was trying to find the int* documentation somewhere online to find out what the + operator does, but I had no luck. I am usually a java person so there's a javadoc for everything, is there a resource like that for C++?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
man(1) covers the C standard library and POSIX, although for very fundamental poo poo like this you probably want to bang through K&R and Koenig&Moo

The_Franz
Aug 8, 2003

Vanadium posted:

use intptr_t yo

Yeah, that code as-is could do weird things on a 64-bit system since you would be shoving a 64-bit pointer into a 32-bit integer.

6174
Dec 4, 2004
I've got a program that needs to use the equivalent of ntohl, but for 64 bit. I only have a little-endian machine to test on at the moment, so can someone verify for me this works correctly on a big-endian machine?

code:
    union {
        uint64_t ui64;
        uint32_t ui32[2];
    } val;
    if (htons(1) == 1)
    {
        // Network order / Big-endian
        val.ui32[0] = ntohl(*((uint32_t *) storage));
        val.ui32[1] = ntohl(*((uint32_t *) (storage + 4)));
    }
    else {
        // Little-endian
        val.ui32[1] = ntohl(*((uint32_t *) storage));
        val.ui32[0] = ntohl(*((uint32_t *) (storage + 4)));
    }
storage is a uint8_t pointer pointing to the first byte of a uint64_t in network order. I know about be64toh in endian.h, but this program (for school) will be tested on a machine that almost certainly doesn't have it (and I can't check).

functional
Feb 12, 2008

I've been playing around with this for a while. How do I get getaddrinfo to give me the IP-address, or rather, the IPv4-address as a valid 32-bit integer? I could use gethostbyname, but the manual recommends avoiding it.

functional fucked around with this message at 05:11 on Feb 8, 2010

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender
getaddrinfo is for being agnostic as to IPv4 vs IPv6. If you specifically want IPv4, just use gethostbyname.

But if you really want to use getaddrinfo, just pull the address-as-a-number out of the sockaddr_in that you get from an IPv4 result, read ip(7) for documentation of that structure.

functional
Feb 12, 2008

ShoulderDaemon posted:

getaddrinfo is for being agnostic as to IPv4 vs IPv6. If you specifically want IPv4, just use gethostbyname.

But if you really want to use getaddrinfo, just pull the address-as-a-number out of the sockaddr_in that you get from an IPv4 result, read ip(7) for documentation of that structure.

Got it. The magic trick is learning that the sockaddr you get back can be cast to a sockaddr_in if you know it's IPv4. Also, I needed ntohl.

functional fucked around with this message at 04:33 on Feb 8, 2010

pseudorandom name
May 6, 2007

functional posted:

Got it. The magic trick is learning that the sockaddr you get back can be cast to a sockaddr_in if you know it's IPv4. Also, I needed ntohl.

And you can determine what type of sockaddr you have by looking at the sa_family field.

Contero
Mar 28, 2004

At what value do (single) floats lose integer precision?

pseudorandom name
May 6, 2007

Contero posted:

At what value do (single) floats lose integer precision?

For IEEE floats, any integer with absolute value less than or equal to 2significand can be represented exactly.

So, for 32-bit floats with a 24-bit significand, 224 = 16777216.

lol 10 bux
Dec 22, 2004
i <3 (o)(o)s
Is it possible for read/write to a regular file to ever successfully return less than the bytes requested, barring possibly being interrupted by a signal, if I know the size of the file?

e.g.
code:
int fd = open("foo", O_RDWR);
struct stat st;
fstat(fd, &st);
unsigned char *buf = malloc(st.st_size);
int rd = read(fd, buf, st.st_size);
lseek(fd, 0, SEEK_SET);
int wr = write(fd, buf, st.st_size);
Is it possible for rd or wr to be other than st.st_size, using basic assumptions like I have read and write access and the file exists? I'm trying to determine if I have to keep a loop everywhere I want to do a simple read/write to make sure the operation fully completes.

MrMoo
Sep 14, 2000

lol 10 bux posted:

Is it possible for read/write to a regular file to ever successfully return less than the bytes requested, barring possibly being interrupted by a signal, if I know the size of the file?

It could be truncated by another process, maybe NFS errors could cut the read short too.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

lol 10 bux posted:

I'm trying to determine if I have to keep a loop everywhere I want to do a simple read/write to make sure the operation fully completes.

In general, there are a wide variety of error conditions that can cause a partial read, and you cannot really predict or prevent them. However, they are exceptional, and essentially none of them* can be corrected by repeating the read in a loop. So you should just check the return of read, and if it isn't correct, don't bother retrying the read - just abort with an error, because retrying won't be any better.

*Exactly one error condition from read - EAGAIN - will be potentially solved by retrying the read. But EAGAIN will only come up if you specifically ask for it by entering nonblocking mode, and even then you shouldn't stick a loop around your read because you really need to do something like a select instead.

pseudorandom name
May 6, 2007

Signals won't result in truncated disk I/O either.

Pooball
Sep 21, 2005
Warm and squishy.

pseudorandom name posted:

Signals won't result in truncated disk I/O either.

Yes they can (in POSIX).

functional
Feb 12, 2008

Can I do an mmap write to stdout? If so, how? Passing 1 as a file descriptor gives me an error.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

functional posted:

Can I do an mmap write to stdout? If so, how? Passing 1 as a file descriptor gives me an error.

If you redirect stdout to a file, and your shell does an arguably weird thing by opening that file in read/write mode instead of write-only mode, then you will be able to mmap stdout.

In general, you can only mmap files or sufficiently file-like (must be random-seekable and have a backing store) descriptors) descriptors, and you must have read permission on the descriptor.

Edit: What are you trying to do? There's probably a better way. Also, although I doubt it matters in this case, you should always specify the error you get instead of "an error", which is possibly the least helpful thing you can say. Posting the actual code you're trying to use is also generally a good idea.

Whilst farting I
Apr 25, 2006

I have a question involving OpenGL.

So let's say that I want to draw a line segment, but instead of just simply drawing the line segment, the segment is "drawn" using really big 5x5 pixels. How would I go from (10,10) to (20,20) with only two 5x5 pixels drawn instead of a bunch of normal pixels, using Bresenham's linescan conversion algorithm? Complicating things even more is trying to draw from the center of each tiny portion of where the 5x5 pixel is supposed to be.

Drawing the line segment normally gives me no trouble whatsoever, but to do this confuses me.

code:
glBegin(GL_LINES);
		glVertex2i(10,10);	
		glVertex2i(20,20);
glEnd();

Adbot
ADBOT LOVES YOU

newsomnuke
Feb 25, 2007

Whilst farting I posted:

How would I go from (10,10) to (20,20) with only two 5x5 pixels drawn instead of a bunch of normal pixels, using Bresenham's linescan conversion algorithm? Complicating things even more is trying to draw from the center of each tiny portion of where the 5x5 pixel is supposed to be.
If you're manually calculating the points using Bresenham, just draw the "pixels" as quads?
code:
float scale = 5;
float halfScale = scale / 2;

glBegin(GL_QUADS);
while (generating_points)
{
	int px = getPointX() * scale;
	int py = getPointY() * scale;

	glVertex2f(px - halfScale, py - halfScale);
	glVertex2f(px + halfScale, py - halfScale); 
	glVertex2f(px + halfScale, py + halfScale);
	glVertex2f(px - halfScale, py + halfScale);
}
glEnd();

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