Search Results

Search found 6 results on 1 pages for 'shinka'.

Page 1/1 | 1 

  • C: sprintf and recursion

    - by Shinka
    In C, is it possible to use recursion within the sprintf function ? For some reason I get a segmentation fault when I do it: inline char *TreeNode_toString(const TreeNode *node) { char *out; if(TreeNode_isExternal(node)) // If the node has no children... { sprintf(out, "%s:%.2f", node->name, node->distance); } else // The node is strictly binary, so it will have two non-null children { char *l = TreeNode_toString(node->l); // l = left child char *r = TreeNode_toString(node->r); // r = right child sprintf(out, "(%s,%s):%.2f", l, r, node->distance); } return out; }

    Read the article

  • C++: Efficiently adding integers to strings

    - by Shinka
    I know how to add integers to strings, but I'm not sure I'm doing it in an efficient matters. I have a class where I often have to return a string plus an integer (a different integer each time), in Java I would do something like public class MyClass { final static String S = "MYSTRING"; private int id = 0; public String getString() { return S + (id++); } } But in C++ I have to do; class MyClass { private: std::string S; // For some reason I can't do const std::string S = "MYSTRING"; int id; public: MyClass() { S = "MYSTRING"; id = 0; } std::string getString() { std::ostringstream oss; oss << S << id++; return oss.str(); } } An additional constraint: I don't want (in fact, in can't) use Boost or any other librairies, I'll have to work with the standard library. So the thing is; the code works, but in C++ I have to create a bunch of ostringstream objects, so it seems inefficient. To be fair, perhaps Java do the same and I just don't notice it, I say it's inefficient mostly because I know very little about strings. Is there a more efficient way to do this ?

    Read the article

  • C: performance of assignments, binary operations, et cetera...

    - by Shinka
    I've heard many things about performance in C; casting is slow compared to normal assignments, functional call is slow, binary operation are much faster than normal operations, et cetera... I'm sure some of those things are specific to the architecture, and compiler optimization might make a huge difference, but I would like to see a chart to get a general idea what I should do and what I should avoid to write high-performance programs. Is there such a chart (or a website, a book, anything) ?

    Read the article

  • C: using clock() to measure time in multi-threaded programs

    - by Shinka
    I've always used clock() to measure how much time my application took from start to finish, as; int main(int argc, char *argv[]) { const clock_t START = clock(); // ... const double T_ELAPSED = (double)(clock() - START) / CLOCKS_PER_SEC; } Since I've started using POSIX threads this seem to fail. It looks like clock() increases N times faster with N threads. As I don't know how many threads are going to be running simultaneously, this approach fails. So how can I measure how much time has passed ?

    Read the article

  • C: Reading file with a starting point

    - by Shinka
    A simple question but I can't find the answer in my book. I want to read a binary file to seed a random number generator, but I don't want to seed my generator with the same seed each time I call the function, so I will need to keep a variable for my position in the file (not a problem) and I would need to know how to read a file starting a specific point in the file (no idea how). The code: void rng_init(RNG* rng) { // ... FILE *input = fopen("random.bin", "rb"); unsigned int seed[32]; fread(seed, sizeof(unsigned int), 32, input); // seed 'rng'... fclose(input); }

    Read the article

  • Throwing out of range exception in C++

    - by Shinka
    This code works; int at(int index) { if(index < 1 || index >= size) throw 0; return x[index]; } Yet this doesn't int at(int index) { if(index < 1 || index >= size) throw std::out_of_range; return x[index]; } I get the error "expected primary expression before ';'". Now... it surprises me because I know std::out_of_range exists and I have #include <stdexcept>

    Read the article

1