Re: Stl map and CString trouble





Scoots schrieb:
" Why do you need pointers for the map and the bytes?"
the map is on the heap, so I need a pointer to it. The bytes are
also
an allocated chunk on the heap, and so I need the addresses for an
application specific task.

It is unusual to allocate a standard container on the heap. The the container's data is always allocated on the heap no matter where the container object is. By putting the container itself on the heap you do add addiatinal work to free the memory andkeep track of correct deallocation. But all that should not make any difference to your problem.

I tried this little app:

--------
#include <iostream>
#include <string>
#include <atlstr.h>
#include <map>

using namespace std;

typedef unsigned char byte;
map<CString, byte*> g_VarMap;

int main()
{
g_VarMap["AA"] = (byte*)0x003a9e0b;
g_VarMap["BB"] = (byte*)0x003a9e1b;
g_VarMap["XX"] = (byte*)0x003a9c95;
g_VarMap["YAR"] = (byte*)0x003a9c96;
g_VarMap["YY"] = (byte*)0x003a9c94;

map<CString, byte*>::iterator i = g_VarMap.find("YAR");

if (i != g_VarMap.end())
{
cout << i->first << endl;
}
return 0;
}
--------

and it works fine for a ANSI build, so the problem is somewhere else.

Are you using MFC in some parts of your app? MFC CString and ATL CString used to be two different classes.


Anyway, how do you assign to the map?


It's over several hundred to thousand lines of parsing a file, but it
boils down to:

int MapVar(string p_pszName, char* p_pszType)
{
//If bool, update boolspace vars.
if (0 == strcmp("BOOL",p_pszType))
{
if (g_pbCurrentBoolSpace + sizeof(bool) >
(bool*)g_piCurrentIntSpace) //this also handily catches if it's
already maxed!
{
//snip
return 0;
}
(*g_VarMap)[p_pszName] = (byte*)
(g_pbCurrentBoolSpace);
g_pbCurrentBoolSpace += sizeof(bool);
}


//If int, update int vars.
if (0 == strcmp("INT", p_pszType))
{
if (g_piCurrentIntSpace + sizeof(int) >
(int*)g_pfCurrentFloatSpace) //this also handily catches if it's
already maxed!
{
//snip
return 0;
}
(*g_VarMap)[p_pszName] = (byte*)
(g_piCurrentIntSpace);
g_piCurrentIntSpace += sizeof(int);
}


//If float, update float vars.
if (0 == strcmp("FLOAT", p_pszType))
{
if (g_pfCurrentFloatSpace + sizeof(float) >
(float*)g_VarSpaceMax) //
this also handily catches if it's already maxed!
{
//snip
return 0;
}
(*g_VarMap)[p_pszName] = (byte*)
(g_pfCurrentFloatSpace);
g_pfCurrentFloatSpace += sizeof(float);
}
return 1;



}
[...]


I prefer to use CStrings if I can, for the utility and the unicode
support.

Well, the code you show uses char*, std::string and CString all in one function. That is neither unicode aware nor "prefer to use CStrings"....

Norbert
.



Relevant Pages

  • Re: Huge pages and small pages. . .
    ... >> what looks like contiguous memory and away you go. ... you need to have them cached in the TLB; if the TLB runs out of ... > low end of the heap, until someone figures out a way to tell the system ... When you allocate memory, the kernel just marks a promised ...
    (Linux-Kernel)
  • Re: Struct inside class
    ... The compiler figures computes the ... including anything derived from "Object": Heap pointer ... memory leaks and why the .NET good garbage collector is required. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Re: Struct inside class
    ... From the previous discussion, we know that i1 is a member variable of a strunct in a class, so it's stored on the heap. ... but inside the allocated space for the reference type instance. ... memory leaks and why the .NET good garbage collector is required. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)