simple memory allocation question

J

jason

i'm a little new to VC++, so i'm curious how to appropriate perform the
following task.

there is a pointer which i wish you point to a buffer of frequently
changing size. i'm wondering what is the proper way to initially
allocate, and then grow that allocation?

some sample code that might illustrate my intentions:

DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
{
if (ptr == NULL)
{
//initial allocation here
ptr = new char[] // like so?
}
//grow the allocation based on the size of stufftoappend here?
}

thanks in advance for any help,

jason
 
P

Peter Ammon

jason said:
i'm a little new to VC++, so i'm curious how to appropriate perform the
following task.

there is a pointer which i wish you point to a buffer of frequently
changing size. i'm wondering what is the proper way to initially
allocate, and then grow that allocation?

some sample code that might illustrate my intentions:

DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
{
if (ptr == NULL)
{
//initial allocation here
ptr = new char[] // like so?
}
//grow the allocation based on the size of stufftoappend here?
}

thanks in advance for any help,

jason

The normal approach here is to use a data structure like std::vector,
which handles details of growing for you.

If you really want to roll your own, you can use malloc() to create the
buffer and realloc() whenever you need to grow it. new doesn't have an
equivalent to realloc().

-Peter
 
J

jason

ok, thanks for clarifying that Peter. yeah, i would love to use a
vector, but the pointer is a little picky about what it can hold, and
isn't something i can change. however, the malloc/realloc info is
exactly what i was missing. thanks for stopping me from barking up the
new/delete tree.

jason
 
V

Victor Bazarov

jason said:
i'm a little new to VC++, so i'm curious how to appropriate perform the
following task.

If your question is VC++-specific (like language extensions, compiler-
specific or 'implementation-specific' behaviours, limitations, bugs, etc.)
then you better post to microsoft.public.vc.language. If your question is
about C++ in general, as a language, then comp.lang.c++ is just as good
(or even better in some cases).
there is a pointer which i wish you point to a buffer of frequently
changing size. i'm wondering what is the proper way to initially
allocate, and then grow that allocation?

some sample code that might illustrate my intentions:

DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
{
if (ptr == NULL)
{
//initial allocation here
ptr = new char[] // like so?

No, definitely not like so. You pass the pointer by value, there is no
way you will retain any changes to it. You need to pass it by a reference
to a pointer. And if you need an array of 'char', why is your pointer
declared 'void*'? Shouldn't it be 'char*'?
}
//grow the allocation based on the size of stufftoappend here?

The C++ language doesn't have any "grow the allocation" mechanism, you
need to "roll your own":

else {
char *newptr = new char[oldsize + howmuchtoadd];
std::copy(ptr, ptr + oldsize, newptr);
std::copy(stufftoappend, stufftoappend + howmuchtoadd,
newptr + oldsize);
delete[] ptr;
ptr = newptr;
}

That all said, you probably should use 'std::string' or 'std::vector' for
your containment needs instead of managing dynamic memory yourself (given
that you're a beginner, that is).


V
 
J

jason

much appreciated. especially the code sample at the bottom.

yeah, i see now the value/reference mistake in my sample.

i have some experience with std::vector, not much with string. if the
pointer won't complain about that type, i will definitely use it.
thanks again.
 
P

Phil Endecott

jason said:
i have some experience with std::vector, not much with string. if the
pointer won't complain about that type, i will definitely use it.
thanks again.

Maybe you need std::string::data() or std::string::c_str()?

Or is it complaining due to constness?

--Phil.
 
L

Lionel B

jason said:
ok, thanks for clarifying that Peter. yeah, i would love to use a
vector, but the pointer is a little picky about what it can hold, and
isn't something i can change.

What exactly is the problem with using std::vector (or std::string, since you seem to be allocating chars)? It would
make your life much easier to use existing containers that handle memory for you - thus it may well be worth your while
to resolve type mismatch issues... post some code and perhaps we can help.
 
J

jason

thanks for the offer! however i think in this case i'll just use a char
array. i'm having to do enough pointer math, that managing all of that
through iterators would be more difficult for me than something that i
can guarantee is contiguous.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top