Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resize a memory buffer without disturbing contents

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
0
0
US
Is there an easy way to resize a memory buffer without disturbing the contents of it?

For instance, if I have char membuff[40], and I copy 20 chars into it, how can I trim the buffer afterwards to a size of 20?

Thanks for your time.
 
Well in such a situation allocate memory from heap using malloc(). And then you can realloc() at a later time wih the changed size.

Hope this helps.
 
use something like that

char *newbuff =(char) malloc(strlen(membuff)+1);
strncpy(newbuff,membuff,strlen(membuff));
free(membuff);

bye
 
Nope the strncpy will not work in case there is a '\0' in the memory space to be copied. In case you want to allocate another memory chunk and copy then use memcpy(). This does not stop on encountering '\0'.

Hope this helps.
 
Some addition: if we use malloc, use realloc (ANSI portable function). Then
Code:
char* membuff = malloc(bufsz=40);
...
membuf = realloc(membuf,bufsz+=morebytes); // no need of memcpy
...
free(membuf);

Don't forget to trace current bufsize value (yet another var). And don't mix
Code:
operator new
and
Code:
malloc/realloc
allocations. And don't forget: realloc returns new pointer to new buffer (with old content).
 
Sorry, we must code
Code:
(char*)malloc
in C++, of course. But it was so native nostalgic C style...
 
So there's no way to do this if I don't declare things with malloc?

It seems rather odd that there's no purely C++ counterpart.
 
Can I use a vector?

The main point of contention about using a vector for this would be if I can point at it as if it were a char buffer with a char pointer. I realize that a vector is a container, but if I do vector <char> v, could I then do a
char *p;
*p = &v;
?

Could I do a memcopy out of a vector into something else? IE:

char test[5];
vector <char> v;
v.pushback(&quot;Hello&quot;);
memcopy(&test[0], &v[0], 5);

Would this work?
 
use a std::list. You can add/remove without disturbing or moving any element in the memory. When using realloc, pointer can move if there is not enough place for it. In the same way can work vector. So you can use vector only when you need byval accessing. If you use references and/or pointers to stored objects then use list.

Ion Filipski
1c.bmp
 
I don't understand what's a problem? If you don't want to use malloc/free (why? it's part of C++ library), write your own one in C++ style: it's so easy:
Code:
class Chunk
{
  char* pmem;
  unsigned   msz;
public:
  Chunk(unsigned sz = 0);
  ~Chunk() { delete [] pmem; }
  char* ptr() { return pmem; }
  unsigned size() { return msz; }
  char* realloc(unsigned newsz);
  operator char*() { return pmem; } // if you wish...
}

Chunk::Chunk(unsugned sz):msz(sz)
{
   pmem = new char[sz];
}

char* Chunk::realloc(unsigned newsz)
{
  char* pnew = new char[newsz];
  memcpy(pnew,pmem,newsz<msz?newsz:msz);
  msz = newsz;
  return pmem = pnew;
}
Add any decorations and parameter chech code if you wish.
No need in STL containers if you want a simplest expandable char buffer...
That's why C++ has no any realloc...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top