Teen Programmers Unite  
 

 

Return to forum top

Memory Allocation

Posted by Cortez [send private reply] at March 09, 2003, 06:02:33 AM

I just wonder what happens to the used stack after usage:

char* something(char* szInp)
{
char* adun = new char[sizeof(szInp)];
strcpy(adun, szInp);
return adun;
}

I mean, something usage like this:

cout<<something("Adun Toridas!");

Is there a way to free the allocated memory (it's in the heap, isn't it?)

Posted by buzgub [send private reply] at March 09, 2003, 06:14:14 AM

you have to assign something to a temporrary:

char* temp = something("Adum Toridas!");
cout << temp;
delete  temp;


I may well have got the delete syntax wrong; I don't do C++.
Posted by RedX [send private reply] at March 09, 2003, 08:25:31 AM

// memory leak alert \\

better use
delete [] temp;
as temp is an array and without the [] you'll only delete the first char from the array.

Posted by CViper [send private reply] at March 09, 2003, 08:43:18 AM

Actually "delete temp;" will deallocate the whole array, as it's alloceted in one block, BUT you should still use "delete [] temp;" when deallocating arrays for consistency.

Had temp been pointing to an array of classes, "delete temp;" would only have called the first destructor, "delete [] temp;" would call all destructors correctly.

Back to the inital question; you could solve the problem by writing a small class, or using std::string.

Posted by taubz [send private reply] at March 09, 2003, 09:56:04 AM

It is allocated on the heap (as you said), and not on the stack. The stack contains regularly-declared variables.

Posted by CodeRed [send private reply] at March 10, 2003, 09:42:33 AM

Can't you just say:

char* adun = szInp;

assuming szInp has previously been declared as char*
What you are doing looks more like java

Posted by buzgub [send private reply] at March 10, 2003, 09:55:46 PM

CodeRed, I believe you can't do that.

His example would create a copy of the array. Yours would create a copy of the pointer to the start of the array.

Posted by Cortez [send private reply] at March 11, 2003, 05:23:05 AM

I just wanted to know if there was a way to delete such a thing, w/o any temp value. That was a strange experience for me indeed..Thanks a lot lot guys, I too thought there's no way other than a temp pointer.

Posted by CodeRed [send private reply] at March 11, 2003, 10:47:06 AM

"His example would create a copy of the array. Yours would create a copy of the pointer to the start of the array"

Yeah but it all depends on what he wants to do with it, it would only matter if he was going to modify it and wanted to save the original array, but it looks like all he is doing is printing it out

Posted by CViper [send private reply] at March 11, 2003, 01:00:14 PM

you can do it without any (visible) temporary variable.

class proxy
{
    const char* a;
    public:
        proxy( const char* _a ) { a = a; }
        ~proxy() { delete [] a; }
    
    inline operator char*() { return a; } 
};

proxy something( const char* a )
{
   // as above, just change the return to
   return proxy(adun);
}


Typing cout << something( const char* a ); should call the operator char*() on the returned proxy class.
Posted by unknown_lamer [send private reply] at March 11, 2003, 02:47:21 PM

The C++ standard has a similar class...*drags _The C++ Programming Language_ off his bookshelf and blows the dust off* auto_ptr

#include <iostream>
#include <memory>

// ...
{
std::cout << std::auto_ptr (something ("foo"));
} 


The auto_ptr will delete itself at the end of its scope. Section 14.4.2 of _The C++ Programming Language_ discusses its full usage. Basically, only have on auto_ptr pointing to any object at once or bad things happen and when you copy an auto_ptr the original auto_ptr now points to nothing. An example (from the book):
void g (Circle* pc)
{
  auto_ptr<Circle> p2 (pc); // now p2 is responsible for deletion
  auto_ptr<Circle> p3 (p2); // Now p3 is responsible for deletion (and p2 isn't)
  p2->m = 7; // programmer error: p2.get()==0
  Shape* ps = p3.get (); // extract the pointer from an auto_ptr
  auto_ptr<Shape> aps (p3); //  transfer ownership and convert type
  auto_ptr<Circle> p4 (pc) // programmer errorL now p4 is also responsible for deletion
} 


You just use the auto_ptr like a normal pointer with those caveats in mind.
Posted by CViper [send private reply] at March 11, 2003, 04:24:56 PM

Didn't the auto_ptr<> implement reference counting? Thought I read somwhere it did... Can't find anything about auto_ptr<>'s in the STL doc's at all right now.

Posted by unknown_lamer [send private reply] at March 12, 2003, 09:42:55 AM

auto_ptr is in <memory>. It probably isn't in the SGI STL docs, but it is part of the standard. No, it does not implement reference counting. When the auto_ptr is destroyed, it deletes the pointer it contains. When you copy an auto_ptr, the original auto_ptr becomes invalid. No reference counting anywhere.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.