• What Do You Need Help With? V6
    7,544 replies, posted
But your blocks are 64 chars, so you're adding 64 bytes for every 1 increase in the index. And yes, your allocation statement can be written: [cpp]blockAddress = mem + index * 64;[/cpp] The original compiler error was only because of mismatched types. Try casting it: [cpp]desiredIndex = ((unsigned char *)memAddr - mem) / 64[/cpp]
[QUOTE=account;39800203]But your blocks are 64 chars, so you're adding 64 bytes for every 1 increase in the index. And yes, your allocation statement can be written: [cpp]blockAddress = mem + index * 64;[/cpp] The original compiler error was only because of mismatched types. Try casting it: [cpp]desiredIndex = ((unsigned char *)memAddr - mem) / 64[/cpp][/QUOTE] Tried changing my allocation to: [cpp] address = &mem[STRING_SIZE * index]; // STRING_SIZE = 64 [/cpp] and my free statement to [cpp] int blockIndex = ((unsigned char*)exampleString - mem) / 64); mcshFree(blockIndex); [/cpp] However, when blockIndex is passed to mcshFree, its value is a crazy constant like -18879364. Obviously that block doesn't exist, yielding a segmentation fault. I know I'm missing something really simple here. Urgh.
[QUOTE=thirty9th;39800351]Tried changing my allocation to: [cpp] address = &mem[STRING_SIZE * index]; // STRING_SIZE = 64 [/cpp] and my free statement to [cpp] int blockIndex = ((unsigned char*)exampleString - mem) / 64); mcshFree(blockIndex); [/cpp] However, when blockIndex is passed to mcshFree, its value is a crazy constant like -18879364. Obviously that block doesn't exist, yielding a segmentation fault. I know I'm missing something really simple here. Urgh.[/QUOTE] That's weird, at first I thought it might have to do with trying to fit a 64-bit address into a 32 bit int, but then I realized that the offset there can be no more than 8192. Try fprintf()ing the difference ((unsigned char *)exampleString - mem) and see what it shows. [editline]4th March 2013[/editline] But in any case, mcshFree should take a pointer to a memory location, not a block index. it should compute the block index itself, which will save you a lot of typing.
[QUOTE=account;39800450]That's weird, at first I thought it might have to do with trying to fit a 64-bit address into a 32 bit int, but then I realized that the offset there can be no more than 8192. Try fprintf()ing the difference ((unsigned char *)exampleString - mem) and see what it shows. [editline]4th March 2013[/editline] But in any case, mcshFree should take a pointer to a memory location, not a block index. it should compute the block index itself, which will save you a lot of typing.[/QUOTE] Oh I realize that I should have the computation in mcshFree for the sake of modularity. Until I figure out this issue, though, I'm testing it from within an overarching function. I will be right back with that output. [editline].[/editline] Ouput for ((unsigned char*)exampleString - mem): [code]-1208279348[/code]
[QUOTE=thirty9th;39800555]Oh I realize that I should have the computation in mcshFree for the sake of modularity. Until I figure out this issue, though, I'm testing it from within an overarching function. I will be right back with that output. [editline].[/editline] Ouput for ((unsigned char*)exampleString - mem: [code]-1208279348[/code][/QUOTE] Well that isn't good :v: Try printing out both exampleString and mem using the %p printf flag. Obviously, if mem is really larger (or much smaller) than exampleString then your malloc function is returning some bad value. [editline]4th March 2013[/editline] Also, if you're using gcc try compiling with the -g flag and stepping through with gdb, examining the value of your malloc's return value and the intermediates inside the malloc function as you go. It's more efficient when writing tons of printf()s gets to be a pain.
I need help figuring out a regular expression for a word that can't have |, >, <, or & unless they have a \ in front of them. Right now I have [^ \t\n|><&]* which successfully does not allow any of the characters mentioned, but I'm not sure how to make it so that it'll allow them if they have the \ in front.
This is gonna get confusing. [code]([^ \t\n|><&]|(\\\| | \\< | \\> | \\&))*[/code] Dunno if this'll work, some languages aren't representable in regular expressions. But it matches 0 or more of either: A character from your character class, in other words anything besides those special characters, or One of '\|', '\<', '\>', or '\&'.
[QUOTE=account;39800892]This is gonna get confusing. [code]([^ \t\n|><&]|(\\\| | \\< | \\> | \\&))*[/code] Dunno if this'll work, some languages aren't representable in regular expressions. But it matches 0 or more of either: A character from your character class, in other words anything besides those special characters, or One of '\|', '\<', '\>', or '\&'.[/QUOTE] So this works: [code][^ \t\n\|><&]*|[\\\||\\<|\\>|\\&]*[/code] but in theory it'll only work if the word is either all escape characters or all non escape characters, right? I can't do this: [code][[^ \t\n\|><&]|[\\\||\\<|\\>|\\&]]*[/code] or I get a syntax error. I'll keep trying see what I can come up with. Thanks for the help, brah.
You gotta use parentheses because square brackets only specify single characters. I think [\\\|...] just means you can have a \ or a |. And yes, the one I gave still matches strings like abc<> because the abc part matched. I suppose if you are in a language that lets you capture matched parts you could compare the match to the entire string to ensure the whole thing matches. But it might also be better to just scan the string for those characters and make sure backslashes precede them. [editline]4th March 2013[/editline] Or scan the string for backslashes and only allow those characters after them.
[QUOTE=account;39801160]You gotta use parentheses because square brackets only specify single characters. I think [\\\|...] just means you can have a \ or a |. And yes, the one I gave still matches strings like abc<> because the abc part matched. I suppose if you are in a language that lets you capture matched parts you could compare the match to the entire string to ensure the whole thing matches. But it might also be better to just scan the string for those characters and make sure backslashes precede them. [editline]4th March 2013[/editline] Or scan the string for backslashes and only allow those characters after them.[/QUOTE] Awesome! The parenthesis worked. Thanks a lot, man.
I am currently working on a bullet hell shooter, and I'm having some troubles with implementing the weapons. The problem lies in the fact that I'd like to be able to fire guns faster than the framerate of the game. I figured this means I need to interpolate things like position and velocity to calculate where the bullets WOULD be had they been fired half way through a frame, for example. Can anyone quickly run me through how this would work? I can't see to get a nice smooth pattern in my bullets when I put the shot interval down to a small number like 0.001.
[QUOTE=account;39799537][cpp](exampleString - mem) / 64[/cpp] I believe that would give you the block it's in. You can also do a right shift for faster division :D [cpp](exampleString - mem) >> 6[/cpp][/QUOTE] I've never actually seen a C compiler that wouldn't automatically replace appropriate divs and muls with shifts.
Oh well, I like making optimizations explicit sometimes
Ok so I have a problem with my programming assignment and I have no idea how to fix it. [code]#include <iostream> #include <iomanip> using namespace std; void PrintArray(int a[], int size) { for( int i = 0; i < size-1; i++ ) cout << setw(2) << a[i] << ","; cout << setw(2) << a[size-1] << endl; } int arrayVals; int num[100]; int i = 0; int arrayPos = 0; int main( ) { cout << "This program sorts a set of numbers." << endl; cout << endl << "How many values? "; cin >> arrayVals; while ( i < arrayVals ) { cin >> num[i]; i++; } int minVal = num[0]; int minPos = 0; cout << endl; while ( arrayPos < arrayVals-1 ) { i = arrayPos; while ( i < arrayVals ) { if ( num[i] <= minVal ) { minVal = num[i]; minPos = i; } i++; } num[minPos] = num[arrayPos]; num[arrayPos] = minVal; PrintArray(num , arrayVals); arrayPos++; minVal = num[arrayPos]; } }[/code] [b]Expected Output[/b] 1, 5, 2, 3, 8, 6, 4, 6, 9, 4 1, 2, 5, 3, 8, 6, 4, 6, 9, 4 1, 2, 3, 5, 8, 6, 4, 6, 9, 4 1, 2, 3, 4, 8, 6, 5, 6, 9, 4 1, 2, 3, 4, 4, 6, 5, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 8, 9 [b]Actual Output[/b] 1, 5, 2, 3, 8, 6, 4, 6, 9, 4 1, 2, 5, 3, 8, 6, 4, 6, 9, 4 1, 2, 3, 5, 8, 6, 4, 6, 9, 4 1, 2, 3, 4, 8, 6, 4, 6, 9, 5 < ---- Problem here. It swaps the last 4 with the 5, instead of the first 4. 1, 2, 3, 4, 4, 6, 8, 6, 9, 5 1, 2, 3, 4, 4, 5, 8, 6, 9, 6 1, 2, 3, 4, 4, 5, 6, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 9, 8 1, 2, 3, 4, 4, 5, 6, 6, 8, 9 This is what is causing it to break and have no idea how to fix it. Any suggestions? It's supposed to show the Selection Sort process.
[cpp]#include <iostream>#include <iomanip> using namespace std; void PrintArray(int a[]) { for(int i = 0; i < arrayVals; i++) { cout << setw(2) << a[i] << ","; } cout << setw(2) << a[arrayVals] << endl; } int arrayVals; // You can access this from pretty much any function you'd like to, no need to pass it to PrintArray int num[100]; // this too int main( ) { cout << "This program sorts a set of numbers." << endl; cout << endl << "How many values? "; cin >> arrayVals; for ( int i = 0; i < arrayVals; i++ ) { cin >> num[i]; } int minVal = num[0]; int minPos = 0; cout << endl; for(int i = 0; i < arrayVals; i++) { for ( int j = 0; j < arrayVals; j++) { if ( num[j] < minVal ) // If it's equal ( <=) , why should we set it again? { minVal = num[i]; minPos = i; } } num[minPos] = num[i]; num[i] = minVal; PrintArray(num); minVal = num[i]; } return 0; // You have to return something! }[/cpp] Pretty sure this should compile, and it makes your code a good bit easier to read. As for your actual problem, I'm not sure, but it should change a good bit of your logic.
[QUOTE=Gulen;39804883][cpp]#include <iostream>#include <iomanip>[/cpp][/QUOTE] There's something about putting all includes in one line that makes my skin crawl.:rolleyes:
[QUOTE=P1raten;39805656]There's something about putting all includes in one line that makes my skin crawl.:rolleyes:[/QUOTE] I have the same with variable declaration.
[QUOTE=mobrockers2;39805803]I have the same with variable declaration.[/QUOTE] Depends if you are initializing them or not, if you just have them be at default value sometimes it can look better
[QUOTE=P1raten;39805656]There's something about putting all includes in one line that makes my skin crawl.:rolleyes:[/QUOTE] Oh, that's a formatting error, I usually put them on separate lines as well, didn't know you could put them on the same line, actually.
[QUOTE=Goz3rr;39805851]Depends if you are initializing them or not, if you just have them be at default value sometimes it can look better[/QUOTE] I might not bitch about it if you're initializing two or more variables with the same value at declaration, but otherwise I can't stand [cpp] int i, j, f; String no, you, didnt; [/cpp]
[QUOTE=Gulen;39805859]Oh, that's a formatting error, I usually put them on separate lines as well, didn't know you could put them on the same line, actually.[/QUOTE] You can't - the preprocessor only looks at lines that begin with # and I think having another # would just cause a syntax error.
How do you slice a 2D list in Python so it's just a column? as in [code] poop = [[1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3,4]] [/code] how would I get just all the 2s or 3s? I can do it like: [code] for x in poop: print x[1] [/code] to get all the 2s but theres gotta be a better way with slices or something?
[QUOTE=Rayjingstorm;39782783]Stylistically are parens preferred around control-flow expressions in Python: [code] if a in b and b in c: or if(a in b and b in c): ? [/code][/QUOTE] I do not believe it is common to use parentheses in such a way (but you are free to do whatever you want, of course). However, if you need to split the expression over multiple lines, it is perfectly acceptable to do so. I.e.: [code] if (a in b and b in c): do_the_thing() [/code]
so I've created some functions to find prime factors [cpp]#include <iostream> #include <vector> #include <math.h> std::vector<long> prime_sieve(long n) { std::vector<bool> boolvector (n, true); for(long i=2; i<sqrt(n); i++) { if(boolvector[i]) { for(long j=i*i; j<n; j+=i) { boolvector[i] = false; } } } boolvector[0] = boolvector[1] = false; std::vector<long> primevector; for(long i=0; i<n; i++) { if(boolvector[i]) primevector.push_back(i); } return primevector; } int trial_division(long long n) { std::vector<long> primes = prime_sieve(sqrt(n)); int primefactor = 0; std::vector<long>::iterator it; for(it = primes.begin(); it < primes.end(); it++) { if((*it)*(*it) > n) break; else if(n % (*it) == 0) { std::cout << *it << "\n"; primefactor = *it; n /= *it; } } return primefactor; } int main() { return trial_division(600851475143); }[/cpp] but it takes 10 seconds to execute. I've tried it with lower values, and it works faster. VS2012 says most of the program is spent on the line [cpp]boolvector[i] = false;[/cpp] how would I fix this?
Well you can fix the function by changing boolvector[i] = false; to boolvector[[b]j[/b]] = false; But that doesn't address the issue of memory usage or speed; only the issue of it not working correctly.
But it does work correctly; it's just slow. If I change the sieve, then the prime factor isn't the highest.
Debug your sieve function. You'll see that it's [i]not[/i] returning a vector of primes like it should, unless you make that modification.
I'm trying to think of how to implement collision detection efficiently right now, after thinking a bit I've come to the conclusion that I should divide the screen into about a 3x3 (or bigger if it gets especially complicated) grid, store the rects of the objects in a list of each of the squares it collides with and then test it in those? does that make sense, if so is that a good approach? If not, I'd like to be pointed in the right direction rather than told the correct way to do it. I like to try and solve things on my own! Helps me learn.
I'm not an expert but generally you make something like a quadtree that can be subdivided and eliminate even more collisions. Still the same idea that you're only testing for collision within those little subdivisions.
oh god that quadtree stuff looks complicated [editline]5th March 2013[/editline] So I assume you set a limit for the depth to the quad tree, right?
Sorry, you need to Log In to post a reply to this thread.