Memory allocation problem

Dear all,

I'm studying C++ and now I started implementing a code to solve some numerical methods.
I created a Matrix class (2D with contiguous memory allocation) and I'm using it in some functions. However, the class which uses the Matrix class provides an array with the first element zero. I suppose it is an array decay problem.

The code is available here: https://github.com/nunesanderson/FEM_CPP.git and the problem is presented in the src/main.cpp file.

The expected result is:
1
2
3
4
5
6
7
8
9
10
11
12
13
 double one_six = 1.0 / 6.0;
    double two_three = 2.0 / 3.0;

    Matrix<double> ans(3, 3);
    ans.mat[0][0] = one_six;
    ans.mat[0][1] = one_six;
    ans.mat[0][2] = 0;
    ans.mat[1][0] = two_three;
    ans.mat[1][1] = one_six;
    ans.mat[1][2] = 0;
    ans.mat[2][0] = one_six;
    ans.mat[2][1] = two_three;
    ans.mat[2][2] = 0;

Output:
0.166667 0.166667 0
0.666667 0.166667 0
0.166667 0.666667 0

but the GaussLegendrePoints points(9) returns:
0 0.166667 0.166667
0.666667 0.166667 0
0.166667 0.666667

The GaussLegendrePoints points(9) calls the function GaussLegendrePoints::triangleThreePointsInside() which has the same code shown in the beginning.

As I did not manage to find the problem, I was wondering if someone in the forum could help.
Furthermore, as I don't have experience with C++ any comment about how to improve the code is very welcome.

Thank you in advance,
Anderson
Last edited on
here you are https://github.com/ne555/FEM_CPP.git

problems in Matrix.cpp
- your default constructor creates a 0x0 matrix, but Matrix_alloc tries to access mat[0]
- you are missing a proper copy constructor and assignment operator, and there are errors in your destructor
I would suggest you to use std::vector instead, .data() will give you a pointer if you needed for an external library
(will have to add a operator() to access in mat(r, c) fashion)


(not fixed)
your makefile is not detecting the dependencies correctly
for example, a change in Matrix.h should send main.cpp to recompile
also, Matrix.cpp contains template code, but it is compiled and procudes an empty object file (Matrix.o)
Dear Aldo,

thank you very much for the help. I understood the problems in my code.

Regarding the make, I found an automated one on internet, but it dos not detect the .h files dependencies and I did not manage to modify it. I really would appreciate if you could take a look on that.

Thank you very much!
Anderson
sorry, I'm kind of busy
the -MM flag will give you the dependencies
for example
$ g++ -MM main.cpp
main.o: main.cpp ../include/Messages.h ../include/Matrix.h Matrix.cpp \
 ../include/ShapeFunctions.h ../include/Matrix.h
(not sure why Matrix.h is listed twice)

also, you should simply move the templates implementation from Matrix.cpp to Matrix.h
if you want them to be in separate files you may just #include "Matrix.impl" at the end
Dear Aldo, thank you very much for your help.

Here goes my actual makefile:

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
60
61
62
63
64
#==============================
# Compiler
CC := g++ 

#==============================
# Build
BUILDDIR := build

#==============================
# Build
TARGET := bin/FEM

#==============================
# Source files
SRCEXT := cpp
SRCDIR := src
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))

#==============================
# Object files
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))

DEPS := $(OBJECTS:.o=.d)
#==============================
# Flags
# Compiler flags
CFLAGS = -I${BLAS_INC} -g -std=c++11 -march=native -MMD -MP

# Linker flags
LDFLAGS = -L${BLAS_LIB} -lgsl -lgslcblas -lopenblas

#==============================
#Link
$(TARGET): $(OBJECTS)
    @echo "========================================="
    @echo "Linking"
    ${CXX} $^ -o $(TARGET)  ${LDFLAGS}

# Compile
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @echo " ==========================="
    @echo " Compiling: $@ $<"
    @mkdir -p $(BUILDDIR)
    $(CC) $(CFLAGS) -c -o $@ $<

#==============================
# Include the dependency files
-include $(DEPS)

#==============================
#Clear the directory
clean:
    @echo "========================================="
    @echo " Cleaning"; 
    $(RM) -rv $(BUILDDIR) $(TARGET)

#==============================
#Run the binary
run:
    @echo "========================================="
    @echo " Running"; 
    bash -c "./$(TARGET)"

.PHONY: clean


It seems to be working properly, but it generates some dependencies with strange paths, like src/../include/Messages.h
Is that correct? You can check the project in the GitHub link.

Thank you very much!
> src/../include/Messages.h
I guess that you've got that because your includes are of the form #include "../include/Messages.h"
the path will resolve correctly, so I don't think that's an issue
Thank you ne555!

As I'm a beginner, it would be great to count with your comments about how to improve my code!



Topic archived. No new replies allowed.