c++

C++ struct size: 2+4+2+2+4 = 16

Possible Duplicate: Why isnt sizeof for a struct equal to the sum of sizeof of each member? Why is the sizeof(); of this structure 16 bytes? I'm compiling in g++. struct bitmapfileheader { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigne...

Copying Part of Fstream to Istringstream

After a lot searching for solutions I've decided to actually ask for some help! I have a file that consists of a number of blocks, where each block may or may not be compressed. Before each block is an indication of the size of the block and whether the block is compressed. Within each block is a string that identifies the block. Giv...

evaluate trig functions in degrees as opposed to radians

So I have a function in an app that needs to let the user calculate a trig function (sin,cos,tan) using radians OR degrees, and also to expect a returned value from inverse trig functions (asin,acos,atan) as either radians or degrees. is there any way to do this without building in a converter directly into the way they input something?...

Segfault when assigning one pointer to another

My brain has never really quite grasped linked lists and the finer points of pointers but I'm trying to help out a friend with some C++ assignments. (And before I go any further, yes, there is std::list but I'm looking for an academic answer, and maybe something that will make linked lists more understandable to he and myself). What we ...

prefix to infix on stack

I'm trying to implement prefix to infix in c++, that's what i've got so far. The input should be for example something like this: /7+23 And the ouput: 7/(2+3) or (7/(2+3)) But instead I get: (/) That's the code I wrote so far: void pre_to_in(stack<char> eq) { if(nowe.empty() != true) { char test; test = eq.top();...

C++ implementing a regex map

I have mutliple regex expressions, each mapped to a different object. After passing in a string, I want to loop through each regex expression until one evaluates to true, then I would like to return the mapped object. What is the best way to implement this in C++? Is there a boost object available for this? ...

sqlite3_open - problems checking if a file is a sqlite3 database

Hey, I'm working with sqlite3 for the first time, and cannot get it to properly check a file before it opens it. So far, sqlite always returns OK on any file. Also, the file name is a variable returned from the GTK file chooser. It returns an absolute path, I'm guessing this is not a problem. Thanks for any help. This is a snippet of...

C++ stl stringstream direct buffer access.

Hello, this should be pretty common yet I find it fascinating that I couldn't find any straight forward solution. Basically I read in a file over the network into a stringstream. This is the declaration: std::stringstream membuf(std::ios::in | std::ios::out | std::ios::binary); Now I have some C library that wants direct access to th...

C++ socket message contains extra ASCII 0 character

So this is a really strange problem. I have a Java app that acts as a server, listens for and accepts incoming client connections, and then read data (XML) off of the socket. Using my Java client driver, everything works great. I receive messages as expected. However, using my C++ client driver on the first message only, the very first c...

Dealing with char arrays in C++

I have this C-styled piece of initialization code: const char * const vlc_args[] = { "-I", "dummy", "--ignore-config", "--extraintf=logger", "--verbose=2" "--plugin-path=/usr/lib/vlc" }; //tricky calculation of the char space used libvlc_new(sizeof(vlc_args)/sizeof(vlc_args[0]), vlc_args, &exc); Since I need to mak...

C++ template gotchas

just now I had to dig through the website to find out why template class template member function was giving syntax errors: template<class C> class F00 { template<typename T> bar(); }; ... Foo<C> f; f.bar<T>(); // syntax error here I now realize that template brackets are treated as relational operators. To do what was intended th...

Design an ipstack in C++

I want to implement an IP stack in C++ as a training project for me to the Linux and networking world. I have some knowledge of how the Linux IP stack works, but, as I said I want to implement something in C++ which has a good design rather then focusing on performance. Does anyone know where I can find written design for IP stacks writ...

Will using new (std::nothrow) mask exceptions thrown from a constructor?

Assume the following code: Foo* p = new (std::nothrow) Foo(); 'p' will equal 0 if we are out of heap memory. What happens if we are NOT out of memory but Foo's constructor throws? Will that exception be "masked" by the nothrow version of 'new' and 'p' set to 0?... Or will the exception thrown from Foo's constructor make it out of the...

Portable thread-safe lazy singleton

Greetings to all. I'm trying to write a thread safe lazy singleton for future use. Here's the best I could come up with. Can anyone spot any problems with it? The key assumption is that static initialization occurs in a single thread before dynamic initialisations. (this will be used for a commercial project and company is not using boo...

difference between -h <name> and -o <outputfile> options in cc (C++)

I am building .so library and was wondering - what is the difference b/w -h and -o cc complier option (using the Sun Studio C++) ? Aren't they are referring to the same thing - the name of the output file? ...

Most efficient tree structure for what I'm trying to do

I'm wondering what the most generally efficient tree structure would be for a collection that has the following requirements: The tree will hold anywhere between 0 and 232 - 1 items. Each item will be a simple structure, containing one 32-bit unsigned integer (the item's unique ID, which will be used as the tree value) and two pointers...

Using "assert" with pointers in C++

When do we need to use "assert" for pointers in C++, and when they are used, how are they most commonly implemented? ...

How do I check if a C++ <string> starts with a certain string, and convert a substring to an int?

How do I do the following (Python pseudocode) in C++? if argv[1].startswith('--foo='): foo_value = int(argv[1][len('--foo='):]) (For example, if argv[1] is '--foo=98', then foo_value is 98.) Update: I'm hesitant to look into Boost, since I'm just looking at making a very small change to a simple little command-line tool. (I'd ra...

C++ Common Ancestor Problem. What is it?

Hi I just finished taking my final exam. There was a questions that said, "define the common ancestor problem in C++", and state what feature of the language is used to address this problem. I don't remember ever learning about the common ancestor problem in class or ever hearing about it. However I wrote the following: I said it had t...

How can I draw a cylinder that connects two points in OpenGL

i have two point each point has its own X and Y value and they have the same Z value. i want a function to draw Cylinder between these two points. ...