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
Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

an skeleton posted:

Ahhh, I see. So the dereferencer is like a trick to make it work?

Pronouns are amazing. If by "it" you mean "doing a weird trick to make a float have an integer's byte pattern", then yes.

an skeleton posted:

I'm not certain what giving it the same "byte pattern" means.

Integers and floats have to be stored in some binary format the computer can natively understand. Integers are typically stored in "two's complement little-endian" if you're on an x86 or x86-64 processor, and floats are typically stored in "IEEE 754 Floating Point" format.

The same number would be represented in both two different ways if we look at the raw bits that are stored in memory.

What Volte's code does is make some assumptions about what the raw bits of floating point numbers and integers look like on your system, play around with the bits of the integer you passed in (that's the + 127 and << 21 and stuff), and then tell the compiler "see all these bits I was turning around, and told you they were an integer? They're actually the bits that make up an IEEE 754 Floating Point Number" through some mischievous ways.

Adbot
ADBOT LOVES YOU

an skeleton
Apr 23, 2012

scowls @ u

Suspicious Dish posted:

Pronouns are amazing. If by "it" you mean "doing a weird trick to make a float have an integer's byte pattern", then yes.


Integers and floats have to be stored in some binary format the computer can natively understand. Integers are typically stored in "two's complement little-endian" if you're on an x86 or x86-64 processor, and floats are typically stored in "IEEE 754 Floating Point" format.

The same number would be represented in both two different ways if we look at the raw bits that are stored in memory.

What Volte's code does is make some assumptions about what the raw bits of floating point numbers and integers look like on your system, play around with the bits of the integer you passed in (that's the + 127 and << 21 and stuff), and then tell the compiler "see all these bits I was turning around, and told you they were an integer? They're actually the bits that make up an IEEE 754 Floating Point Number" through some mischievous ways.

Yes by "it" I was saying, that without that first asterisk, would the casting work at all or does it require the dereferencing symbol?

anyway, makes (some) sense, thanks for the explanation.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

The Gripper posted:

If it makes you feel any better I know what pointers, casts and references are and still have no clue what it's supposed to be doing since I don't know the math behind it.

The math itself isn't too hard. The + 127 offsets the exponent bias, and the << 23 is stuffing the value into the exponent field. It's relying on the fact that floating point number is stored in terms of powers of two, so all we do two get a power of two is stuff the power we want into the exponent field.

The Gripper
Sep 14, 2004
i am winner

an skeleton posted:

Yes by "it" I was saying, that without that first asterisk, would the casting work at all or does it require the dereferencing symbol?
The casting would work, but you'd be left with a pointer. The first asterisk dereferences that pointer (gets the data at the location the pointer is pointing at), which in that case is a float.

Suspicious Dish posted:

The math itself isn't too hard. The + 127 offsets the exponent bias, and the << 23 is stuffing the value into the exponent field. It's relying on the fact that floating point number is stored in terms of powers of two, so all we do two get a power of two is stuff the power we want into the exponent field.
I figured as much, I just don't have the knowledge of floats (or exp2, or anything) to know what the intended result was. Lucky for my bosses, I've never been tested on it!

Volte
Oct 4, 2004

woosh woosh

Combat Pretzel posted:

Forgot to mention, the exponents are floating point and can be negative.
Ah, well in that case you can still use exp(x * log(2)). The math library might even have an exp2 function in it depending on the platform.

Sorry for the undefined behaviour. For some reason I thought that setting a union's value through one member and then accessing it with a different member was equally undefined, so I did it the simpler way. My mistake. v:shobon:v

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Volte posted:

For some reason I thought that setting a union's value through one member and then accessing it with a different member was equally undefined, so I did it the simpler way. My mistake. v:shobon:v
C99 explicitly allows it (6.5.2.3p3). C++11 doesn't as far as I'm aware, although it incorporates the C99 memory model by reference, and thus should work too.

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!

Volte posted:

Ah, well in that case you can still use exp(x * log(2)). The math library might even have an exp2 function in it depending on the platform.
Cool thanks. That may come handy, if the bounds of x aren't known.

For the specialized case, the LUT is still faster, tho. Since I know roughly what values I'll be dealing with, this is rather easy to generate.

code:
double pow2_f[1024];

void gen_lut_pow2frac()
{
	for(int i = 0; i < 1024; i++)
	{
		pow2_f[i] = pow(2.0, (double)i / 1024.0) / (double)(2 << 15);
	}
}

double __forceinline fastpow2(double exp)
{
	return (double)(2 << (int)(exp + 15)) * pow2_f[(int)(exp * 1024.0) & 0x3FF];
}
In the benchmark rigged version of my oscillator, the version using the exp-log stuff takes only 61% of the time it'd need with pow, with my fastpow2 however only 34%.

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

floWenoL posted:

This is undefined behavior.

(Use a union.)

Isn't using a union to do type punning also undefined behavior? I thought the only defined way to do it was via char* didn't see previous posts

Illusive Fuck Man
Jul 5, 2004
RIP John McCain feel better xoxo 💋 🙏
Taco Defender
I'm trying to figure out a heap overflow vulnerability in some old software. I can't actually debug it right now because ~hurricanes~

Comparing the old vulnerable source to the patched source led me here:
code:
char * newstr = strcpy( malloc(strlen(str) + 1), str);
Under what circumstances would code like this gently caress things up? No null terminator? I kinda feel like I'm looking in the wrong place, because in the actual program a \0 should have been added before this point. Supposedly the program should crash if I supply a long enough string as input, but I don't know exactly how that could happen from this. The allocator actually uses some kind of resource pool passed as an argument, but I haven't looked into that bit yet.

astr0man
Feb 21, 2007

hollyeo deuroga
I'm not 100% sure about your vulnerability problem, but you should always check the return value of malloc.

edit: Also, where does str come from?

nielsm
Jun 1, 2009



Illusive gently caress Man posted:

code:
char * newstr = strcpy( malloc(strlen(str) + 1), str);

I'm pretty sure that should be equivalent to a strdup() call, except that strdup() will actually check the return value of malloc() before copying the data.

Some other things to consider:
1. Try to use string functions that take a length parameter as much as possible.
2. If the input string has a known length from somewhere (other than the implicit length from the hopefully-there \0 terminator), try to pass that along and use it.
3. If you use a bounded-length string function to copy a string you don't know if is properly terminated, make sure there's room to manually put in a \0 after it.
4. Why are you not using C++ and a safer string class.

Illusive Fuck Man
Jul 5, 2004
RIP John McCain feel better xoxo 💋 🙏
Taco Defender

nielsm posted:

I'm pretty sure that should be equivalent to a strdup() call, except that strdup() will actually check the return value of malloc() before copying the data.

Yeah, it was actually in a macro like #define app_pool_strdup ~thatline~

nielsm posted:

Some other things to consider:

This isn't actually my code. I'm trying to find a specific exploit in old version of an open source app from ~2002.


astr0man posted:

edit: Also, where does str come from?

The program reads a line of input from a remote user, splits it up by inserting a few null characters, then copies parts of it (using that line) into a new struct.

mjau
Aug 8, 2008

Volte posted:

Ah, well in that case you can still use exp(x * log(2)). The math library might even have an exp2 function in it depending on the platform.

The exp2 function should be available anywhere pow/exp/etc are. C99 added float and long double versions (exp2f/exp2l)

Volte
Oct 4, 2004

woosh woosh

mjau posted:

The exp2 function should be available anywhere pow/exp/etc are. C99 added float and long double versions (exp2f/exp2l)
I think exp2 was only added to the standard in C99. I would think it would be available pretty much everywhere, but it's not a guarantee.

chglcu
May 17, 2007

I'm so bored with the USA.

Volte posted:

I think exp2 was only added to the standard in C99. I would think it would be available pretty much everywhere, but it's not a guarantee.

Pretty sure it is C99, and it doesn't seem to be available in Visual Studio, as of 2010 at least. Microsoft doesn't seem to care at all about C99, which can be a bit annoying.

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!
I'm using VS2012 and it's not there either. Well, kind of. Their AMP headers come with it.

mjau
Aug 8, 2008

Volte posted:

I think exp2 was only added to the standard in C99.

Ah, you're right, it's C99. I was so sure it predated that :eng99:

xgalaxy
Jan 27, 2004
i write code

Combat Pretzel posted:

I'm using VS2012 and it's not there either. Well, kind of. Their AMP headers come with it.

That's because MS is dumb and doesn't ship anything newer than C89.

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!
I thought MSVC does C11?

KaneTW
Dec 2, 2011

It does (kinda) do C++11, but that's C++, not C.

The Gnome
Sep 8, 2011

by R. Guyovich
Alright so I have this program that I coded and right now it's using no functions. I need to create a few functions, starting with one called char get_menu_choice() and I'm not sure how I would begin to do that. Any ideas? Thanks!

http://pastebin.com/c9uNwfsz

xgalaxy
Jan 27, 2004
i write code

KaneTW posted:

It does (kinda) do C++11, but that's C++, not C.

Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now.
The thing is though, they don't even support half the features of C++ 11 either in VS2k12.
All their dev time went into their proprietary extensions called C++/CX and C++ AMP.

And, aside from this year with the introduction to in IDE graphics debugging (which, lets be honest, just took PIX and integrated it), there hasn't been any real advancements in the IDE for C++ in 12 years. Most of the effort has been in the .NET realm.

Xcode is a much better development environment for C and C++ developers nowadays.

xgalaxy fucked around with this message at 16:55 on Nov 2, 2012

Combat Pretzel
Jun 23, 2004

No, seriously... what kurds?!

The Gnome posted:

Alright so I have this program that I coded and right now it's using no functions. I need to create a few functions, starting with one called char get_menu_choice() and I'm not sure how I would begin to do that. Any ideas? Thanks!

http://pastebin.com/c9uNwfsz
If I had to guess, such a get_menu_choice() function would just wrap cin >> selection and return the value. If you were to make it some more complex, you'd move the 'parsing' into it and return a well defined value to handle in the main body or pass it to another function.

code:
#define MENU_INVALID 0
#define MENU_ADDFILE 1
#define MENU_SHOWTOTALS 2
#define MENU_SEARCHRESULTS 3
#define MENU_EXIT 4

int get_menu_choice()
{
	char selection;
	cin >> selection;

	switch(selection)
	{
		case 'a':
		case 'A':
			return MENU_ADDFILE;
		case 'p':
		case 'P':
			return MENU_SHOWTOTALS;
		case 's':
		case 'S':
			return MENU_SEARCHRESULTS;
		case 'q':
		case 'Q':
			return MENU_EXIT;
		default:
			return MENU_INVALID;
	}	
}

That Turkey Story
Mar 30, 2003

Announcements coming today (November 2, 2012) at 12:45 PM PST:

Beman Dawes posted:

Herb Sutter, the Convenor of the ISO C++ Standards Committee, will be
making some announcements tomorrow likely to be of interest to C++
developers in general and Boosters in particular. The venue is a talk
titled "The Future of C++" at the Microsoft Build Conference.

While some of Herb's announcements will be specific to Microsoft, the
key C++ initiatives involve Boost and the whole C++ community. Some of
the ideas evolved from feedback Herb got at C++Now/BoostCon 2012 last
May.

I'll post a summary of the announcements tomorrow, but you can also
watch the live stream at http://channel9.msdn.com at 12:45 PM Pacific
time. For other time zones, see
http://www.timeanddate.com/worldclock/fixedtime.html?iso=20121102T1945

--Beman

PS: We don't usually post teasers like this on the Boost lists, but
Herb asked that a notice be posted, and given the endless work he has
put in on this over the summer, I couldn't say no.

Edit:

C++ Standard stuff:
Standards meeting attendence has increased. Standardization process now includes SGs for concurrency, modules, filesystem, networking, transactional memory, numerics, reflection, concepts, ranges, feature test, and databases.

Next C++ standard in 2014 (minor). Expecting to include polymorphic lambdas (lambdas that are templates), return type deduction from normal functions, a C++ version of VLAs, static if, and preliminary concepts support. Standard library literal support, including support for binary literals (no more need to hate on me for BOOST_BINARY :p), reader-writes locks, thread-safety for streams, possibly concurrent queue and hashtables.

Next major C++ standard is targeted for 2017. TS (formerly knows as TR) based on Boost.Filesystem coming in 2013. Similarly, a networking specification is targeted for 2013, expecting revisions each year. Transactional memory TS in 2014.

New website to keep people up-to-date with C++ standardization: http://www.isocpp.org

Standard C++ Foundation -- a non-profit not owned by any company, founded and funded by over a dozen companies including Microsoft, Google, Intel, etc. aimed in promoting understanding and use of C++.

Some MS-specific stuff:
VC++ 2012 CTP, downloadable now, supports explicit conversion operators, raw string literals, delegating constructors, function template default arguments, uniform initialization, and variadic templates (finally).

That Turkey Story fucked around with this message at 21:38 on Nov 2, 2012

The Gnome
Sep 8, 2011

by R. Guyovich

Combat Pretzel posted:

If I had to guess, such a get_menu_choice() function would just wrap cin >> selection and return the value. If you were to make it some more complex, you'd move the 'parsing' into it and return a well defined value to handle in the main body or pass it to another function.

code:
#define MENU_INVALID 0
#define MENU_ADDFILE 1
#define MENU_SHOWTOTALS 2
#define MENU_SEARCHRESULTS 3
#define MENU_EXIT 4

int get_menu_choice()
{
	char selection;
	cin >> selection;

	switch(selection)
	{
		case 'a':
		case 'A':
			return MENU_ADDFILE;
		case 'p':
		case 'P':
			return MENU_SHOWTOTALS;
		case 's':
		case 'S':
			return MENU_SEARCHRESULTS;
		case 'q':
		case 'Q':
			return MENU_EXIT;
		default:
			return MENU_INVALID;
	}	
}

http://pastebin.com/c9uNwfsz

Here is an update. As you guys can see, I've used functions correctly, however, I'd like to be able to use these functions without having the variables declared globally and would rather have them use inputs and such. Any help?

Thanks!

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

xgalaxy posted:

Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now.
The thing is though, they don't even support half the features of C++ 11 either in VS2k12.
All their dev time went into their proprietary extensions called C++/CX and C++ AMP.

And, aside from this year with the introduction to in IDE graphics debugging (which, lets be honest, just took PIX and integrated it), there hasn't been any real advancements in the IDE for C++ in 12 years. Most of the effort has been in the .NET realm.

Xcode is a much better development environment for C and C++ developers nowadays.

There is literally only one developer maintaining the STL in VS 2012 right now.

MrBadidea
Apr 1, 2009

xgalaxy posted:

Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now.
The thing is though, they don't even support half the features of C++ 11 either in VS2k12.
All their dev time went into their proprietary extensions called C++/CX and C++ AMP.

And, aside from this year with the introduction to in IDE graphics debugging (which, lets be honest, just took PIX and integrated it), there hasn't been any real advancements in the IDE for C++ in 12 years. Most of the effort has been in the .NET realm.

Xcode is a much better development environment for C and C++ developers nowadays.

There's a few more parts rammed into VS2012 for graphics work. Yes, the GPUDebugging is basically Pix but integrated, it feels a lot easier to work with and far less of a pain in the rear end.

The part I'm most confused about though, is the addition of a model viewer/editor. I mean, I can kind of see some minor benefits of having something lightweight to check your model assets. But having it built into your code IDE seems a bit off, especially when you consider that it's supported formats are Obj, SDKMesh (urgh) and FBX which has about a million model/scene viewers around already.

nielsm
Jun 1, 2009



That Turkey Story posted:

Announcements coming today (November 2, 2012) at 12:45 PM PST:

Thanks for the heads-up, watching it now. Quite interesting.

awesmoe
Nov 30, 2005

Pillbug

That Turkey Story posted:

C++ Standard stuff:

Thanks, you just saved me 90 minutes. Some of that stuff's pretty cool.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde

Combat Pretzel posted:

I thought MSVC does C11?
It hasn't implemented C99, much less C11. There's not much paying demand for new C features on the Windows platform and Microsoft won't do them just to placate standard spergs.

djbrakke
Oct 18, 2012
Taking an online course on compsci and am learning basic C programming.
Having trouble on an assignment that has us take user input to create a half pyramid.
It's suppose to look like a staircase going left to right (opposite of what's shown)...user just inputs height.
##
###
####
#####
etc....

Below is my code thus far...the user input goes in correctly but nothing is drawn. What am I doing wrong? There is apparently suppose to be 3 loops used in this exercise. Any help is appreciated.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main (void)
{
// User inputs pyramid height using 1-23. Other numbers prompt retry.
int height;
do
{
printf ("Enter pyramid height Mario! (1-23): ");
height = GetInt();
}
while (height < 1 || height > 23);

// height transfers into next loop where it's calculated to create the pyramid.
int space = 0;
int hash = 0;
while (height > 0);
{
(space = (height - 1), hash = ((height + 1) - space));
printf("%d ", space);
printf("%d##", hash);
printf("\n");
height--;
}
return 0;
}

Duke of Straylight
Oct 22, 2008

by Y Kant Ozma Post
while (height > 0);

This line means "as long as height > 0, keep checking whether height > 0". It is probably helpful to start using compiler warning messages from early on because I'm pretty sure most compilers catch this. And don't worry about it, everyone has problems with terminating statements with semicolons at first (unless they use a language which doesn't require it in the first place). In this case, remove the semicolon to make it actually run the following block.

djbrakke
Oct 18, 2012
Thank you, that helped partially!
More updates to come.

raminasi
Jan 25, 2005

a last drink with no ice
When you post code post it in code tags.

Karl Sharks
Feb 20, 2008

The Immortal Science of Sharksism-Fininism

Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help.

That Turkey Story
Mar 30, 2003

Karl Sharks posted:

Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help.

There are a ton, but the current flavor of the month tends to be Eigen, although it is far from "simple" C++ code as it uses a lot of advanced, modern techniques.

BirdOfPlay
Feb 19, 2012

THUNDERDOME LOSER

Karl Sharks posted:

Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help.

Also, you could build your own little functions for those basic matrix operations that take 2D array arguments. Are you doing anything beyond simple add, multiply, scalar multiply? Depending on the number of operations you need and how generalized you make the functions (that is to say, whether you're writing them for general cases or the hyper-specific cases you know you'll be working with), you could whip them up yourself and just continue on your way.

Granted, I half suspect that your asking the thread cause it's due in a day or two and have over stepped your bounds a little, but if you have the time, this'd be a nice little side project for you to build up your own linear algebra library and will be a boost to your C++ (and general) programming skills.

Suran37
Feb 28, 2009
I am working on making my own shell for one of my cs classes. Anyway I have to make a process run in the back if the user adds '&' at the end of the command.
I simply check if one of the args was an '&', if so I set runBG to equal 1. Then I fork the child and run normally, and if runBG equals 1, I do a waitpid(-1, &status, WNOHANG). Otherwise, I do a waitpid(-1, &status, 0).

It seems to work fine except if I try to run commands after a background process finishes the output gets messed up.

Normal:


After I tell a command to run in the background:


Any help on what I'm missing would be great.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
It looks like you're not setting that flag back to zero for the next command.

Adbot
ADBOT LOVES YOU

Suran37
Feb 28, 2009

Jabor posted:

It looks like you're not setting that flag back to zero for the next command.

I do set it to zero after the execution of the command. If it's easier to see my terrible code: http://pastebin.com/pfcpJBbF

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