Search Results

Search found 67 results on 3 pages for 'stackedcrooked'.

Page 3/3 | < Previous Page | 1 2 3 

  • Best practices for handling binary data in Ruby?

    - by StackedCrooked
    What are the best practices for reading and writing binary data in Ruby? In the code sample below I needed to send a binary file using over HTTP (as POST data): f = File.new("resp.der", "r") # binary file begin while true out.syswrite(f.sysread(1)) # out is an output stream (type IO) end rescue EOFError => err puts "Sent response." end While this code seems to do a good job, it probably isn't very idiomatic. How can I improve it?

    Read the article

  • Aliasing `T*` with `char*` is allowed. Is it also allowed the other way around?

    - by StackedCrooked
    Note: This question has been renamed and reduced to make it more focused and readable. Most of the comments refer to the old text. According to the standard objects of different type may not share the same memory location. So this would not be legal: int i = 0; short * s = reinterpret_cast<short*>(&i); // BAD! The standard however allows an exception to this rule: any object may be accessed through a pointer to char or unsigned char: int i = 0; char * c = reinterpret_cast<char*>(&i); // OK However, it is not clear to me if this is also allowed the other way around. For example: char * c = read_socket(...); unsigned * u = reinterpret_cast<unsigned*>(c); // huh? Summary of the answers The answer is NO for two reasons: You an only access an existing object as char*. There is no object in my sample code, only a byte buffer. The pointer address may not have the right alignment for the target object. In that case dereferencing it would result in undefined behavior. On the Intel and AMD platforms it will result performance overhead. On ARM it will trigger a CPU trap and your program will be terminated! This is a simplified explanation. For more detailed information see answers by @Luc Danton, @Cheers and hth. - Alf and @David Rodríguez.

    Read the article

  • Experiences using Wt C++ framework?

    - by StackedCrooked
    Has anyone actually used Wt? Did it work well? Did you experience certain limitations? Or advantages? Note: Please avoid the discussion of whether C++ is suited to be a web development language. I just want to give Wt a try because it seems like a fun thing to do.

    Read the article

  • Using abstract base to implement private parts of a template class?

    - by StackedCrooked
    When using templates to implement mix-ins (as an alternative to multiple inheritance) there is the problem that all code must be in the header file. I'm thinking of using an abstract base class to get around that problem. Here's a code sample: class Widget { public: virtual ~Widget() {} }; // Abstract base class allows to put code in .cpp file. class AbstractDrawable { public: virtual ~AbstractDrawable() = 0; virtual void draw(); virtual int getMinimumSize() const; }; // Drawable mix-in template<class T> class Drawable : public T, public AbstractDrawable { public: virtual ~Drawable() {} virtual void draw() { AbstractDrawable::draw(); } virtual int getMinimumSize() const { return AbstractDrawable::getMinimumSize(); } }; class Image : public Drawable< Widget > { }; int main() { Image i; i.draw(); return 0; } Has anyone walked that road before? Are there any pitfalls that I should be aware of?

    Read the article

  • Why do you sometimes need to write <typename T> instead of just <T> ?

    - by StackedCrooked
    I was reading the Wikipedia article on SFINAE and encountered following code sample: struct Test { typedef int Type; }; template < typename T > void f( typename T::Type ) {} // definition #1 template < typename T > void f( T ) {} // definition #2 void foo() { f< Test > ( 10 ); //call #1 f< int > ( 10 ); //call #2 without error thanks to SFINAE } Now I've actually written code like this before, and somehow intuitively I knew that I needed to type "typename T" instead of just "T". However, it would be nice to know the actual logic behind it. Anyone care to explain?

    Read the article

  • Including a C header which declares a variable called "new"?

    - by StackedCrooked
    I'm trying to use the OpenCA library in a C++ application. However, when including the file pki_x509_data_st.h the following code fragment is encountered: typedef struct pki_x509_callbacks_st { /* ---------------- Memory Management -------------------- */ void * (*new) (void ); void (*free) (void *x ); void * (*dup) (void *x ); This won't compile because of the "new" pointer declaration. How can I make it work?

    Read the article

  • Delayed evaluation in Clojure

    - by StackedCrooked
    I'm having some trouble understanding how the delay macro works in Clojure. It doesn't seem to do what expect it to do (that is: delaying evaluation). As you can see in this code sample: ; returns the current time (defn get-timestamp [] (.getTime (java.util.Date.))) ; var should contain the current timestamp after calling "force" (def current-time (delay (get-timestamp))) However, calling current-time in the REPL appears to immediately evaluate the expression, even without having used the force macro: user=> current-time #<Delay@19b5217: 1276376485859> user=> (force current-time) 1276376485859 Why was the evaluation of get-timestamp not delayed until the first force call?

    Read the article

  • Vectors or Java arrays for Tetris?

    - by StackedCrooked
    I'm trying to create a Tetris-like game with Clojure and I'm having some trouble deciding the data structure for the playing field. I want to define the playing field as a mutable grid. The individual blocks are also grids, but don't need to be mutable. My first attempt was to define a grid as a vector of vectors. For example an S-block looks like this: :s-block { :grids [ [ [ 0 1 1 ] [ 1 1 0 ] ] [ [ 1 0 ] [ 1 1 ] [ 0 1 ] ] ] } But that turns out to be rather tricky for simple things like iterating and painting (see the code below). For making the grid mutable my initial idea was to make each row a reference. But then I couldn't really figure out how to change the value of a specific cell in a row. One option would have been to create each individual cell a ref instead of each row. But that feels like an unclean approach. I'm considering using Java arrays now. Clojure's aget and aset functions will probably turn out to be much simpler. However before digging myself in a deeper mess I want to ask ideas/insights. How would you recommend implementing a mutable 2d grid? Feel free to share alternative approaches as well. Source code current state: Tetris.clj (rev452)

    Read the article

  • HTTP server that handles requests through IO devices?

    - by StackedCrooked
    This question can probably only be answered for Unix-like systems that follow the "everything is a file" idiom. Would it be hard to create a web server that mounts local devices for handling http traffic? It would enable a program to read raw http requests from /dev/httpin (for example) and write the responses to /dev/httpout. I think this would be nice because it would allow me to create a web server from any programming language that is capable of handling IO streams. I don't really know where to start on this. Any suggestions on how to setup such a system?

    Read the article

  • Is `std::string(strerror(errno))` dangerous?

    - by StackedCrooked
    At some places in my code, I print debug messages like this: int ret = getLinkSpeed(device.getSysName(), linkSpeed); if (ret < 0) { logDebug("Failed to obtain port speed for this device. Error: " + std::string(strerror(errno))); } From the documentation it is not entirely clear if strerror will return 0 under certain conditions (which would cause my code to crash). Does anyone know if it's safe?

    Read the article

  • How to interpret binary data as an integer?

    - by StackedCrooked
    The codebase at work contains some code that looks roughly like this: #define DATA_LENGTH 64 u_int32 SmartKey::SerialNumber() { unsigned char data[DATA_LENGTH]; // ... initialized data buffer return *(u_int32*)data; } This code works correctly, but GCC gives the following warning: warning: dereferencing pointer ‘serialNumber’ does break strict-aliasing rules Can someone explain this warning? Is this code potentially dangerous? How can it be improved?

    Read the article

  • Using sed with html data

    - by StackedCrooked
    I'm having some problems using sed in combination with html. The following sample illustrates the problem: HTML="<html><body>ENTRY</body><html>" TABLE="<table></table>" echo $HTML | sed -e s/ENTRY/$TABLE/ This outputs: sed: -e expression #1, char 18: unknown option to `s' If I leave out the / from $TABLE so that it becomes <table><table> it works ok. Any ideas on how to fix it?

    Read the article

  • Separate "include" and "src" folders for application-level code?

    - by StackedCrooked
    This questions concerns mostly Unix/Linux style C++ development. I see that many C++ libraries store their header files in a "include" folder and source files in an "src" folder. For the sake of conformance I adopted this in my own code. But it is not clear to me whether this should be done for application code as well. I've seen a few cases where a flat directory structure is used for that. What would be the recommended approach?

    Read the article

< Previous Page | 1 2 3