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
Entheogen
Aug 30, 2004

by Fragmaster
how exactly do << and >> work?
I know they are bitwise rotation to the left or right, but I think they eat up a carry that is 1>>1 = 0 instead of - max integer.

Adbot
ADBOT LOVES YOU

Entheogen
Aug 30, 2004

by Fragmaster
when i do a left shift, does the left most bit get sent to some carry register? on x86 machines, what would be the name of it?
I guess same would go for the right shift and right most digit.

I know how this works in PIC Micro controller, but I have no idea of how i would go about using it in C or assembler for x86.

Entheogen
Aug 30, 2004

by Fragmaster

illamint posted:

I'm fairly new to C++ and I'm wondering what the regular expression library du jour is. For a project in my CS class, we have to parse fairly large files (right now I'm going line-by-line with the regular expression "^([A-Za-z0-9]+),([A-Za-z0-9]*),([A-Za-z0-9]*),(s?f?|f?s?);$"), and I'm using regex.h which is unfortunately incredibly slow. I see that there's Boost.Regex, but we have to hand in our source code at the end of the project and I'm not sure that it's feasible to merge the Boost shared library stuff into my makefiles just for this. Should I just parse it by hand? Is there a better solution? I'm used to being able to just go to the Java or Python docs and glance at the syntax in the manual but I'm having kindof a hard time doing that with C++.

use lex

DFAs are harder to implement in C++ than writing a lexer that can do the same thing. Lexer outputs DFA anyway. Just define your reg expression and associate a rule for when lexer finds it (that is what C++ code to execute), and voila, you are done.

Entheogen fucked around with this message at 14:53 on Mar 7, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Milde posted:

There's erase(iterator pos) and erase(iterator first, iterator last). The latter erases a range of elements. You want the former (and with an iterator).

You should bookmark this http://www.sgi.com/tech/stl/Vector.html. :)

http://www.cppreference.com/

That website is also really awesome for general references on C/C++ standard libraries and other stuff. I use it a lot.

Entheogen
Aug 30, 2004

by Fragmaster

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

I believe the OS allocates a chunk of RAM to the program and maps it using virtual memory. That is for program the memory may start at 0x0000 or something but then OS maps it to real memory. I am not quite sure.

I think for C++ programs "how large it is" can grow as the program continues to allocate new memory on heap, so the OS must be adding to its virtual page. I know for java you have to actually set a maximum amount of heap memory you will use, but not for C++.

Thug Bonnet posted:

Identical in what sense?

I think he means that start of program's heap address is like 0x000 (from program's perspective) ( i have no idea what it actually is ), and is the same for all threads that OS is executing.

Entheogen fucked around with this message at 22:32 on Mar 10, 2008

Entheogen
Aug 30, 2004

by Fragmaster
i just use iterators for FOREACH in C++ and use signed integers for everything else.

code:
#define FOREACH(_it,_l) for(__typeof((_l).begin()) _it=((_l).begin());(_it)!=(_l).end();(_it)++)
the reason to use signed integers for indecies of arrays is that you can set it to -1 to signify it not pointing to anything. Which sometimes you have to do. Otherwise what is the issue? both take up the same space.

Entheogen
Aug 30, 2004

by Fragmaster
hey, i never had issues with C++ iterators and STL containers. I use Java for bigger projects because its easier to work with. C++ is good for small programs when you just want to calculate some stuff though.

oh ok, is all the fuss about because many functions take size_t instead of int? i don't see how this could become an issue unless you are dealing with large indecies.

Entheogen fucked around with this message at 01:54 on Jul 4, 2008

Entheogen
Aug 30, 2004

by Fragmaster

sarehu posted:

Here's an algorithm that scales better with respect to integer size. I don't think it's a good idea to use a macro.

code:
uint32_t evenbits(uint64_t x) {
  x = (x & 0x1111111111111111LL) | ((x & 0x4444444444444444LL) >> 1);
  x = (x & 0x0303030303030303LL) | ((x & 0x3030303030303030LL) >> 2);
  x = (x & 0x000f000f000f000fLL) | ((x & 0x0f000f000f000f00LL) >> 4);
  x = (x & 0x000000ff000000ffLL) | ((x & 0x00ff000000ff0000LL) >> 8);
  x = (x & 0x000000000000ffffLL) | ((x & 0x0000ffff00000000LL) >> 16);
  return (uint32_t) x;
}

What does L stand for in there?

Entheogen
Aug 30, 2004

by Fragmaster
what does even bits mean? is it an AND operation with something that has 1's only in even places so the product only has 1's in even positions?

vvvv thanks that makes perfect sense now vvvvv

For what purposes would you want to use evenbits function for?

Entheogen fucked around with this message at 20:01 on Jul 4, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Mustach posted:

That macro re-evalutates its first argument 15 times.

doesn't this get done at compile-time, so by the time it compiles its super fast code since it doesn't have to do an extra function call?

Entheogen
Aug 30, 2004

by Fragmaster

Mustach posted:

Only if the argument is a constant expression. There stil won't be an "extra" function call since the macro just expands to a single statement, but EVEN_BITS_W(f()); expands to
code:
( \
((((f()) >> (0)) & 0x1)<< 0) | \
((((f()) >> (2)) & 0x1)<< 1) | \
((((f()) >> (4)) & 0x1)<< 2) | \
((((f()) >> (6)) & 0x1)<< 3) | \
((((f()) >> (8)) & 0x1)<< 4) | \
((((f()) >> (10)) & 0x1)<< 5) | \
((((f()) >> (12)) & 0x1)<< 6) | \
((((f()) >> (14)) & 0x1)<< 7) | \
((((f()) >> (16)) & 0x1)<< 8) | \
((((f()) >> (18)) & 0x1)<< 9) | \
((((f()) >> (20)) & 0x1)<<10) | \
((((f()) >> (22)) & 0x1)<<11) | \
((((f()) >> (24)) & 0x1)<<12) | \
((((f()) >> (26)) & 0x1)<<13) | \
((((f()) >> (28)) & 0x1)<<14) | \
((((f()) >> (30)) & 0x1)<<15));

oh ok, i see what you are saying. even though macro is replaced by pre-compiler with whatever it is defined to, if data is a function call it will be evaluated 15 times.

Entheogen
Aug 30, 2004

by Fragmaster

Insurrectum posted:

I'm talking about time differences in the 10 to 100s of milliseconds

http://www.cppreference.com/stddate/index.html

I believe time returns time in mili seconds, or it might be micro. In any case it should be enough for timing your programs.

Entheogen
Aug 30, 2004

by Fragmaster

Insurrectum posted:

I tried that and all I could get was seconds. Here's the solution I found:

code:
int main() {
    int before = GetTickCount();
    ...
    PROGRAM
    ...
    int after = GetTickCount();
    cout << "It took this program " << (after-before) << " milliseconds to run" <<endl;
    cin.get();
    return 0;
}

did you try using clock() method? that should definitely have granularity finer than 1 second.

what does your GetTickCount() do? do you just increment some variable in your loop somewhere in PROGRAM?

Entheogen
Aug 30, 2004

by Fragmaster

Avenging Dentist posted:

Check Boost.Turing, it's in the Boost Sandbox.

hahah, this is a joke, right?

Drx Capio posted:

What's an easy way to determine if an arbitrary program will halt? Is there a library for it?

If you are asking this seriously, then the answer is no. There is no general algorithm that will take another arbitrary algorithm and determine if it will ever halt or not. Look up halting problem on google if you don't believe me.

Entheogen
Aug 30, 2004

by Fragmaster
double post :(

Entheogen fucked around with this message at 21:12 on Jul 13, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Nubile Cactus posted:

Anyone have a list of c++ exercises, from relatively simple to slightly complex? Looking to brush up on c++ and I've always found exercises the best way to do it, but the book I'm using doesn't have any.

You can try doin practice programing competitions problems in C++. It will force you to laern more C++ and STL and also improve algo skills. Probably not that great for OOP learning tho.

I did a bunch of TopCoder problems using C++/STL and I found that it boosted my knowledge of STL quite a bit, since I ended up using it a lot.

Entheogen
Aug 30, 2004

by Fragmaster
how exactly does one link statically instead of dynamically in c++?

the only way to link dynamically would be against a DLL or SO file?

when I include .h files in my C++ program and it is linked against a .lib file is that static linking by default?

Entheogen
Aug 30, 2004

by Fragmaster
I used pointers before to switch between two arrays that contain same types.

such as
code:
vector< int > A, B;
vector<int> * curr = & A;
while( true )
{
   // do something 
   if( curr == & A ) curr = &B;
   else curr = &A;
}
Is there a better way of doing this without pointers? Or is this fine. Also in this case the pointer, is of course, pointing to something on a stack, right?

If I did something like this:
code:
int * a = & int(5);
then a is pointing to something on the stack and will be gone after it is popped off?

Also is there a way to get around using some confusing rear end referencing and dereferncing operators when dealing with iterators on containers that contain pointers themselves?

such as
code:
#define FOREACH(_it,_l) for(__typeof((_l).begin()) _it=((_l).begin());(_it)!=(_l).end();(_it)++) 

.....

class Foo
{
   public:
      void lol(){}
};

.......

vector< Foo * > lolz;
FOREACH( el, lolz )
{
   (*el)->lol();
}

Entheogen fucked around with this message at 21:02 on Jul 20, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Zombywuf posted:

std::swap will usually be optimised to do this internally, i.e. it woun't copy the contents of the vectors, just swap the pointers to the internal arrays. It's also better from a readability perspective, i.e. swap(A, B) does what it says.

does it copy in the example I had? I thought that the pointer just changed address from either A or B. I didn't realize it was copying their entire contents each time I did curr = & A

Entheogen
Aug 30, 2004

by Fragmaster

floWenoL posted:

Unless someone overrode operator& for vectors somewhere before. :q:

I don't understand. Is not & a referencing operator. So it gets you address of whatever it is on its right? What is going on here, I am getting confused.

vvvvvvvvvvvvvvvvvvvvvv OK, so STL overloaded & operator for vectors to copy them?

Entheogen fucked around with this message at 23:06 on Jul 21, 2008

Entheogen
Aug 30, 2004

by Fragmaster

Sabotaged posted:

Okay, I've been using a lot of STL lately, and it scares me a lot because I don't really understand how something like an stl string manages its memory.

If I declare a regular std::string foo, it would automatically allocate itself on the heap I'm assuming? Does it use reference counting or something to determine when to automatically delete itself?

What about string's c_str? Where is this memory? I assume there are no guarantees using this memory outside the immediate scope of the string?

I am probably wrong, but I think string allocates itself on stack if you allocate it on stack and just wraps char * inside of it. It deletes itself when it is popped from stack or when you delete it yourself assuming you put it into dynamic memory.

Entheogen
Aug 30, 2004

by Fragmaster
you need to store your numbers in an array not an int. int is for just one number that is represented with 4bytes usually. Also you are assigning evennumbers to store numbers % 2 before you even read store numbers from file. The reason it outputs only 1s is because you are only printing out one variable which never changes, and got set to 1 the last time you modified it. That is because the last number to be assigned to evennumber is 2 % 2 == 0 which is true or 1.

Also don't do while ( !outFile.eof() ) to print stuff to that file stream. You only need to check for that when you are reading it in.

oh and to catch errors, like whether files were succefully opened or not, you can just surround the whole thing with try catch block and catch std::exception & exc.

I am not sure if my code takes care of new line and carriage return characters for input. If you run into problems you can use .ignore function, but the way you should use it is
code:
if( ifs.peek() == '\n' || ifs.peek() == '\r' ) ifs.ignore();
that way you are only ignoring new line chars which you don't care for.

just do something like this. I use vector here which is STL class for dynamic arrays (they can be resized easily).

code:
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    ifstream ifs( "input_numbers.txt" );
    vector<int> numb3rs;
    while( !ifs.eof() )
    {
         int numb3r;
         ifs>>numb3r;
         numb3rs.push_back( numb3r );
    }
    ofstream ofs( "output_even_numbers.txt" );
    for( int i = 0; i < numb3rs.size(); ++i )
    {
       if( numb3rs[i] % 2 == 0 ) ofs<<numb3rs[i]<<endl;
    }
    ifs.close();
    ofs.close();
}

Entheogen fucked around with this message at 02:23 on Jul 23, 2008

Entheogen
Aug 30, 2004

by Fragmaster
is there a way to determine endianess of a floating point number at runtime in either C++ or Java? Is this something that cannot be determined from data alone? I am reading a binary file and would like to know if I have to reverse the bits or not.

Entheogen
Aug 30, 2004

by Fragmaster

Avenging Dentist posted:

Why wouldn't you do that at compile time?

What do you mean? The idea is that I will have binary files as input and some of them might have IEEE floating point numbers in one endianness and another in different one. Instead of forcing user to choose which one they would like the file to be read as, I was wondering if there is an automatic way of doing this, but my suspicion is there is not because otherwise there would have to be some sort of meta-data describing the contents of file, and there is no such data, its just floating point numbas.

Adbot
ADBOT LOVES YOU

Entheogen
Aug 30, 2004

by Fragmaster

Avenging Dentist posted:

That's stupid.

Edit for content: Seriously, what kind of rear end in a top hat would make a binary file specification without defining the endianness?

I am reading in files made from Fortran where it is reverse. But I would also like my software to work with files generated with normal endianness. Ideally I don't want to rely on any sort of meta-data in the file itself to be able to successfully read it in and visualize it. I think I will try NaN and Infinite values perhaps or try to look for those bits that can never be true if its correct endianess. In worst case scenario I will just let the end-user decide what endianess they want before they read in the file.

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