By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

simple memory allocation question

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
jason wrote:
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

--
Pull out a splinter to reply.
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
jason wrote:
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
Jul 23 '05 #4

P: n/a
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.

Jul 23 '05 #5

P: n/a
jason wrote:
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.
Jul 23 '05 #6

P: n/a
jason wrote:
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.

--
Lionel B

Jul 23 '05 #7

P: n/a
ben
The normal approach here is to use a data structure like std::vector,
which handles details of growing for you.
is std::deque better?

ben

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

--
Pull out a splinter to reply.

Jul 23 '05 #8

P: n/a
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.

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.