Jump to content

[C++] dynamic allocation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
armon

armon

    Newbie

  • Members
  • Pip
  • 2 posts
Hello,

I was playing with classes and i met with something very interesting

I wrote something like that
void tester()

{

     NameOfClass *new_guy = new NameOfClass [5];

     new_guy -> setSomeKindOfValue(500);

     cout << new_guy ->getSomeKindOfValue() << endl;

     new_guy++;

     new_guy ->setSomeKindOfValue(400);

     cout << new_guy -> getSomeKindOfValue() << endl;

     

     //new_guy --; if i do not comment this place, destructor will be invoked forever...

     

     delete [] new_guy ;  

}


so i thought about something like that:

void tester()

{

     int rozmiar = 0;

     char * k= new char[5];

     

     k[0] = 'a';

     k[1] = 'b';

     k[2] = 'c';

     k[3] = 'd';

     k[4] = '\0';


     while(*k++)

       rozmiar++;

    

          

     delete []k;  //so will it free reserved memory or not?

}


Of course programs are written just to show you what i meant...

So how do you think? If i want to free memory, should the pointer point to the beggining of the dynamic table?

Best regards :)

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Yes, when freeing memory the pointer should always point to the beginning of the dynamic table. In your first example, I don't particularly like it as it is, but to expand on it, use a separate iterator:
[highlight=C++]void tester()
{
NameOfClass *new_guy = new NameOfClass [5];
NameOfClass *iter_ptr = new_guy;
iter_ptr -> setSomeKindOfValue(500);
cout << iter_ptr ->getSomeKindOfValue() << endl;
iter_ptr++;
iter_ptr ->setSomeKindOfValue(400);
cout << iter_ptr -> getSomeKindOfValue() << endl;

//new_guy --; if i do not comment this place, destructor will be invoked forever...

delete[] new_guy;
new_guy = NULL;
iter_ptr = NULL;
}[/highlight]
However a loop would also be preferable, instead of this use of an iterator. If you must use an iterator, there's some great STL language constructs you can use to contain object pointers, which allow for easier iteration.

And yes, the second will free the reserved memory, but I'd write it like "delete[] k;" instead.
Wow I changed my sig!

#3
armon

armon

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for answering me...

Okay i understand it now, but why do you say that in second function where i wrote delete[] k will free memory... i incremented it to point to almost end?

And i noticed you bolded delete[] k; is that different from delete []
k?


Yea i know there are some other good things to use, but i just want to understand it better :)

Oh and if my apllication end (not local scope), all memory that i reserved by new type[number]; will be freed automatically right?

Best regards :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You should not be changing the value of k while it points to an array. That's just a bad idea. You should not step off the end of your allocated memory (that's also just bad). You would do better to have a second pointer that you use to step through k.

If I was your teacher and saw this, I'd be docking points like crazy. If I was your employer, you'd get an informal reprimand the first time I saw that, and a formal one the second.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts

Quote

Oh and if my apllication end (not local scope), all memory that i reserved by new type[number]; will be freed automatically right?

On modern operating systems, yes. Still, you should get into the habit of freeing everything you allocate.
sudo rm -rf /