Re: A better BinaryConverter function in C++



Gov <prabha.govind@microsoft(.)com> wrote:
I have this code for a binary converter function in C++...can u
suggest better way of doing it or improvements in this code...I guess
there might be better way of doing the same

string ConvertToBinary(string Msg)
{
const char * pCMsg=new char[Msg.size())];
pCMsg=Msg.c_str();

You seem to believe these two lines copy the string into a newly
allocated memory block. They do nothing of the sort. What happens is you
allocate a block of memory and store its address in pCMsg pointer. You
then promptly forget this address and overwrite it with Msg.c_str(). As
a result, pCMsg points to the string's internal storage (no copy is
made), and the allocated memory is leaked.

const char * pPath=new char[Path.size()];
pPath=Path.c_str();

Same here.

for(int i=0;i<Msg.size();i++)
{
if(pCMsg[i] & 0x80) //0x80=10000000
pMsg=1;else pMsg=0;BFile<<pMsg;
[snip]
if(pCMsg[i]& 0x1) //0x1=00000001
pMsg=1;else pMsg=0;BFile<<pMsg;

You can make it

std::bitset<8> bs(pCMsg[i]);
BFile << bs;

This converts one byte at a time.

delete pCMsg;
delete pPath;

Here you are deleting memory you didn't allocate, memory that belongs to
strings Msg and Path. If you are lucky, you'll crash immediately. If you
are unlucky, these statements will corrupt the heap but allow the
application to continue execution, and you will crash hours later in a
seemingly unrelated piece of code. Good luck debugging _that_ one.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: C API and memory allocation
    ... must allocate new memory for the returned string. ... nothing in the API that provides for freeing that allocated memory ... should not attempt to free this buffer. ...
    (comp.lang.python)
  • Re: "".join(string_generator()) fails to be magic
    ... beforehand so intermediate strings must get moved around in memory while ... concatenating. ... all but the last string are already ... concatenated and the last one does not fit into the allocated memory ...
    (comp.lang.python)
  • Re: Voting for FastCode rules on memory reading
    ... > because the MM allocated memory for strings up to ... > memory allocated for a string. ... That depends on the memory manager. ... that the last DWORD of the string is also used for something else. ...
    (borland.public.delphi.language.basm)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)