You can use hash_table which is part of stdext. It's not part of standard template library but will work with visual studio. http://www.sgi.com/tech/stl/hash_map.html[^]
Budric B
Posts
-
How to create a HashTable in C++? -
MSVC++ 2005, Project Dependancies and unresolved externals [modified]Hi, I'm having some trouble using classes defined in one project (compiled as exe) in another project (also to be compiled as exe, to run in a separate process, but part of the same solution). Is it possible at all? Or do I have to import all the .cpp files into the other project and compile them there? I've set up a project dependency, but that doesn't work. I get unresolved externals during compilation. Note, I've also set up project references.
modified on Thursday, July 17, 2008 12:45 PM
-
Reference are always lvalue?I suppose if you're returning a reference from a function then it's not an lvalue.
-
ifstream slow to closeHi, I'm getting some terrible performance when calling ifstream close. It takes about the same amount of time to close() as it takes to read my 64 Mb of data! I've been playing around with it and found it only happens if I have been writing to a file recently. Here's my test code:
void readTest(const std::string & path)
{
ifstream in;
in.open(path.c_str(), ios_base::in | ios_base::binary);
char * buffer = new char[67108864];
in.read(buffer, 67108864);
SW_ELAPSED_MSG()
in.close();
SW_ELAPSED_MSG()
delete [] buffer;
}void writeTest(const std::string & path)
{
ofstream out;
out.open(path.c_str(), ios_base::out | ios_base::binary);
char * buffer = new char[67108864];
out.write(buffer, 67108864);
//out.flush();
out.close();
delete [] buffer;
}int _tmain(int argc, _TCHAR* argv[])
{
writeTest("testfile");
SW_START();
readTest("testfile");
SW_ELAPSED_MSG()
return 0;
}Note my macros do the timing and display it on screen. I don't believe there's any problem with that code. So anyone know why? I'm closing the streams. Why is the read stream affected at all by the write stream? Also as you can see I have a flush() statement in there. It doesn't help, commented or not.
-
operator << inherited?It appears that when I remove the reference operator, I don't have to specify "using". It's a shame though as I was hoping to subclass an existing implementation and just add a few of my own << operators to write my own types. Looks like I'll have to try defining the operators outside of the class.
-
operator << inherited?Hi, I was wondering if the << operator is inherited? I can't get the following to work:
class A
{
public:
A& operator << (double & arg)
{
return *this;
}
};
class B : public A
{
public:
B & operator<<(int & arg)
{
return *this;
}
};
...
B test;
test << 5.0;with the error that no operator that takes double as argument.
-
Fast serializationIt doesn't have that much behavior other than a constructor and a destructor to free the memory. The reason I was doing it this way was because I would be adding more data members to describe the data later. But you're probably right that it wouldn't add that much performance loss to write each data member. I'll have to time it and see. I've made a workaround where I allocate 2 buffers, 1 for the object and one for the data, the rest of the code stays the same. That doesn't trigger the assertion. However I don't know if the object's memory gets freed or not since I'm still calling delete Voxels and not delete [] voxels;
-
Fast serializationNo, I first write the object, which contains the data members and a pointer at the end. The size of the object is sizeof(Voxels). I then write the array of stuff. When I read, I read everything. I know that the first sizeof(Voxels) bytes is the object including a pointer with a value that's meaningless. I then assign the pointer to point to the data, which just happens to be right after the object (because that's where I read it into).
-
Fast serializationI see...even though it's an object? I know I'm casting an array to an object, but I would have thought things would behave as they would for any object created with new.
-
Fast serializationNo, the pointers does not get moved. Voxels is a class and it gets returned from the loadVoxels() function. The problem is when I call "delete Voxels;" the destructor is deleting the member array and that's when the assertion fails.
-
Fast serializationHi, I need very fast serialization to disk of a large object (64 MB/128 MB etc). Serializing members one by one is not an option (Boost serialization for example is very slow), and the object does need to be shared across systems so I don't need to worry about endianness. Here's what I came up with:
void saveVoxels(Voxels * voxelData, const char * fileName) throw(...)
{
using namespace std;
ofstream file (fileName, ios::out | ios::binary);
if (file.is_open())
{
file.write((char *)voxelData, sizeof(Voxels));
unsigned int size = voxelData->m_sliceWidth * voxelData->m_sliceHeight * voxelData->m_numSlices * sizeof(unsigned short);
file.write((char *)voxelData->m_pTexture, size);
file.close();
}
}
Voxels* loadVoxels(const char * fileName) throw(...)
{
using namespace std;
ifstream file(fileName, ios::in | ios::binary | ios::ate);
if (file.is_open())
{
unsigned long size = file.tellg();
char * memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();//initialize Voxels \* voxelData = (Voxels \*) memblock; voxelData->m\_pTexture = (unsigned short \*) (memblock + sizeof(Voxels)); return voxelData; }
}
Which as far as I can tell works. However when deleting the object I get _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) assertion error. I guess I didn't really allocate the m_pTexture pointer which is what gets deleted...Any solutions?
-
Windows XP Memory allocation questionsthanks for the heads up on the flag, I'll try it with my code.
-
Windows XP Memory allocation questionsHi, in the process of running my program is allocating a lot of memory (gigabytes). After it reaches a certain size strange things start to happen so I'm trying to debug it. I have some questions. 1. To my surprise new operator does not return NULL on error. It seems to throw an exception. When did this happen? What's the exception object? I'm using VS 8. 2. Is new a thread safe operator? What if I'm using some libraries that most likely use malloc. Is memory allocation still thread safe? I'm using dynamic link run time for my program and hopefully all the libraries. If some library is using a different run-time but I don't free its memory is that fine? 3. What is the memory allocation limit for a process in Windows XP? Is it 2 GB? Or RAM + swap space? 4. Any free GUI profilers out there? Thanks for your help.
-
Including winsock2.h problemI spent way too much time on this problem, that's why I know ;)
-
Including winsock2.h problemNo, in fact you need to include winsock2.h before windows.h because windows.h includes old winsock.h which interferes with the new one. Also winsock2.h may include windows.h anyway on line: #ifndef _INC_WINDOWS #include #endif I will check out the BSD sockets thing...thanks.
-
Including winsock2.h problemFound the problem, I called one of my variables "far" which turns out to be #defined somewhere Still open to suggestions on portable socket library. I just don't want to mess around with it.
-
Including winsock2.h problemHi, I have a project I'm working on using GLUT, openGL and a few libraries. Now I'm trying to add sockets to it and whenever I include winsock2.h (haven't even written any relevant code) syntax error : ',' at some line which is meaningless appears. The code compiles and runs if I don't include winsock2.h I'm using VC++ 2005. I have ws2_32.lib set up to link. I'm not including windows.h, MFC support. I just can't figure out what's wrong. Example code for winsock compiles and works. As an aside can anyone point to a cross platform sockets library? I think GTK has one but it's way too big.
-
Windows - check if file/dir is in useHi, I would like to check if a file or a directory is in use in windows XP. Basically if you open up explorer navigate to some folder, then open another explorer window and try to erase that folder you'll get an error that the folder could not be removed. Unfortunately if you're in some subfolder the delete on parent operation will erase some folders, then hit the problem folder and stop. That's terrible, because you've erased half the stuff before you find out there's a problem. I want to be able to check if anyone's using any of the files/folders, if not then erase them programatically (possibly get a lock so no one can open it while I'm checking the other files). Any help? It must be possible because of programs like FileMon. But I can't get the source code for it.
-
Image Subtraction questionHi, I have a simple question for those with experience. A part of an algorithm describes to do a difference between image, A-B. My question is what if A[i, j] < B [i, j] for one or more pixel i, j? Negative intensity values are meaningless. Is there a standard definition for image subtraction? I can't seem to find an answer to this simple question. Thanks.
-
Check if a thread is still runningYes, that's what I use. Taken from the site:
// thread body: while(running) { /* loop */ if(paused) switch(::WaitForSingleObject(event, time)) { case WAIT_OBJECT_0: break; case WAIT_TIMEOUT: continue; } // rest of thread } /* loop */
except instead of while(running), I have a for loop from 0 to numImages.