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
OzyMandrill
Aug 12, 2013

Look upon my words
and despair

Argh - yeah, I figured it would be easier talking as though they were int positions ;)
Anyway in the first case, simplest would be just wait until randoms has 1000 entries, like:

code:
while( randoms.size() < 1000 )
    randoms.push_back( rand()%1000 );
:)

I am also assuming the OP is a beginner trying to get their head around how the lists work, not actually coding anything useful, so I was aiming for simplicity (& missed?)

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

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

rhag posted:

Has anyone ever used libunrar (from https://github.com/vilkov/libunrar)?

You're not allowed to.

pre:
   Unrar source may be used in any software to handle RAR archives
   without limitations free of charge, but cannot be used to re-create
   the RAR compression algorithm, which is proprietary. Distribution
   of modified Unrar source in separate form or as a part of other
   software is permitted, provided that it is clearly stated in
   the documentation and source comments that the code may not be used
   to develop a RAR (WinRAR) compatible archiver.
If you develop a program that isn't the unrar binary that also unpacks RAR archives, that's against the license of the code. It's not open-source. You need to fork out to the unrar binary.

Jewel
May 2, 2009

Suspicious Dish posted:

You're not allowed to.

pre:
   Unrar source may be used in any software to handle RAR archives
   without limitations free of charge, but cannot be used to re-create
   the RAR compression algorithm, which is proprietary. Distribution
   of modified Unrar source in separate form or as a part of other
   software is permitted, provided that it is clearly stated in
   the documentation and source comments that the code may not be used
   to develop a RAR (WinRAR) compatible archiver.
If you develop a program that isn't the unrar binary that also unpacks RAR archives, that's against the license of the code. It's not open-source. You need to fork out to the unrar binary.

Hm? Doesn't that say you can't use it to create RAR's, but it's okay to use it to uncompress them? It's a little confusing.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Oh, yeah, that's confusing. I'm assuming that an "archiver" tool is one that uncompresses RARs, but I'm not sure.

nielsm
Jun 1, 2009



Suspicious Dish posted:

Oh, yeah, that's confusing. I'm assuming that an "archiver" tool is one that uncompresses RARs, but I'm not sure.

Nah, in traditional terms the archiver creates archives, the unarchiver unpacks.

From a business point of view, the idea of libunrar is to let anyone integrate support for reading and unpacking RAR files into their software, in the hope that having wide support for the format creates more demand for selling WinRAR to create the files.

Volguus
Mar 3, 2009
There are a lot of open source programs out there (7z for example) that can and do uncompress rar files. Won't create them, but can extract them. I'll go study them to see how do they do it, but I was kinda hoping for some obvious mistake that I'm doing that will let me extract multi volume archives without too much fuss. The unrar binary is quite convoluted.

Suspicious Dish
Sep 24, 2011

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

rhag posted:

There are a lot of open source programs out there (7z for example) that can and do uncompress rar files.

The last time I checked, 7z required that you have the external unrar program on your machine (it came bundled with it on Windows), but since then it seems it's got a native implementation of it. Possibly clean room engineered? It doesn't seem to use any unrar code though.

hooah
Feb 6, 2006
WTF?

OzyMandrill posted:

Argh - yeah, I figured it would be easier talking as though they were int positions ;)
Anyway in the first case, simplest would be just wait until randoms has 1000 entries, like:

code:
while( randoms.size() < 1000 )
    randoms.push_back( rand()%1000 );
:)

I am also assuming the OP is a beginner trying to get their head around how the lists work, not actually coding anything useful, so I was aiming for simplicity (& missed?)

Your assumption is correct. I changed my first for loop to use your while loop, but now the code just spits out a bunch of 0s.

Hughlander
May 11, 2005

Can anyone explain what the extension or the proposed (and later removed I suppose?) C++0x operator typeof does? For the latter what it was replaced with in C++11 and why? I was working on Google's breakpad compiling it with Clang 3.4 and it puked over a typeof specifier. And I admit it's a corner of C++ I hadn't touched yet.

Posting Principle
Dec 10, 2011

by Ralp
typeof is a nonstandard extension. Are you looking for typeid or decltype?

Posting Principle fucked around with this message at 14:32 on Aug 13, 2013

Hughlander
May 11, 2005

Posting Principle posted:

typeof is a nonstandard extension. Are you looking for typeid or decltype?

No, I'm talking about typeof, hence calling it an extension in the first part of the sentence. But in double checking the pedantry I came across http://lifecs.likai.org/2009/11/emulating-c0x-auto-keyword-in-c.html that answered the question. I wonder now why I couldn't find that page yesterday.

more like dICK
Feb 15, 2010

This is inevitable.

Hughlander posted:

Can anyone explain what the extension or the proposed (and later removed I suppose?) C++0x operator typeof does? For the latter what it was replaced with in C++11 and why? I was working on Google's breakpad compiling it with Clang 3.4 and it puked over a typeof specifier. And I admit it's a corner of C++ I hadn't touched yet.

decltype is the C++11 replacement. IIRC the name was chosen so that it didn't conflict with the existing extensions. If you're using C++11, you should probably prefer decltype to typeof.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
The name was changed to decltype because the semantics ended up being different than typeof, so just blindly replacing typeof with decltype may not work out if references are involved.

hooah
Feb 6, 2006
WTF?
Ok, I figured out the last problem, but now I'm having trouble using ifstream to read strings from a file. I have a file, sentence.dat, that contains "the quick brown fox jumps over the lazy dog". The eventual goal (as before, I'm a beginner just learning how to use stuff) is to turn that file into a list of strings, then remove any instance of a given string. Here's what I have so far, but MSVC 2010 is throwing an error at the >> operator:
code:
#include <iostream>
#include <list>
#include <stdlib.h>
#include <fstream>

using namespace std;

int main()
{
	list<string> sentence;
	list<string>::iterator i;
	string word;

	ifstream inFile("sentence.dat");

	if(! inFile)
		cout << "Error opening file 'sentence.dat'" << endl;
	while(inFile >> word)
		*i=word;
	for(i=sentence.begin(); i!=sentence.end(); i++)
		cout << (*i) << " ";
	cout << endl;
	return 0;
}

nielsm
Jun 1, 2009



hooah posted:

Ok, I figured out the last problem, but now I'm having trouble using ifstream to read strings from a file. I have a file, sentence.dat, that contains "the quick brown fox jumps over the lazy dog". The eventual goal (as before, I'm a beginner just learning how to use stuff) is to turn that file into a list of strings, then remove any instance of a given string. Here's what I have so far, but MSVC 2010 is throwing an error at the >> operator:

You haven't assigned anything to i at the point you are reading in strings, so you are effectively trying to put the contents of word into an unknown place.

When you want to continually add items to a container like that you should generally use the container.push_back(element) function rather than iterators. (There is also something called a back-inserter iterator, but you probably shouldn't worry about those just yet.)

Something like this:
C++ code:
    while (inFile >> word)
        sentence.push_back(word);
Also, figure out how to enable warnings for your compiler. In this case it would warn you about using i before assignment (usage of uninitialized variable), which would be a strong hint about the mistake.

hooah
Feb 6, 2006
WTF?
Warnings were already enabled, but I didn't notice one for that (which may just be because the error that MSVC throws is so verbose I didn't see a warning). I changed to using push_back, but still get an error on the "while" line. The error says that "no operator is found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion".

tractor fanatic
Sep 9, 2005

Pillbug
I thought the idea behind decltype was because typeof might be ambiguous in meaning static or dynamic type.

Strabo
Feb 25, 2011
Is there any reason for doing something like this?
code:
union {
	long last_address;            /**< the last image address for "reload" function used*/
	long mark_image_address;      /**< the mark image address for "ASCII" function used*/
	long slideshow_image_address; /**< the slideshow image address */
	long custom_image_address;    /**< the custom image address */
} extend_address;

OzyMandrill
Aug 12, 2013

Look upon my words
and despair

hooah posted:

Warnings were already enabled, but I didn't notice one for that (which may just be because the error that MSVC throws is so verbose I didn't see a warning). I changed to using push_back, but still get an error on the "while" line. The error says that "no operator is found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion".

I think the problem is because you are trying to do:
code:
while( std::ifstream& )
as the >> operator returns the reference to a stream (the original stream I think), so you can tag on another >>
However, there is no acceptable conversion from an object reference to a bool which is what the while() needs.

I'm not very good at stream stuff myself, but I think it would need something like this:
code:
while( (inFile >> word).goodbit )
I'm pretty sure that is syntactically wrong, but I think you need to be checking the state of the stream, not just testing the return value of the >> operator.

The Gripper
Sep 14, 2004
i am winner

OzyMandrill posted:

I think the problem is because you are trying to do:
code:
while( std::ifstream& )
It's actually much simpler, he forgot to #include <string>.

mobby_6kl
Aug 9, 2009

by Fluffdaddy

Strabo posted:

Is there any reason for doing something like this?
code:
union {
	long last_address;            /**< the last image address for "reload" function used*/
	long mark_image_address;      /**< the mark image address for "ASCII" function used*/
	long slideshow_image_address; /**< the slideshow image address */
	long custom_image_address;    /**< the custom image address */
} extend_address;

This seems to a avoid all the use cases of unions since the variables are all of the same type, and you'd need a separate indicator to tell you what it stores anyway. Maybe it's some sort of future-proofing, in case they decide to also store another data type in there?

Strabo
Feb 25, 2011

mobby_6kl posted:

This seems to a avoid all the use cases of unions since the variables are all of the same type, and you'd need a separate indicator to tell you what it stores anyway. Maybe it's some sort of future-proofing, in case they decide to also store another data type in there?
Yeah, that's the only reason I can think of, why somebody would want to do that. I was contemplating posting this in the coding horrors thread, because it's pretty confusing to anyone trying yo understand the code and the rest of the code that this came with was kinda horrible as well.
But then I thought that this may be some super advanced C trick that I don't know about yet :v:

hooah
Feb 6, 2006
WTF?

The Gripper posted:

It's actually much simpler, he forgot to #include <string>.

Yeah, this was the problem. That's what I get for taking a month or so break right after my very first programming class and then trying to pick it back up before fall classes start.

Edit:
In doing some poking, it turns out that MSVS 2010, which I got for free from the school I took my first class from, won't be fully supporting C++ 11. In my hunt, I came across Eclipse, but there's more than one C/C++ version - just Eclipse and Eclipse Indigo. Would either of these be a good choice for something that's (relatively) easy to switch to from MSVS and also fully supports the new standard? If not, what would be another suggestion?

hooah fucked around with this message at 22:05 on Aug 15, 2013

raminasi
Jan 25, 2005

a last drink with no ice

hooah posted:

Yeah, this was the problem. That's what I get for taking a month or so break right after my very first programming class and then trying to pick it back up before fall classes start.

Edit:
In doing some poking, it turns out that MSVS 2010, which I got for free from the school I took my first class from, won't be fully supporting C++ 11. In my hunt, I came across Eclipse, but there's more than one C/C++ version - just Eclipse and Eclipse Indigo. Would either of these be a good choice for something that's (relatively) easy to switch to from MSVS and also fully supports the new standard? If not, what would be another suggestion?

Using anything other than Visual Studio for Windows C++ development is going to be some degree of headache or another, and you should only do it if the headache of using Visual Studio is greater. "It doesn't support all of C++11" isn't a headache unless you anticipate needing some specific feature of C++11 that VS2010 doesn't support. Is that the case?

Jewel
May 2, 2009

GrumpyDoctor posted:

Using anything other than Visual Studio for Windows C++ development is going to be some degree of headache or another, and you should only do it if the headache of using Visual Studio is greater. "It doesn't support all of C++11" isn't a headache unless you anticipate needing some specific feature of C++11 that VS2010 doesn't support. Is that the case?

I ran into a slight annoyance where VS2010 doesn't have C++11's Delegated Constructors I think. I'm not completely sure so don't quote me on this :shobon:

For those not familiar, it's this syntax:

C++ code:
Foo::Foo(float a, float b)
{
	SetUpStuff(a, b);
	DoThingWith(a);
}

Foo::Foo(Vector2 v)
	: Foo(v.x, v.y)
{
}
It helps sometimes when you don't want to have to make a separate Init function that each constructor calls.

hooah
Feb 6, 2006
WTF?
I don't foresee any need for anything specific in C++11, but the textbook for one of my fall classes is the 5th edition of C++ Primer, which makes a big deal about being rewritten for the new standard. I was just trying to deal with any headache of switching now vs. continuing along with VS2010 for the time being, only to run into an assignment that requires something from the new standard in the middle of the semester when I have decidedly less time for dicking around.

raminasi
Jan 25, 2005

a last drink with no ice
I would be surprised if Programming 201 or whatever exercises the parts of C++11 that VS2010 is missing, but I'm just a guy on the Internet. I would actually consider emailing the professor if you know who it is.

yippee cahier
Mar 28, 2005

Strabo posted:

Yeah, that's the only reason I can think of, why somebody would want to do that. I was contemplating posting this in the coding horrors thread, because it's pretty confusing to anyone trying yo understand the code and the rest of the code that this came with was kinda horrible as well.
But then I thought that this may be some super advanced C trick that I don't know about yet :v:

Self documenting code? So you never have to wonder what kind of address "address" is? That's my guess.

OzyMandrill
Aug 12, 2013

Look upon my words
and despair

sund posted:

Self documenting code? So you never have to wonder what kind of address "address" is? That's my guess.
I'm sure I will have done something similar, as I tend deal with multi-platform stuff where there can be several fields that are only relevant to one particular code path (i.e. exporting specific texture formats, or shader specific vars)

If I want to pass a var from an early function I've hacked/bodgedextended to some later process, but the size of the spaghetti-codecodebase prohibits just adding an extra var. Especially if dealing with external tools that are very particular about the format and were written 3 years ago & the guys left...

Tbh, the existence of a var called 'extend_address' is probably due to someone doing exactly the same thing, but early enough in the dev process that they could add a whole var. Roll on a couple of years and a few tens of extra possible code paths & functions & you could be easily stuffed.

Doing it like this when you know others will be poking away at the codebase is 'safer' than just wanging it in a generic 'extra' field, and 'politer' in that not only should they notice this var is being 'reused', but you may even be able to track down in CVS who added the latest 'overload' & buggered up the one you did 3 months ago...

tractor fanatic
Sep 9, 2005

Pillbug
edit: gently caress it

tractor fanatic fucked around with this message at 06:19 on Aug 18, 2013

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
Does calling a function allow strict aliasing violations, even if the function is inlined? i.e. if I write this:

code:
inline void copy4(void *dest, const void *src)
{
    *static_cast<int*>(dest) = *static_cast<const int *>(src);
}

int main(int argc, const char **argv)
{
    float f1 = 1.0f;
    float f2 = 0.0f;
    copy4(&f2, &f1);
    printf("Should be 1: %f\n", f2);
    return 0;
}

... is that always going to work?

OneEightHundred fucked around with this message at 00:09 on Aug 19, 2013

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
That's UB even without the inlining, so no, it's not guaranteed.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


How would I track the real execution time of a program in C without using the shell time command, or using clock()? My program runs a lot longer than the 2147 seconds I can get out of clock() (and I can't poll mid run), and I can't figure out for the life of me how to use the time_t I get from time() and difftime() to print a human-readable difference (rather than some time a few hours since 1/1/1970, that is).

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.

Ciaphas posted:

How would I track the real execution time of a program in C without using the shell time command, or using clock()? My program runs a lot longer than the 2147 seconds I can get out of clock() (and I can't poll mid run), and I can't figure out for the life of me how to use the time_t I get from time() and difftime() to print a human-readable difference (rather than some time a few hours since 1/1/1970, that is).

code:
time_t start, end;

start = time(NULL);
/* do long processing */
end = time(NULL);

/* Cast result to int because time_t may be 32 or 64 bits depending on platform.
   Hopefully your processing doesn't take longer than 2 billion seconds */
printf("Processing took %d seconds\n", (int) (end - start));

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Blarg, I guess I should have realized time_t is just an int typedef. I'm not thinking too well today. Thanks :smith:

b0lt
Apr 29, 2005

Ciaphas posted:

Blarg, I guess I should have realized time_t is just an int typedef. I'm not thinking too well today. Thanks :smith:

It's technically legal for time_t to be a typedef to float or double :v:

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


b0lt posted:

It's technically legal for time_t to be a typedef to float or double :v:

I hate this/these loving language(s). :sigh:

ephphatha
Dec 18, 2009




Even if it is, casting to an int will convert the number (possibly with rounding errors).

schnarf
Jun 1, 2002
I WIN.

OneEightHundred posted:

Does calling a function allow strict aliasing violations, even if the function is inlined? i.e. if I write this:

code:
inline void copy4(void *dest, const void *src)
{
    *static_cast<int*>(dest) = *static_cast<const int *>(src);
}

int main(int argc, const char **argv)
{
    float f1 = 1.0f;
    float f2 = 0.0f;
    copy4(&f2, &f1);
    printf("Should be 1: %f\n", f2);
    return 0;
}

... is that always going to work?

You should be able to use memcpy() for that; any sane compiler will optimize the function call away to do what you want: http://gcc.godbolt.org/#%7B%22versi...B0x%22%7D%5D%7D

Adbot
ADBOT LOVES YOU

The_Franz
Aug 8, 2003

Ephphatha posted:

Even if it is, casting to an int will convert the number (possibly with rounding errors).

I don't think so. Even if the void* types of the inline function parameters are discarded, he is still casting the pointer types to int* and then dereferencing the value, not casting the values directly. That shouldn't do any conversions or rounding.

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