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
Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

DoctorTristan posted:

Am I right in thinking that if I have a class with a public static member, I should always initialise it in a .cpp file and not in the header?
Yes, only constant integral types can be initialized in the class declaration/header (until C++0x).

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

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

Mustach posted:

Yes, only constant integral types can be initialized in the class declaration/header (until C++0x).

And then when C++ gets a module system, it'll change all over again!!! :woop:

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Ugh don't remind me of how badly I want modules in C++.

ctz
Feb 6, 2003
C99: Is the following statement true or false?

It is impossible to portably call the following function with integer literals, without casting, and with a non-zero first argument.

code:
void test(int count, ...)
{
  va_list args;
  va_start(args, count);
  
  while (count--)
  {
    size_t i = va_arg(args, size_t);
    printf("%zu\n", i);
  }
}
In other words, all the following uses are non-portable:

code:
test(1, 1);
test(1, 1u);
test(1, 1ul);
test(1, 1ull);
While the following are portable:

code:
{ size_t i = 1; test(1, i); }
test(1, (size_t) 1);
Right?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Incorrect. Variadic arguments are subject to the rules of default argument promotion (float -> double, and smaller integer types converted up to int or unsigned int).

EDIT: note that this means integral types larger than int retain their size and require knowledge of their types to be dereferenced. See §7.15 of the ISO standard.

Avenging Dentist fucked around with this message at 01:27 on May 13, 2009

ctz
Feb 6, 2003

Avenging Dentist posted:

Incorrect. Variadic arguments are subject to the rules of default argument promotion (float -> double, and smaller integer types converted up to int or unsigned int).

I don't think default argument promotion applies here: the interesting arguments are decimal integer literals which are always at least ints (6.4.4.1).

Avenging Dentist posted:

EDIT: note that this means integral types larger than int retain their size and require knowledge of their types to be dereferenced. See §7.15 of the ISO standard.

Exactly! In the examples:

code:
test(1, 1);
test(1, 1u);
test(1, 1ul);
test(1, 1ull);
The types of the second argument are int, unsigned int, unsigned long and unsigned long long. None of these are guaranteed "compatible types" with size_t, and as 7.15 says:

quote:

If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined, except for the following cases:
* one type is a signed integer type, the other type is the corresponding unsigned integer type, and the value is representable in both types;
* one type is pointer to void and the other is a pointer to a character type.
(emphasis mine)

Avenging Dentist
Oct 1, 2005

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

ctz posted:

I don't think default argument promotion applies here: the interesting arguments are decimal integer literals which are always at least ints (6.4.4.1).

Then you use ints and not size_ts, which honestly is a pretty bad idea to begin with. Or you do the right thing and pass a format string. Or the even righter thing and use C++ templates.

Actually according to the excerpt you posted, it's legit to use size_t if all your inputs are unsigned, since size_t is an unsigned integer type.

You might be able to fix it with variadic macros, but it would be non-trivial.

Avenging Dentist fucked around with this message at 02:10 on May 13, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
The Final Solution, using Boost.Preprocessor:

code:
#include <boost/preprocessor.hpp>

#define doit(r,data,elem) (size_t)(elem)                \
        BOOST_PP_COMMA_IF(BOOST_PP_LESS_EQUAL(r,data))

#define foo(count,...)                                  \
    real_foo(count,                                     \
        BOOST_PP_LIST_FOR_EACH(doit,count,              \
            BOOST_PP_TUPLE_TO_LIST(count,(__VA_ARGS__)) \
            )                                           \
        )

int main()
{
    foo(4, 2,3,5,7);
}
gcc -std=c99 -E variadics.c
code:
/* ... */
int main()
{
    real_foo(4, (size_t)(2) , (size_t)(3) , (size_t)(5) , (size_t)(7) );
}

Contero
Mar 28, 2004

In C++, static global functions are static in the C way right? As in can be only used by that c/cpp file and not externed? I'm pretty sure this is true I just want to make sure there isn't some special C++ thing going on since static seems to have about 17 different uses.

Avenging Dentist
Oct 1, 2005

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

Contero posted:

In C++, static global functions are static in the C way right? As in can be only used by that c/cpp file and not externed? I'm pretty sure this is true I just want to make sure there isn't some special C++ thing going on since static seems to have about 17 different uses.

Yes. But the idiomatic C++ way is to use an anonymous namespace.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Yes but the more "C++ way" is use the anonymous namespace instead. namespace{ /* whatever */ }

edit: bah, and I even almost said "idiomatic," too

Contero
Mar 28, 2004

That's exactly the kind of thing I was hoping to hear! Thanks.

fart barterer
Aug 24, 2006


David Byrne - Like Humans Do (Radio Edit).mp3
Last night I was playing around in C++, I'm a total beginner but I was thinking about how an X-COM or FFTactics style game could be made. I figured the "map" would probably be a 3-dimensional array. Or 4-dimensional, if I wanted to store a bunch of data for each cell? But I guess it would be cheaper to make each entity contain it's own data and position in space than have all that blank data where there's nothing going on. I'm just musing here, if anyone wants to add anything or throw any concepts at me before I get my head going down the wrong path.

I've also played around with writing to the screen buffer while doing Wii homebrew, like how to draw an image or a box, so for now I've stared with a two dimensional array and I'm dicking around with "drawing" numbers to it, and printing it out as a "screen" of numbers as "pixels". Eventually from a 3-dimensional array I'll try to create functions to draw cubes and triangles at whatever size I want, etc.

Anyhow, I've been having some strange errors come up. I have 2 const ints for the "resolution" of the array (which is called "scrn") that I can set before compiling. If I set them both to four (so including 0 it's a 5x5) and I set scren[2][2] to 1 (while all the others are 0) it looks like this:

code:
00000
00000
00100
00000
00000
But if I set either the top or bottom row to anything, it changes the other, shifted over one. For example setting [0][3] (which are read as x/y) to one would produce:

code:
00001
00000
00000
00000
00010
And 0-1 would draw one at 4-0, etc. It's always shifted to the left on the bottom, so 0-0 wouldn't make any visible change, nor would 4-0
I figured it had something to do with the functions I had written (I had a plain old function to write to one "pixel") but even if I bypassed them and just used "scrn[0][4] = 1" I'd get the same result. I don't have the source code on me, but if anyone has any idea I'd be grateful for some input. I'm also throwing all those other weird theories out there some people could maybe throw some ideas at me.

EDIT: Now that I think about it there's still the possibility that it's in the prntScreen function, which goes along and writes what's at X/Y to screen increments X and writes again until it reaches the end of the "line", then increments y and resets x to 0, repeat. That's probably it. Bah.

fart barterer fucked around with this message at 19:35 on May 14, 2009

Morton the Mole
Feb 24, 2005
Paid money to stop lurking.

androo posted:

If I set them both to four (so including 0 it's a 5x5)

I think you need to read up on some basic C++ array stuff, because an_array[4][4] does not yield a 5x5 including the 0th place.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Ahhhh, I remember the days when I used to think that the array was the only data structure in the world. :kiddo:

Vinterstum
Jul 30, 2003

androo posted:

(which is called "scrn")

"scrn[0][4] = 1"

in the prntScreen function


Don't. Just... don't. The extra letters needed for "screen" and "printScreen" never hurt anyone, you've got plenty of disk space available for sourcecode, and you need all the extra readability you can get.

StickGuy
Dec 9, 2000

We are on an expedicion. Find the moon is our mission.

Avenging Dentist posted:

Ahhhh, I remember the days when I used to think that the array was the only data structure in the world. :kiddo:
I didn't realize yesterday was so far in the distant past. :iceburn:

fart barterer
Aug 24, 2006


David Byrne - Like Humans Do (Radio Edit).mp3

Morton the Mole posted:

I think you need to read up on some basic C++ array stuff, because an_array[4][4] does not yield a 5x5 including the 0th place.

Doh, I'll look that up then.

Vinterstum posted:

Don't. Just... don't. The extra letters needed for "screen" and "printScreen" never hurt anyone, you've got plenty of disk space available for sourcecode, and you need all the extra readability you can get.

Hah, good point. It's just nice to bang them out really quick while typing, and I never know when something else could be using those terms I guess?

Avenging Dentist posted:

Ahhhh, I remember the days when I used to think that the array was the only data structure in the world. :kiddo:

: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.

PIGEOTO
Sep 11, 2007

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.

Arrays are nice, but depending on what you are doing there could be much better alternatives. What you are doing looks just right for arrays since you can't get any better, but if you are interested in other data structures then take a look at the C++ STL. A good choice in data structure can mean the difference between your algorithm taking minutes over milliseconds, but they all have different uses.

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum
Does anyone know anything about decoding wma files so that I can get the samples from their wav form? I have done it with mp3s, but I don't see much documentation on wma files and google just shits out a bunch of "free decoder" stuff when I try and search for it... any help would be appreciated.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
If you're fine with being windows-only, just use DirectShow. For other platforms, ffmpeg is the only particularly functional option, which you can use either by using libavcodec directly or by using a wrapper such as FFmpegSource.

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];

POKEMAN SAM
Jul 8, 2004

Dijkstracula posted:


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];

I'm a C++ scrub, but will this:
struct tile world[256][256];
lay out in memory the same as this:
struct tile world[256*256];?

I always get confused with C++'s pointer vs. array stuff.

That Turkey Story
Mar 30, 2003

Ugg boots posted:

I'm a C++ scrub, but will this:
struct tile world[256][256];
lay out in memory the same as this:
struct tile world[256*256];?

I always get confused with C++'s pointer vs. array stuff.

They both occupy the same amount of space in a contiguous block of memory. Multidimensional arrays are row major.

POKEMAN SAM
Jul 8, 2004

That Turkey Story posted:

They both occupy the same amount of space in a contiguous block of memory. Multidimensional arrays are row major.

That's what I was wondering (the contiguous block of memory.) I assumed so, but I've learned never to assume anything about C/C++.

Thanks.

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.
If I have a class containing pointers to other objects and a destructor that destroys the other objects, will this in turn call the destructors of the objects below it (which would be set up to destroy those and any they contain)?

Is there a better way to do this or is that okay?

e; Think I answered my own question. It's a fastcgi application and memory usage is staying constant regardless of usage.

Murodese fucked around with this message at 12:13 on May 17, 2009

Stealthgerbil
Dec 16, 2004


what is the best way to read data from a file?

I have a file with a bunch of numbers in a line...

such as

'2410 1 3832 3 2838 2 123 1'

I need to read each number and store it in a variable. so far i have tried streaming it in but it only gets each single digit. i am trying to get each number and stop at the space.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
int i;
file >> i;
:confused:

Stealthgerbil
Dec 16, 2004


well the problem is that i have to store the data in a structure as well. pretty much i have to read from the text file a number followed by an integer from 1-3. then i store that in a structure. once it is in the structure the amount will get added or subtracted depending on the integer code. in the file there are spaces between the numbers as well.

i was thinking of doing something like

while (inFile >> temp.balance)
{
inFile >> temp.balance;
inFile >> temp.code;
}

but it doesn't really work. I also tried reading each of the individual characters. I tried turning that into an array but i couldn't figure out a good way to turn the array back into a double variable.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Maybe you should have mentioned these details in your first post, Mr. The Riddler.

That code snippet doesn't do what you want, because while (inFile >> temp.balance) attempts to read something into temp.balance, and then inFile >> temp.balance; attempts to read the next thing into temp.balance. So, you've got three attempted reads per loop instead of the two that you want.

quote:

in the file there are spaces between the numbers as well.
The default behavior for >> is to skip whitespace.

Stealthgerbil
Dec 16, 2004


i am having the problem now that when i use the inFile >> next, it reads the entire line of text. i get some weird number instead of 2000, which is the first number.

i am thinking of trying to have it read the characters invidually and stop at the first blank space and then turn that character string into a double variable but i am not exactly sure what an efficient way for this is. I am thinking of running a do-while loop until it hits a space. it would store each character in a string and then convert the entire string into a double variable and store that in the structure.

Avenging Dentist
Oct 1, 2005

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

Stealthgerbil posted:

i am having the problem now that when i use the inFile >> next, it reads the entire line of text. i get some weird number instead of 2000, which is the first number.

Stop going down the path you've been going and listen to people. You just keep making things worse and not really explaining what you're even doing.

code:
while(!file.eof())
{
    // Fill in a single foo_class object
    foo_class tmp;
    file >> tmp.foo;
    file >> tmp.bar;

    // Do something with the foo_class object
    tmp.fart();

    // Go back to the top and repeat with a new object
}

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

Avenging Dentist posted:

code:
while(!file.eof())
{
    ...
}

You know better than to teach him this

Avenging Dentist
Oct 1, 2005

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

Otto Skorzeny posted:

You know better than to teach him this

I give no gently caress whatsoever about errors, especially not with I/O streams. scanf or a parser, nothing else.

(Besides that, a real C++ programmer would set it to throw exceptions so you know what went wrong at the point that it went wrong, rather than at the next iteration.)

EDIT: Here is simultaneously why not using exceptions with I/O streams is dangerous and also part of why I don't like I/O streams.

Avenging Dentist fucked around with this message at 21:52 on May 19, 2009

Stealthgerbil
Dec 16, 2004


Well it works except the only problem is that it only reads the first 2 pairs of numbers from the file and doesn't read the rest.

I have it set like
code:
while (!in_stream.eof( ))
{
in_stream >> trans.transAmt >> trans.transCode;
cout << trans.transAmt << endl << trans.transCode;
system("pause");
return trans;
}
It works but it only gets the first 2 pairs of numbers. It doesn't finish reading the file because it doesn't exit the while loop. It just loops the second pair of values read and does the pause function.

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

Stealthgerbil posted:

code:
while (!in_stream.eof( ))
{
    ...
    return trans;
}
:psyduck:

Avenging Dentist
Oct 1, 2005

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

Otto Skorzeny posted:

:psyduck:

Let's not forget system("pause");

TheSleeper
Feb 20, 2003

Stealthgerbil posted:

Well it works except the only problem is that it only reads the first 2 pairs of numbers from the file and doesn't read the rest.

I have it set like
code:
while (!in_stream.eof( ))
{
in_stream >> trans.transAmt >> trans.transCode;
cout << trans.transAmt << endl << trans.transCode;
system("pause");
return trans;
}
It works but it only gets the first 2 pairs of numbers. It doesn't finish reading the file because it doesn't exit the while loop. It just loops the second pair of values read and does the pause function.

For everyone's sake set up an appointment with your TA or professor or something. I don't think you really understand a lot of really basic concepts in programming.

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog

TheSleeper posted:

For everyone's sake set up an appointment with your TA or professor or something. I don't think you really understand a lot of really basic concepts in programming.

Like indentation.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
        int main()
      {
    while(true)
  {
printf("Butts\n");
  }
      }

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