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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reallocate with new operator?

Status
Not open for further replies.

RbgoWeb

Programmer
Apr 10, 2002
91
NL
With function realloc() I can resize a dynamically allocated array like:

//Initial array
long length = 100;
long* larray = (long *)malloc(sizeof(long) * length);


//resizing array
length += 25;
larray = (long *)realloc(larray, sizeof(long) * length);


How can I do something similar with new operator?

 
Code:
long* realloc_long_array( long* ptr, long nlen, long nNewLen){

	assert( nNewLen > nlen);
	long* pret = new long[nNewLen];
	memset(pret, 0L, sizeof(long)*nNewLen);
	memcpy(pret, ptr, nlen);
	delete [] ptr;
	return pret;
}

-pete
 
Thank you palbano for your effort.

What is nice about realloc() is that it resizes a memory block and only moves it when it is necessary. realloc() returns NULL in two cases:
realloc() returns NULL when the specified size is NULL, in which case the allocated block is freed; this is a good thing. (Although _msize() still says it has a size which is a bad thing.)
realloc() also returns NULL when there is insufficient memory to resize a memblock, in which case the original block is untouched; this is a very good thing too.

I am hoping to see at least something similar can be done with the new operator. Anyone?

(Till now I have only used new for dynamically allocating/instanciating classes.)
 
Ok, now that you changed your question i will change my answer: No

-pete
 
You can overload a version of new that operates on a chunk of memory you acquire and manage, then write a resize function to resize an allocated block from that chunk.

Of course, that's essentially rewriting new.


std::deque only allocates more memory when it needs it, too. It admittedly doesn't do much about freeing that memory, though. Feel free to write your own container to do exactly what you think is right for the situation.

No comment on resizing to 0. No comment on _msize.

operator new throws an exception when it can't find enough memory. This is a better thing since you don't have to propogate the error code up manually in order to get it somewhere you can do something about it.
 
Thanks for help and filling me in on this.
I somehow expected operator new to have more advanced
features since it is part of C++ and so 'newer' than C.
 
Newer doesn't always mean better or even easier to use. For instance, Cobol has a feature called MOVE CORRESPONDING which does the equivalent of this in one statement.
Code:
struct{
   int a;
   int b;
   int c;
} ABC;

struct {
   int a;
   int c;
} AC;

ABC.a = AC.a;
ABC.c = AC.c;

In Cobol, it would just be

MOVE CORRESPONDING AC TO ABC
Algol 68 and Fortran 77 have string slicing features that still don't exist in C++ (or C99). You have to use memcpy.

In Algol 68, you could use a const POD type declaration as a parameter but you still can't do that in any of the modern languages. Equivalent of
Code:
VectorMult({1,2,3},{1,2,3},vectorProd);
If there are too many features, it would end up like PL1 or Perl - it would be so big that even 'experienced' programmers would have difficulty understanding it.
 
If it got too many features, it might even end up being like C++. Oh, wait...


If you run a Google search, I know there's an article out there about why there's no "renew" operator. Check it out if you're curious.
 
XWB, maybe those features are to high-level to be part of C. C is explained to me also as a high level assembler; it is one step above assembly language. What is nice about it is that you can build your own hi level features (functions) that are open for improvements in the future and can be shared(open source), while not being forced to use a language build in implementation which can be great, but also could be better.

chipperMDW, found a page that could be it...
thanks for that branch of more information
 
There is a reason for new operator to not be able to reallocate memory again. I am not able to recall but there is a good article on it. The reason for no realloc in C++ except the standard C realloc, is because, new has to call the constructors of the class in case u allocate class objects. Which cause problems when u reallocate. I think new suck and i always use malloc due to this :)
 
>> I think new suck and i always use malloc due to this :)

Wow, could you explain that for me a little? So you malloc your Objects? How do you get the constructor to run, you call it your self using the offset? That would not be portable across compilers would it? Also it's a little like reinventing the wheel isn't it?


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top