dynamic Memory and pointers

Hello! I've got some questions. I'm new in C++ and don't know much about technical aspects.

1. For example there is my little function:

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
#include <vector>
#include <cstdio>

typedef vector<double> dvector;

dvector* funcValue(dvector &x)
    {
        dvector* y;
        y = new dvector(x.size(),0.0);
        /* some code to get values  
           and put `em into y */
       return y;
    }

int main(int argc, char** argv)
    {
       dvector* x;
       dvector* f;
       int i=10,j;
       x = new dvector(i);
       for (j=0; j<10; j++)
           {
               f = funcValue(*x);
           }
       return 0;
    }

Q: funcValue returns a pointer so I believe that memory allocated in function is occupied by my program. Then I do another call of funcValue and new block of memory is allocated, but previous is not destroyed. I'm afraid that every call I allocate new block of memory and sooner or later I consume all available one.
Am I right? Or compiler makes code in such way that blocks are destroyed?



2.
Look here, there is my little code (don't laugh at me, please):
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
48
49
50
51
52
53
54
55
56
57
58
59
#include <vector>

#define _gradF_Dx 1.0
#define _gradF_Dh 2.0

typedef std::vector<double> dvector;

dvector* gradF(dvector &x, double (*funcv)(dvector&));

double funcValue(dvector &x);
    
int main(void)
    {  
        dvector* x = new dvector(3,0.0);
        dvector* g;
        x->at(0) = 0.0;
        x->at(1) = 0.0;
        x->at(2) = 0.0;
        g = gradF(*x,funcValue);
        
        printf("%.12f %.12f %.12f\n",g->at(0),g->at(1),g->at(2));
        
        return 0;
    }
    
dvector* gradF(dvector &x, double (*funcv)(dvector&))
    {  
        dvector* g;
        dvector* tmp1, *tmp2;
        g = new dvector(x.size(),0.0);
        tmp1 = new dvector(x.size(),0.0);
        tmp2 = new dvector(x.size(),0.0);
        *tmp1 = x; *tmp2 = x;
        
        for (int i=0; i<x.size(); i++)
            {
                tmp1->at(i) += _gradF_Dx;
                tmp2->at(i) -= _gradF_Dx;
                g->at(i) = ((*funcv)(*tmp1) - (*funcv)(*tmp2))/_gradF_Dh;    
                tmp1->at(i) = x.at(i);
                tmp2->at(i) = x.at(i);;
            }
            
        delete tmp1; delete tmp2;
        return g;
    }
    
double funcValue(dvector &x)
    {
        double y;//=0.0;
        
        for(int i=0; i<x.size(); i++)
            {
                y += x.at(i)*x.at(i);
                printf("%.12f  ",x.at(i));
            }
        printf(" | %.12f\n",y);
        return y;
    }

Hm... If I compile and run my code like this ( value is not assigned to y in funcValue ), I will get:
1.000000000000 0.000000000000 0.000000000000 | 1.000000000000
-1.000000000000 0.000000000000 0.000000000000 | 2.000000000000
0.000000000000 1.000000000000 0.000000000000 | 1.000000000000
0.000000000000 -1.000000000000 0.000000000000 | 2.000000000000
0.000000000000 0.000000000000 1.000000000000 | 1.000000000000
0.000000000000 0.000000000000 -1.000000000000 | 2.000000000000
-0.500000000000 -0.500000000000 -0.500000000000
It means that y "remember" its value every second call. Take a look at code -

(*funcv)(*tmp1) - (*funcv)(*tmp2) - this is the only line with funcValue calls. So, I believe that I've got a problem with function pointer.
Could someone clarify the work of function pointers? Why it remembers itself inside gradF and "forgets" outside?

P.S. I'm using MinGW g++ 3.4.5 from command line
Last edited on
Firstly, lets look at some problems with your code.

1) Your creating your vectors dynamically. Which is fine, but almost pointless because if your trying to save memory you could create a vector OF pointers.
2) Your not freeing all of your memory correctly, Both x and g are never deleted.

I think you need to re-think your solution.
Topic archived. No new replies allowed.