c++

Bug in Programming Interviews Exposed: Linked Lists

Hello, I was going through the Programming Interviews Exposed book. There's a code given for inserting an element at the front of linked lists. bool insertInFront( IntElement **head, int data ){ IntElement *newElem = new IntElement; if( !newElem ) return false; newElem->data = data; *head = newElem; return true; } ...

How much overhead is there when creating a thread?

Hey all, I just reviewed some really terrible code - code that sends messages on a serial port by creating a new thread to package and assemble the message in a new thread for every single message sent. Yes, for every message a pthread is created, bits are properly set up, then the thread terminates. I haven't a clue why anyone would do...

C++ and SDL: How does SDL_Rect work exactly?

Hello there, I'm working on some SDL stuff and I've run into some trouble when trying to set the location of a loaded BMP. Here's the code. while(event.type != SDL_QUIT) //The game loop that does everything { SDL_Rect *location; location = SDL_Rect(600,400,0,0); SDL_PollEvent(&event); //This "polls" the event //Drawing ...

Stack overflow - static memory vs. dynamic memory

If you write int m[1000000]; inside the main function of C/C++, it will get a runtime error for stack overflow. Instead if you write vector<int> m; and then push_back 1000000 elements there, it will run fine. I am very curious about why this is happening. They both are local memory, aren't they? Thanks in advance. ...

FreeImage dll nor working for batch process

Hi, I need to load batches of image files and change their dimensions. I'm doing it through FreeImage.dll in C++. Each batch contains JPG and TIF files. The problem is the exe processes the first batch accurately but for further batches it starts skipping some files, specially JPG files. I tried Initializing and Uninitializing FreeIma...

Access problem in local class

void foobar(){ int local; static int value; class access{ void foo(){ local = 5; /* <-- Error here */ value = 10; } }bar; } void main(){ foobar(); } Why doesn't access to local inside foo() compile? OTOH I can easily access and modify the static variable v...

How to add an error logger to a class.

Hi, I would like to use a generic error msg handler so I can GetLastError and SetError easily for any class. I came up with this scheme. But I have a couple of questions. Please note this implementation is just for testing. But I want to get the basic design right. #include <iostream> #include <stdarg.h> class ErrorHandler { p...

make file issue : always spits out "Nothing to be done for `make.w'."

I have 3 files Head.cpp , Head.h and Hello.cpp . I am trying to build a make for the compilation process. My makefile is make.w Hello : Head.o Hello.o g++ -o Head.o Hello.o Head.o : Head.cpp g++ -o Head.cpp Hello.o: Hello.cpp g++ -o Hello.cpp every time I type the command make make.w - I get " Nothi...

Comparing typenames in C++

I typed this into a template function, just to see if it would work: if (T==int) and the intellisense didn't complain. Is this valid C++? What if I did: std::cout << (int)int; // looks stupid doesn't it. ...

Stack memory is being referred by dynamically allocated object member variable

Hi, I have following code class Test { public: int &ref; int a; Test(int &x) :ref(x) { cout<<"Address of reference "<<&ref<<endl; cout<<"&a : "<<&a<<endl; cout<<"this = "<<this<<endl; } }; int main() { Test *pObj = NULL; { int i = 10; cout<<"Address of refer...

Using Thread for Taking input in one of them and Displaying output in the other one

I am making a chat application for my homework which runs within a Linux Terminal. So, I need to take from the user some input and display the output from all the users as well in a well organized manner. So, I made two threads. One thread have a cin command and the other thread is having a display function which basically uses printf to...

#include <Header.h> is not compiling while #include "Header.h" is compiling

Don't understand why #include <Header.h> is not compiling while #include "Header.h" is compiling with Visual Studio 2008. Am I missing something? ...

invalid conversion from ‘const void*’ to ‘const char*’ in tokoyo cabinate table database

hello, i am new to tokyo cabin ate and i compile the example program and i am getting an error can any one tell me why i get this error invalid conversion from ‘const void*’ to ‘const char*’ #include <tcutil.h> #include <tctdb.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> int main(int argc, char **argv){ TCTDB *...

How is C++ VIRTUAL function not redundant?

Possible Duplicates: Overriding vs Virtual How am i overriding this C++ inherited member function without the virtual keyword being used? I am learning C++ at this moment, but I'm not completely in the dark when it comes to programming languages. Something makes no sense to me. My understanding is that a VIRTUAL function in ...

a simple function

Hi all I need to declare a variable in Node to keep position of that node so I declare it in node.h like: std::vector<double> exPosition;(public) then I defined a simple function for getting this variable like:virtual Vector GetmyPosition (void) const=0; then in node.cc I wrote this simple function: node::GetmyPosition (void) cons...

Is there any way to prepare a struct for future additions?

I have the following struct which will be used to hold plugin information. I am very sure this will change (added to most probably) over time. Is there anything better to do here than what I have done assuming that this file is going to be fixed? struct PluginInfo { public: std::string s_Author; std::string s_Process...

optimization of access to members in c++

I'm running into an inconsistent optimization behavior with different compilers for the following code: class tester { public: tester(int* arr_, int sz_) : arr(arr_), sz(sz_) {} int doadd() { sm = 0; for (int n = 0; n < 1000; ++n) { for (int i = 0; i < sz; ++i) { ...

What's the principle of type casting?

Quoted from here: CMediaType mymt; AM_MEDIA_TYPE pmt = (AM_MEDIA_TYPE*)&mymt; Why can a CMediaType object be cast to AM_MEDIA_TYPE? Is such feature available in c? UPDATE Can someone answer it seriously what's the principle behind cast subclasses to their base classes, can I do it the other way around? UPDATE2 AM_MEDIA_TYPE/C...

Is there a way to make a C++ struct value-initialize all POD member variables?

Suppose I have a C++ struct that has both POD and non-POD member variables: struct Struct { std::string String; int Int; }; and in order for my program to produce reproduceable behavior I want to have all member variables initialized at construction. I can use an initializer list for that: Struct::Struct() : Int() {} the p...

Managing a priority queue?

I have a structure struct state{ int cur[10]; int next[10]; int priority; }; and a priority queue of these states.How can I manage the priority queue so that front element is the element with the minimum value of 'priority' ? ...