Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple memory allocation question

Reply
Thread Tools

simple memory allocation question

 
 
jason
Guest
Posts: n/a
 
      06-23-2005
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

 
Reply With Quote
 
 
 
 
Peter Ammon
Guest
Posts: n/a
 
      06-23-2005
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.
 
Reply With Quote
 
 
 
 
jason
Guest
Posts: n/a
 
      06-23-2005
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-23-2005
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
 
Reply With Quote
 
jason
Guest
Posts: n/a
 
      06-23-2005
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.

 
Reply With Quote
 
Phil Endecott
Guest
Posts: n/a
 
      06-23-2005
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.
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      06-24-2005
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

 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      06-24-2005

> 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.



 
Reply With Quote
 
jason
Guest
Posts: n/a
 
      06-24-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
a simple problem about memory allocation Richard Bos C Programming 6 11-09-2003 09:58 AM



Advertisments