Memory allocation

Hello i am currently learning about the heap memory or the free store, and was wondering in what situations do you need use dynamic allocation? I understand what it is but not how it is needed.

And how do i know how much memory my program uses and how can i know how much memory available?
Can someone please give me an example, maybe in a piece of code. thanks

Last edited on
in what situations do you need use dynamic allocation?
1) you do not know how much data you will get.
2) You do not want to store things on stack. (Usually space issues)
3) You want to benefit from polymorphism. (You can do this with stack allocated mmory, but it is not pretty)
Use the heap:
-When dealing with large data types
-When data is to be accessed after a function has returned(ie: with loacl variables)

But do not forget to clean up!!!


....and for the example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int* func();

int main(){
    int* i=new int{0};
    
    cout<<"i before: "<<*i;
    delete i;
    i=func();
    cout<<"i after func: "<<*i<<" mem address: "<<i;
    *i=99;
    cout<<"i is modifying int on the heap created by func."<<endl<<"i is: "<<*i;
    delete i;
    
    return 0;
}

int* func(){
    int* heap_int=new int{9878};
    
    cout<<"heap_int in func"<<*heap_int<<" mem address: "<<heap_int;
    
    return heap_int;
}


Aceix.
Last edited on
I like this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <string>
#include <memory>

struct Person
{
    Person( const std::string &f,
            const std::string &l )
    {
        firstName = f;
        lastName = l;
    }

    std::string firstName;
    std::string lastName;
};

void showPersons( const std::vector<std::shared_ptr<Person>> &persons );

int main()
{
    // Make container for persons
    std::vector<std::shared_ptr<Person>> persons;

    // Make persons
    auto ivan = std::make_shared<Person>( "Ivan", "Ivanov" );
    auto petr = std::make_shared<Person>( "Petr", "Petrov" );

    // Add persons to the container
    persons.push_back( ivan );
    persons.push_back( petr );

    // Show persons
    showPersons( persons );

    return 0;
}

void showPersons( const std::vector<std::shared_ptr<Person>> &persons )
{
    for ( size_t i = 0; i < persons.size(); ++i ) {
        std::cout << "First Name: " << persons[i]->firstName << "\n";
        std::cout << "Last Name: " << persons[i]->lastName << "\n";
        std::cout << "\n";
    }
}
Last edited on
I haven't learned about class / vector yet so i don't understand anything of that, hehe.
But, how can this code run and still print out 55?
I haven't used
new
and should
the_heap
not be destroyed by the time it returns?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <new>


int func(void)
{
    int a = 1; int b = 2; int c=3; int d=4; int e=5;
    int five = 55;
    int* the_heap = &five;
    return *the_heap;
}

void funk(void)
{
    int myInt = func();
    std::cout << myInt << std::endl;
}

int main()
{
    funk();
}

Last edited on
You are returning by value:
1
2
3
4
5
6
7
int func() //Returns by value
{
    int a = 1; int b = 2; int c=3; int d=4; int e=5;
    int five = 55; //Declaring a variable
    int* the_heap = &five; //Saving pointer to it
    return *the_heap; //Getting value by this pointer and copy-return it
} //the_heap is destroyed here, but we already copied its value 
Is it not the same as this?
This is what my book says about this code:
The following code segment compiles without error but doesn’t work (don’t
you just hate that?):


And i have been trying to understand why for days and it seems like the same above?

1
2
3
4
5
6
7
8
9
10
11
double* child(void)
{
 double dLocalVariable;
 return &dLocalVariable;
}
void parent(void)
{
 double* pdLocal;
 pdLocal = child();
 *pdLocal = 1.0;
}
Last edited on
No it is not. Here child returns pointer, not value.
And you can't just copy that address to parent()?
You do copy address. But now it points to nonexisting variable
1
2
3
4
5
6
7
8
9
10
11
12
double* child(void)
{
 double dLocalVariable; //Createing variable
 return &dLocalVariable; //REturning pointer to it
} //dLocalVariable is destoyed. All pointers to it (including returned one) are invalid

void parent(void)
{
 double* pdLocal;
 pdLocal = child(); //child() returns invalid pointer
 *pdLocal = 1.0; //Undefined behavior: We are trying to write to the nonexisting variable
}

Thanks, makes more sense now, i'm gonna bookmark this page. ^^
Topic archived. No new replies allowed.