Solved

memory scope

Posted on 2002-05-08
18
229 Views
Last Modified: 2010-04-02
if i dynamically create an array within a function body, will the memory be released when the function exits automatically?

ie
void func()
{
   CMy_Class* temp_array;
   int some_size = 10;

   temp_array = new CMy_Class[some_size];

   //do some stuff
}
is the memory released or do i have a memory leak?

if so, how do i stop it? i;ve tried to use

delete temp_array;

but i get access violation errors (using ms visual studio).

thanks

cal

0
Comment
Question by:calum
  • 7
  • 4
  • 3
  • +4
18 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 6996690
It's a memory leak.

You should free it with:

delete temp_array[];
0
 
LVL 4

Accepted Solution

by:
pagladasu earned 50 total points
ID: 6996693
The memory is allocated from the programs heap. So, even when you return from the function, the memory allocated is not released. You have to take care in your function, using the delete operator to do so. However, once you return from the function, you cannot access the variable temp_array because it is not in scope.
0
 
LVL 16

Expert Comment

by:imladris
ID: 6996695
Sorry, should be:

delete [] temp_array;
0
 
LVL 16

Expert Comment

by:imladris
ID: 6996700
And, yes, pagladasu is right, you would have to do that in the function. If you want to do it later (after you have exited the function) then the function will have to pass a pointer to temp_array to the calling code so that it will still have access to it.
0
 

Author Comment

by:calum
ID: 6996745
thanks - but i get an access violation when i attempt to delete [] temp_array;

what;s wrong?
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 6996765
have you kept the destructor public?
0
 

Author Comment

by:calum
ID: 6996796
do you mean the destructor for the C_My_Class? the class def looks something like

class CCritter
{
public:

     CCritter();
     ~CCritter();
        //some methods etc
};

0
 
LVL 3

Expert Comment

by:jtm111
ID: 6996799
does CMy_Class use dynamically allocated memory and is IT being released correctly?

Programming Tip:

In your constructor, initialize dynamically allocated data members to NULL.

myobj = NULL;

In the destructor, check for NULL before you delete:

if (myobj != NULL)
  delete myobj;

0
 

Author Comment

by:calum
ID: 6996819
jeez - i don;t know. i do call delete my_obj in other places, and don;t get the access violation. it;s only when i create an array of the objects and then try to delete the array i get this access violation.

the object itself contains a linked list, which is populated later in the program, but i have destructors for the objects the list contains. there is no other dynamically created member.

how would i set my_obj = NULL in the constructor? is that a this pointer?

should i therefore test the this pointer in the destructor to make sure it;s not NULL before deletion?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:pagladasu
ID: 6996820
I think the problem is in the code that you have written for the destructor of CMy_Class.
0
 

Author Comment

by:calum
ID: 6996872
hmm - any suggestions as to what to look out for? what is the error message actually telling me?

0
 
LVL 4

Expert Comment

by:pagladasu
ID: 6996926
try compiling the program in Release mode and execute
0
 

Expert Comment

by:tsukko
ID: 6997064
Can you possibly post the errors you're getting along with the code?
0
 
LVL 1

Expert Comment

by:ilikenine
ID: 6997665
which compiler u r using ?
Is the class derived from some other class ?
Can you post the code?

-ilikenine
0
 

Author Comment

by:calum
ID: 6998505
i think i;ve solved the problem:

if i use:

CMy_Class** temp_array = new CMy_Class*[some_size];

i can happily use delete[] temp_array.

who should get the points??

cal
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6998634
Check your array bounds. If you go beyond what you allocate, and then delete, it will crash badly in visual c++. I think it overwrites a end of data marker or something the system is using to know how much you have.

Above, you allocated an array of pointers(unsigned int on windows). Is that what you wanted??



0
 

Author Comment

by:calum
ID: 6998691
yeah - i populate the array with pointers to objects, so i can sort the objects on the member varibles using qsort, since i can't get a linked list of pointers to objects to sort using the sort method.

who gets the points? you;ve all been really helpful.

0
 

Author Comment

by:calum
ID: 7001051
everyone v helpful with this topic, even though i eventually found the answer myself. hope the thread is useful. calum
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
add elements to existing standard structure 2 96
c++ syntax question 9 44
Websocket through haproxy reused connection causes "Not a valid frame" error. 4 74
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now