Eigen's Map<> as a class member - arrays

I'm trying to have a class that contains array but have an interface to them through eigen.
class A {
public:
array<double,3> xa;
Map<Matrix<double,3,1>> x;
A() : x(xa.data(),xa.size()) {}
};
this doesn't work :
A a;
a.xa[0] = 0.12;
cout << a.x ;
I assume the problem is because Map<> doesn't have a default constructor. http://eigen.tuxfamily.org/dox/TutorialMapClass.html#TutorialMapPlacementNew

The example that you provide does work for me (Eigen 3.0.1 and GCC 4.6.1)
#include <Eigen/Core>
#include <array>
#include <iostream>
using namespace std;
using namespace Eigen;
class A {
public:
array<double,3> xa;
Map<Matrix<double,3,1>> x;
A() : x(xa.data(),xa.size()) {}
};
int main()
{
A a;
a.xa[0] = 0.12;
cout << a.x ;
}
when compiled with
g++ test.cpp -std=c++0x -o test -I/usr/include/eigen3
I get the following output when calling the resulting test executable:
[/tmp]% ./test
0.12
2.07717e-317
0%

Related

GTest | Seg faults when testing insertion of randomly generated values at random index to a dynamic array

I created a custom generic DynamicArray class where everything works fine except when testing an add() method in which I am adding element ats a specific index.
I am using gtest framework for testing, more specifically, I am using type-parametrized tests for creating test cases. I run the tests against these types (int, int*, char*, char*, std::string), I created functions that generate random values for me and I use them in my test cases.
The problem is that sometimes I get seg faults and some other times I don't.
Here is the structure of my project:
build/
include/
structs/
DynamicArray.h
src/
main.cpp
tests/
TestCases/
DynamicArrayTest.cpp
TestClasses/
DynamicArrayTest.h
TestHelpers/
generators.h
test_runner.cpp
CMakeLists.txt
CMakeLists.txt:
# cmake version and project info
############################################################################
# Variables
# Compile flags
SET(GCC_DEBUGGING_COMPILE_FLAGS "-Wall -ggdb3")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_DEBUGGING_COMPILE_FLAGS}")
set(SRCS
src/main.cpp
)
set(EXEC_SRCS
src/main.cpp
)
set(HEADERS
include/structs/structures.h
)
set(TEST_SRCS
tests/TestCases/DynamicArrayTest.cpp
tests/test_runner.cpp
)
############################################################################
# Gtest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
############################################################################
##
# Build targets
##
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
add_executable(${PROJECT_NAME} ${EXEC_SRCS} ${SRCS})
if(${PROJECT_NAME}_ENABLE_UNIT_TESTING)
add_library(${PROJECT_NAME}_LIB ${HEADERS} ${SRCS})
endif()
elseif(${PROJECT_NAME}_BUILD_HEADERS_ONLY)
add_library(${PROJECT_NAME} INTERFACE)
else()
add_library(
${PROJECT_NAME}
${HEADERS}
${SRCS}
)
endif()
############################################################################
##
# Including Directories
##
target_include_directories(Structs
PUBLIC ${PROJECT_SOURCE_DIR}/include
)
############################################################################
##
# Installation
##
############################################################################
##
# Tests
##
enable_testing()
add_executable(StructsTest
${TEST_SRCS}
)
target_compile_options(StructsTest PRIVATE -ggdb3)
target_link_libraries(
StructsTest
GTest::gtest_main
)
target_include_directories(StructsTest
PUBLIC ${PROJECT_SOURCE_DIR}/tests
PRIVATE ${PROJECT_SOURCE_DIR}/include
)
include(GoogleTest)
gtest_discover_tests(StructsTest)
Code to be tested:
include/structs/DynamicArray.h
namespace structs
{
#define INITIAL_SIZE 10
template <typename T>
class DynamicArray
{
private:
size_t m_capacity;
size_t m_size;
T* m_data;
void resize();
public:
// constructors & destructors
DynamicArray();
DynamicArray(const DynamicArray&);
DynamicArray(T&);
DynamicArray(T&&);
DynamicArray(T*, size_t);
~DynamicArray();
// operations
void push_back(T);
void set(size_t, T);
void add(size_t, T);
T get(size_t);
T pop();
T remove(size_t);
size_t size();
size_t capacity();
bool is_empty();
void read();
void reset();
}; // DynamicArray class
}// namespace structs
// .. Impl
template<typename T>
structs::DynamicArray<T>::DynamicArray()
:m_capacity(INITIAL_SIZE),
m_data(new T[m_capacity]),
m_size(0)
{
if (m_data == nullptr)
throw std::bad_alloc();
m_size = 0;
}
template<typename T>
structs::DynamicArray<T>::DynamicArray( T* values, size_t size )
: DynamicArray()
{
for (size_t i = 0; i < size; i++)
{
this->push_back(*values);
values++;
}
}
template<typename T>
structs::DynamicArray<T>::~DynamicArray()
{
delete[] m_data;
}
template<typename T>
void structs::DynamicArray<T>::push_back( T value )
{
if (m_size >= m_capacity)
resize();
*(m_data + m_size) = value;
m_size ++;
}
template<typename T>
void structs::DynamicArray<T>::add( size_t index, T value )
{
if (index > m_size)
throw std::out_of_range("Invalid: Exceeds range, use set(size_t, T) instead");
if ( m_size == m_capacity )
resize();
for (long i = m_size - 1; i >= index; i--)
m_data[i+1] = m_data[i];
m_data[index] = value;
m_size ++;
}
Now to the test part:
I created a header file where I store the test fixture class and its implementation located in tests/TestClasses/*Test.h and I placed the test cases in tests/TestCases/*Test.cpp, and test_runner.cpp is the test driver.
DynamicArrayTest.h
The constructor SetUp() has a seperate implementation for each type, to make things a bit shorter I am going to only show the implementation of std::string type.
I assumed in the test case that resize() will not be called, and the array size will always be within the limit of the initial size it was created with to avoid adding a complexity overhead.
#pragma once
#include <iostream>
#include <random>
#include <cstdlib>
#include <ctime>
#include <type_traits>
#include "structs/DynamicArray.h"
#include "gtest/gtest.h"
#include "TestHelpers/generators.h"
#define TEST_INITIAL_SIZE 10
namespace structs
{
namespace test
{
template<typename T>
class DynamicArrayTest : public ::testing::Test
{
protected:
void SetUp() override;
void TearDown() override;
public:
structs::DynamicArray<T> a0_; // empty
structs::DynamicArray<T> a1_; // filled upon initialization
T value_;
T array_[TEST_INITIAL_SIZE];
std::vector<T> sample_values_;
};
} // namespace test
} // namespace structs
using namespace structs::test;
template <typename T>
void DynamicArrayTest<T>::SetUp()
{
}
template<>
void DynamicArrayTest<std::string>::SetUp()
{
value_ = generate_string(generate_numeric<int>(0, 20));
for (size_t i = 0; i < TEST_INITIAL_SIZE; i++)
array_[i] = generate_string(generate_numeric<int>(0, 20));
for (size_t i = 0; i < TEST_INITIAL_SIZE; i++)
sample_values_.push_back(generate_string(generate_numeric<int>(0, 20)));
for (size_t i = 0; i < TEST_INITIAL_SIZE; i++)
a1_.push_back(generate_string(generate_numeric<int>(0, 20)));
}
template <typename T>
void DynamicArrayTest<T>::TearDown()
{
// clean up
a1_.reset();
}
The functions that generate the random values lives in tests/TestHelpers/generators.h:
template<typename T>
T generate_numeric(const T, const T);
char generate_char();
std::string generate_string(const size_t);
Test cases: DynamicArrayTest.cpp
#include <iostream>
#include "gtest/gtest.h"
#include "structs/structures.h"
#include "TestClasses/DynamicArrayTest.h"
#include "TestHelpers/generators.h"
using namespace structs::test;
TYPED_TEST_SUITE_P(DynamicArrayTest);
TYPED_TEST_P(DynamicArrayTest, AddMethodTest)
{
//structs::DynamicArray<TypeParam> initial_a1 = this->a1_;
size_t random_index = generate_numeric(0, TEST_INITIAL_SIZE);
size_t initial_size = this->a1_.size();
this->a1_.add(random_index, this->value_);
// Value is added at correct index
EXPECT_EQ(this->a1_.get(random_index), this->value_);
// size is updated
EXPECT_EQ(this->a1_.size(), initial_size + 1);
}
REGISTER_TYPED_TEST_SUITE_P(DynamicArrayTest,
AddMethodTest
);
//using MyTypes = ::testing::Types<char, int, char*, int*, std::string> ;
using MyTypes = ::testing::Types<std::string> ;
INSTANTIATE_TYPED_TEST_SUITE_P(DynamicArrayTest_, DynamicArrayTest, MyTypes);
Output Samples:
$ ./StructsTest
[==========] Running 13 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 13 tests from DynamicArrayTest_/DynamicArrayTest/0, where TypeParam = std::basic_string<char, std::char_traits<char>, std::allocator<char> >
[ RUN ] DynamicArrayTest_/DynamicArrayTest/0.AddMethodTest
[ OK ] DynamicArrayTest_/DynamicArrayTest/0.AddMethodTest (0 ms)
[----------] 13 tests from DynamicArrayTest_/DynamicArrayTest/0 (8 ms total)
[----------] Global test environment tear-down
[==========] 13 tests from 1 test suite ran. (8 ms total)
[ PASSED ] 12 tests.
$ ./StructsTest
[==========] Running 13 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 13 tests from DynamicArrayTest_/DynamicArrayTest/0, where TypeParam = std::basic_string<char, std::char_traits<char>, std::allocator<char> >
[ RUN ] DynamicArrayTest_/DynamicArrayTest/0.AddMethodTest
Segmentation fault (core dumped)
When running the test with valgrind and a seg fault happens, valgrind always show this:
==138433== Process terminating with default action of signal 11 (SIGSEGV)
==138433== Access not within mapped region at address 0x0
==138433== at 0x49F1241: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==138433== by 0x49F15FD: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==138433== by 0x127B84: structs::DynamicArray<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (DynamicArray.h:160)
==138433== by 0x127336: gtest_suite_DynamicArrayTest_::AddMethodTest<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::TestBody() (DynamicArrayTest.cpp:84)
The line where the error happens as pointed by valgrind is here:
// DynamicArray.h
template<typename T>
void structs::DynamicArray<T>::add( size_t index, T value )
{
if (index > m_size)
throw std::out_of_range("Invalid: Exceeds range, use set(size_t, T) instead");
if ( m_size == m_capacity )
resize();
for (long i = m_size - 1; i >= index; i--)
m_data[i+1] = m_data[i]; // <- this
m_data[index] = value;
m_size ++;
}
If the issue is with the assignment, I don't understand why does it succeed sometimes?
The issue was in this line:
size_t random_index = generate_numeric(0, TEST_INITIAL_SIZE);
TEST_INITIAL_SIZE was set to 10 which is the max capacity size causing resize() to be called and it wasn't implemented properly.
I modified the constant to a value less than max capacity by 2
#define TEST_INITIAL_SIZE INITIAL_SIZE - 2
solved

How to make a Go dll function that takes a go struct or interface as an argument

I am working with go and dlls, and need to create a go dll that exports a function that accepts a go struct.
//test.go
package main
import "fmt"
//export TestFunc
func TestFunc(s TestStruct) {
fmt.Println(s.a)
}
func main() {}
//teststruct.go
package main
type TestStruct struct {
a int
}
and build with
go build -buildmode=c-shared -o test.dll .\test.go .\teststruct.go
I get a perfect dll file.
Now when I use this Go code to call it
//testcall.go
package main
import (
"log"
"syscall"
"unsafe"
)
func main() {
l, e := syscall.LoadLibrary("test.dll")
if e != nil {
log.Fatal(e)
}
p, e := syscall.GetProcAddress(l, "TestFunc")
if e != nil {
log.Fatal(e)
}
_, _, _ = syscall.Syscall(p, 1, uintptr(unsafe.Pointer(&TestStruct{
a: 1002,
})), 0, 0)
}
and run with
go run .\testcall.go .\teststruct.go
I get a fatal error:
The specified procedure could not be found.
When I do the same in C, it works fine:
//test.c
#include <stdio.h>
#include "struct.h"
__declspec(dllexport) void TestFunc(struct TestStruct s) {
printf("%d\n", s.a);
}
//testcall.c
#include <windows.h>
#include "struct.h"
int main() {
HINSTANCE l = LoadLibrary("test.dll");
FARPROC p = GetProcAddress(l, "TestFunc");
struct TestStruct s;
s.a = 1002;
p(s);
}
//teststruct.h
struct TestStruct {
int a;
};
and build with:
gcc -c test.c
gcc -shared -o test.dll test.o
gcc .\testcall.c
I don't get any error, and it gives me
1002
In addition, if I use import "C" in my test.go file, it will give me
unrecognized Go type TestStruct
and that should happen, because C does not know what that type is. So is there any way to use a go struct as a parameter, and why is it giving me a procedure not found error?

Clang/LLVM 9 and 10 SIGSEGV for inline static class members. Bug?

Using inline static class members in Clang gives me unexpected behaviour when the member is another class/struct:
https://godbolt.org/z/mbH6k7
// std=c++17
#include <iostream>
struct A {
double a = 42;
A() { std::cout << "A()" << std::endl; }
};
inline static A a{}; // No problem
namespace N {
inline static A a{}; // No problem
}
struct B {
B() { std::cout << "B()" << std::endl; }
inline static double d; // No problem with built-in types
A& a1 = N::a; // No problem
inline static A a2 = N::a; // No problem
inline static A a3{}; // <-- Problem here!
};
B b1;
inline static B b2;
int main() {
return 0;
}
Expected output, works in Clang 8.0.0, gcc, msvc:
A()
A()
A()
B()
B()
Actual output for Clang 9.0.0 and onwards: 139 (SIGSEGV).
Is this a bug, or what am I missing?
This sounds like a pretty cut-and-dry bug to me:
Per [class.static.data]
An
inline static data member may be defined in the class definition and may specify a brace-or-equal-initializer.
Your code conforms to this:
struct B {
// ...
inline static A a3{};
};

__attribute__((weak)) not working with multiple object files

I probably understand something wrong about weak:
My situation:
"Physical layer" with some WEAK callbacks
"Framing layer" that implements those callbacks, and provides new WEAK callbacks for the application layer
Main - application layer.
phy.h
#pragma once
void phyStuff(void);
// new callback for higher layer
void __attribute__((weak)) phy_cb();
phy.c
#include <stdlib.h>
#include <stdio.h>
#include "phy.h"
// Callback, weak
void phy_cb() {
printf("phy_cb, default implementation. BAD!!!\n");
}
void phyStuff(void) {
printf("PHY stuff. Running phy_cb.\n");
phy_cb();
}
frm.h
#pragma once
#include "phy.h"
void frmStuff(void);
// new callback for higher layer
void __attribute__((weak)) frm_cb();
frm.c
#include <stdio.h>
#include "phy.h"
#include "frm.h"
// implement the callback
void phy_cb() {
printf("phy_cb, FRM implementation. GOOD\n");
}
// Callback, weak
void frm_cb() {
printf("frm_cb, default implementation. BAD!!!\n");
}
void frmStuff(void) {
printf("FRM stuff. Running frm_cb\n");
frm_cb();
}
main.c
#include <stdio.h>
#include "frm.h"
#include "phy.h"
void frm_cb() {
printf("frm_cb, APP implementation. GOOD\n");
}
void main(void) {
printf("Main.\n");
phyStuff();
frmStuff();
}
Now, if I compile it...
$ gcc main.c phy.c frm.c
$ ./a.out
Main.
PHY stuff. Running phy_cb.
phy_cb, default implementation. BAD!!! <--- not expected!
FRM stuff. Running frm_cb
frm_cb, APP implementation. GOOD
Why isn't the weak symbol overridden in this case? Is there any workaround?
You should be applying __attribute__((weak)) to the implementations, not the prototypes:
phy.h
#pragma once
void phyStuff(void);
// new callback for higher layer
void phy_cb();
phy.c
#include <stdlib.h>
#include <stdio.h>
#include "phy.h"
// Callback, weak
__attribute__((weak)) void phy_cb() {
printf("phy_cb, default implementation. BAD!!!\n");
}
void phyStuff(void) {
printf("PHY stuff. Running phy_cb.\n");
phy_cb();
}
frm.h
#pragma once
#include "phy.h"
void frmStuff(void);
// new callback for higher layer
void frm_cb();
frm.c
#include <stdio.h>
#include "phy.h"
#include "frm.h"
// implement the callback
void phy_cb() {
printf("phy_cb, FRM implementation. GOOD\n");
}
// Callback, weak
__attribute__((weak)) void frm_cb() {
printf("frm_cb, default implementation. BAD!!!\n");
}
void frmStuff(void) {
printf("FRM stuff. Running frm_cb\n");
frm_cb();
}
main.c
#include <stdio.h>
#include "frm.h"
#include "phy.h"
void frm_cb() {
printf("frm_cb, APP implementation. GOOD\n");
}
void main(void) {
printf("Main.\n");
phyStuff();
frmStuff();
}
Now we get the desired result:
$ gcc main.c phy.c frm.c
$ ./a.out
Main.
PHY stuff. Running phy_cb.
phy_cb, FRM implementation. GOOD
FRM stuff. Running frm_cb
frm_cb, APP implementation. GOOD
To better see what's happening, try compiling the source files separately and looking at the symbols in each object:
$ gcc phy.c -o phy.o -c
$ gcc frm.c -o frm.o -c
$ gcc main.c phy.o frm.o
$ nm phy.o | grep _cb
0000000000000000 W phy_cb
$ nm frm.o | grep _cb
0000000000000010 W frm_cb
0000000000000000 T phy_cb
$ nm a.out | grep _cb
000000000040052d T frm_cb
0000000000400581 T phy_cb
The 'W' from nm indicates it is a weak symbol.

Is there a way to make a function global to the library and to those who include/link the library?

I'm a bit confused now. I thought that when you used extern on a function, it would become global to everything, but it doesn't seem so... What I want right now, is to have some set of functions that I can use in my static library and in the program that links it. How do I that?
I'm using Objective-C
It works for me, if I just use extern instead of extern inline when defining the function.
Example: inlib.h
extern int foo(int i);
extern int callsfoo(int i);
inlib.m:
#import "inlib.h"
#import "stdio.h"
extern int foo(int i) { printf("Foo: i = %d\n", i); }
extern int callsfoo(int i) {
printf("Callsfoo:\n");
foo(i);
}
Library created with:
gcc -ObjC -c inlib.m -o inlib.o
ar -q lib.a inlib.o
caller.m:
#import "inlib.h"
#import "stdio.h"
int main(int argc, char** argv) {
printf("Calling foo directly.\n");
foo(1);
printf("Calling foo via callsfoo.\n");
callsfoo(2);
return 0;
}
Compiled with: gcc -ObjC -o caller caller.m lib.a -lobjc
Run with: ./caller
Returns:
Calling foo directly.
Foo: i = 1
Calling foo via callsfoo.
Callsfoo:
Foo: i = 2
On CardDefs.h I have:
extern inline
card_config mcc (card_suit s, card_value v, card_points p)
{
card_config ccfg;
ccfg.m_suit = s;
ccfg.m_value = v;
ccfg.m_points = p;
return ccfg;
}
And I have to use this function inside the library and outside. I have other functions that are similar to this.

Resources