473,753 Members | 7,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple memory allocation question

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::MyEve nt(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
8 1570
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::MyEve nt(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
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
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.publi c.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::MyEve nt(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(stuff toappend, 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
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
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::da ta() or std::string::c_ str()?

Or is it complaining due to constness?

--Phil.
Jul 23 '05 #6
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3277
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that, no memory is lying around. GC reports only 84k of allocations. Starting 5-10 of this apps is going to start taking a considerable amount of memory. Is there a way to reduce this? Tom
16
2020
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body of the destructor. So I think I am somehow using delete incorrectly, but I'm not sure exaclty what I'm doing wrong. I'd apprecitate any clarification and suggestions for rewriting the below properly.
13
5740
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list of "cells" inspired by the one in Plauger's text
11
3400
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
8
5111
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
10
1840
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it didn't work; then he installed 2.0 and it still didn't work; so he tried with 2.1 and it didn't work, then 3.0 and nothing still worked. I have Intel Centrino Mobile (laptop computer), and he has Intel Pentium...
30
3543
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
4425
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both the allocation is impossible. Sworna vidhya
8
3167
by: Chris M. Thomasson | last post by:
Here is the initial crude implmentation which compiles under Comeau with no warnings: ___________________________________________________________________ #include <cassert> #include <cstdlib> #include <new> #include <list> template<typename T, std::size_t BUFDEPTH = 1024>
0
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9421
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4771
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.