Are there known issues with passing linked lists from C code to C++?
I have a Qt dialogue that I'm adding to legacy C code. A Qt class (in a C++ library) calls a C library function, which returns a static pointer to a linked list. That list is created in the C library. For example:
C code:
typedef my_struct_s my_struct_t, *my_struct_p;
struct {
int some_data;
double some_more_data;
my_struct_p next;
} my_struct_s;
void create_list()
{
my_struct_p next;
SP_head = (my_struct_p) calloc(1, sizeof(my_struct_t));
next = (my_struct_p) calloc(1, sizeof(my_struct_t));
SP_head->some_data = 1;
next->some_data = 2;
SP_head->next = next;
}
static my_struct_p SP_head=(my_struct_p)0;
extern my_struct_p get_list() {
if(!SP_head)
create_list();
return SP_head;
}
C++ code:
myclass::do_something()
{
my_struct_p list = get_list();
for(my_struct_p ptr = list; ptr; ptr = ptr->next)
{
std::cout << ptr->value;
}
}
SP_head is valid and contains a bunch of entries when I obtain it. In my debugger I can see the next entries are populated and valid on return from the function get_list().
When I try to assign ptr=ptr->next, whether inside a for loop or at the end of a while loop or via a temporary pointer, the value is null. i.e
temp_ptr = ptr->next;
// temp_ptr is null
// but I can see ptr->next looks ok in a debugger
I've now changed the code to copy each element of the list into an array and return that array, and it works fine. Is this something to do with the stack and an incompatibility with C and C++, or just an indication that I have a memory problem somewhere else?
Of course, C++ is prepared to handle C, then you should assume that there are no incompatibility when the proper interface is used.
After all, there are many SW out there implemented in C and interfaced from C++. I'm using, for instance, SWI-Prolog - implemented in C - from Qt.
But to compile your code, I had to do several changes: here the bottom line
file my_list.h
#ifdef __cplusplus
extern "C" {
#endif
struct my_struct_s {
int some_data;
double some_more_data;
struct my_struct_s *next;
};
typedef struct my_struct_s my_struct_t, *my_struct_p;
void create_list();
extern my_struct_p get_list();
#ifdef __cplusplus
}
#endif
file my_list.c
#include "my_list.h"
#include <stdio.h>
#include <stdlib.h>
static my_struct_p SP_head=(my_struct_p)0;
void create_list()
{
my_struct_p next;
SP_head = (my_struct_p) calloc(1, sizeof(my_struct_t));
next = (my_struct_p) calloc(1, sizeof(my_struct_t));
SP_head->some_data = 1;
next->some_data = 2;
SP_head->next = next;
}
my_struct_p get_list() { return SP_head; }
file main.cpp
#include "my_list.h"
#include <iostream>
class myclass {
public:
void do_something()
{
my_struct_p list = get_list();
for(my_struct_p ptr = list; ptr; ptr = ptr->next)
std::cout << ptr->some_data << std::endl;
}
};
int main() {
create_list();
myclass c;
c.do_something();
}
now that compile and run:
g++ my_list.c main.cpp
./a.out
1
2
Related
I supposed I do not have to link to a shared library when I use dlopen. However, in cmake target_link_libraries(main_dlopen dl) results in linker errors
main_dlopen.cpp.o: In function `main':
main_dlopen.cpp:25: undefined reference to `ntclass::init(char const*)'
etc...
If I use target_link_libraries(main_dlopen dl ntclass), where libntclass.so is my library, then everything is fine.
Is it really fine or am I missing something? As a background, I want to test non thread-safe libraries, as explained here, and expect that linking should be avoided with non thread-safe libs.
Partially answered myself below
A full example is below (used this as reference).
(shared library)
ntclass.h
#ifndef NTCLASS_H
#define NTCLASS_H
#include <cstddef>
class ntclass
{
private:
static char *sptr;
char *ptr;
public:
ntclass() : ptr(NULL) {}
~ntclass();
void init(const char* str);
void print();
};
typedef ntclass* create_t();
#endif // NTCLASS_H
ntclass.cpp
#include "ntclass.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
char *gptr = NULL;
char *ntclass::sptr = NULL;
ntclass::~ntclass()
{
if (gptr)
{
delete[] gptr;
gptr = NULL;
}
if (sptr)
{
delete[] sptr;
sptr = NULL;
}
if (ptr)
{
delete[] ptr;
ptr = NULL;
}
}
void ntclass::init(const char* str)
{
int size = strlen(str)*sizeof(char);
gptr = new char[size];
memcpy(gptr, str, size);
sptr = new char[size];
memcpy(sptr, str, size);
ptr = new char[size];
memcpy(ptr, str, size);
}
void ntclass::print()
{
std::cout << "Global: " << gptr << std::endl;
std::cout << "Static: " << sptr << std::endl;
std::cout << "Normal: " << ptr << std::endl;
}
extern "C" ntclass *create()
{
return new ntclass();
}
(Main executable)
main_dlopen.cpp
#include <iostream>
#include "ntclass.h"
#include <dlfcn.h>
#include <stdlib.h>
using namespace std;
int main()
{
void *handle = dlopen("./libntclass.so", RTLD_NOW);
if (handle == NULL)
{
cerr << dlerror() << endl;
exit(-1);
}
create_t *createA = (create_t*) dlsym(handle, "create");
create_t *createB = (create_t*) dlsym(handle, "create");
ntclass *A = createA();
ntclass *B = createB();
A->init("A");
B->init("B");
A->print();
B->print();
delete A;
delete B;
return 0;
}
(cmake)
cmake_minimum_required(VERSION 2.8)
set( CMAKE_VERBOSE_MAKEFILE on )
set(CMAKE_BUILD_TYPE RelWithDebInfo)
add_library(ntclass SHARED ntclass.cpp)
add_executable(main_dlopen main_dlopen.cpp)
target_link_libraries(main_dlopen dl) # <-- Here is a problem
Partial answer:
I added keyword virtual for ntclass methods (init, print, ~ntclass) and it works fine now. Still, can anyone explain why is it needed?
Your "partial answer" is a correct fix. For explanations, see that nice answer about virtual keyword.
In short:
Until init method in ntclass is declared as virtual, expression
A->init("A")
uses definition of the method in that class (independent on which actual type object A has). And because this definition is absent in main_dlopen.cpp, linker generates the error.
With virtual keyword, resolution of init method is deffered to runtime, when actual type of A object will be known.
I am trying to implement a Polynomial structure using a linked list of Terms (the linked list is implemented separately).
When I run my main program, I get a (Thread 1: EXC_BAD_ACCESS code=2) error on the line
coeff = x; in the definition my setCoeff function.
I tried commenting out that specific function call, but it gives me the same error for the setX() and setY() functions.
I think I have my files and functions set up properly, I cannot figure out why it is not letting me use these functions.
Please help !
In order, I have included: Polynomial.h, Polynomial.cpp, and main.cpp.
#ifndef __Polynomial__Polynomial__
#define __Polynomial__Polynomial__
#include <stdio.h>
class Term {
private:
int coeff;
int deg_x;
int deg_y;
public:
Term();
int getCoeff();
int getX();
int getY();
void setX(int);
void setY(int);
void setCoeff(int);
};
#endif /* defined(__Polynomial__Polynomial__) */
___________________________
#include "Polynomial.h"
Term::Term() {
coeff = NULL;
deg_x = NULL;
deg_y = NULL;
}
int Term::getCoeff(){
return coeff;
}
int Term::getX() {
return deg_x;
}
int Term::getY() {
return deg_y;
}
void Term::setX(int x){
deg_x = x;
}
void Term::setY(int x){
deg_y = x;
}
void Term::setCoeff(int x){
coeff = x;
}
__________________________
#include <iostream>
#include <fstream>
#include "Polynomial.h"
int main() {
Term* t1;
t1->setCoeff(4);
t1->setX(3);
t1->setY(6);
}
You never create an object. You have Term* t1, which is an uninitialized pointer to random memory, then you try to use it with t1->setCoeff(4) which is trying to use an object that was never created. That's definitely gonna go wrong.
Do this instead..
auto t1 = std::make_unique<Term>();
Or if you don't need it to be a pointer, you can create a simple stack variable and access it with '.' operator like this ...
Term t1;
t1.setCoeff(4);
t1.setX(3);
t1.setY(6);
I'd like to be able to write my ISR in one place:
some_collection TimerHandlers;
// added to ISR table in linker script
void rawTimerIRQHandler() {
call_each_handler_in(handlers);
}
Such that I can then register handlers in other files
// file1.cpp
void ledTimerHandler1() {
}
register(ledTimerHandler1); //or in an init function if not possible here
// file2.cpp
void ledTimerHandler2() {
}
register(ledTimerHandler2); //or in an init function if not possible here
And when the hardware jumps to rawTimerIRQHandler, it executes ledTimerHandler1 and ledTimerHandler2 in some arbitrary order.
Obviously, I can implement this using something similar to a vector<void(*)()>, but since the number of these handlers is known at compile-time, is there any way I can generate an array (or template linked list) at compile-time? I'd like to avoid the dynamic memory allocation that comes with vector.
I'm open to using template<>, #define, or even GCC-specific attributes to acheive this goal.
The scaffolding's a bit tedious but once it's done the usage couldn't be simpler:
// example.h:
#include "Registered.h"
struct example : Registered<example> {};
// main.cc:
#include <iostream>
#include "example.h"
int main ()
{
for ( auto p = example::registry; p; p=p->chain )
std::cout << p << '\n';
}
// Registered.h :
template<class registered>
struct Registered {
static registered *registry;
registered *chain;
Registered() : chain(registry) {registry=static_cast<registered*>(this);}
};
// example.cc:
#include "example.h"
template<> example *Registered<example>::registry = 0;
static struct example first, second, third; // these can be defined anywhere w/ static duration
edit: moved the first,second,third declaration/definitions to satisfy my inner pedant
Absolutley. If I understand correctly, you just want a fixed array of function pointers to your handlers. Using C++11 syntax, and assuming 3 handlers just for the sake of the example,
#include <array>
const std::array<HandlerPtr, 3> handlers= {&ledTimerHandler1, &ledTimerHandler2, &ledTimerHandler3};
or using more classic C/C++ syntax
const HandlerPtr handlers[] = {&ledTimerHandler1, &ledTimerHandler2, &ledTimerHandler3};
Based off jthill's answer, here's what I'll probably end up using (since I don't need a generic form):
struct timer_handler {
static timer_handler *first = 0;
timer_handler *next;
void (*f)();
public:
timer_handler(void (*f)()) : next(first), f(f) { first = this;}
// connect this to the interrupt vector
static inline void executeAll() {
auto p = first;
while(p) {
p->f();
p = p->next;
}
}
};
//a.cpp
void foo() {
}
timer_handler tfoo = foo;
//b.cpp
void bar() {
}
timer_handler tbar = bar;
I'm trying to create a linked list class template (Yes, I know there's one in the c++ library but I wanted to create my own for fun). I've traced through the code and all seems well until the program exits.
Here's the used code:
list.h:
#ifndef LIST_H
#define LIST_H
#include "misc.h"
template <typename T> class CList {
private:
class CNode {
friend CList;
private: T data;
CNode* next;
public: CNode() : next(NULL) {}
~CNode() { delete [] next; }
};
private: int length;
CNode* first;
public:
CList() : length(0), first(NULL) {}
CList(int i_length) : first(NULL) {
int i;
CNode* cur = NULL;
CNode* prev = NULL;
if (i_length < 0) length = 0;
else length = i_length;
for (i=0;i<length;i++) {
// allocate new CNode on heap
cur = new2<CNode>();
// attach preceding CNode pointer
if (prev) prev->next = cur;
else first = cur;
prev = cur;
}
}
~CList() { delete first; }
};
misc.h
#ifndef MISC_H
#define MISC_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
inline void terminate( const char* message, int code ) {
printf("\n\n%s\n\n",message);
system("pause");
exit(code);
};
template <typename T> inline T* new2() {
T* ret = new T;
if (!ret) terminate("Insufficient Memory",-2);
return ret;
}
template <typename T> inline T* new2(int num) {
if (num <= 0) terminate("Invalid Argument",-1);
T* ret = new T[num];
if(!ret) terminate("Insufficient Memory",-2);
return ret;
}
#endif
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "../Misc/misc.h"
#include "../Misc/list.h"
int main(int argc, char* argv[]) {
//CList<int> m;
CList<int> n(5);
system("pause");
return 0;
}
Here is what the variable "n" looks like at the breakpoint just before "return 0;".
http://s20.beta.photobucket.com/user/marshallbs/media/Untitled_zps52497d5d.png.html
Here's the context in which the error occurs. Unfortunately at this point I can no longer view the variable "n" on the watch list.
_mlock(_HEAP_LOCK); /* block other threads */
__TRY
/* get a pointer to memory block header */
pHead = pHdr(pUserData);
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
There is no error when I use the default constructor for my list. I don't understand what's going on as the memory release process should stop when it reaches the fifth CNode object which has a null "next" pointer. It acts as though it's trying to releasing an invalid non-null pointer but I don't see how this can happen.
I built and ran (from the debugger) the code as-is and got no assertion failures. In fact, there is no memory deallocation at all because CList doesn't have a destructor (didn't you post the complete code?).
One problem is that you allocate next using new and free it using delete[]. This is undefined behaviour.
Allocation:
cur = new2<CNode>(); // new2 uses `new' and not `new[]'
Deallocation:
~CNode() { delete [] next; }
Replace the latter with delete next;.
I am trying to build a Linked list application using C++ programming language & features such as inheritance etc.
I have split the interface & implementation in different files but not able to compile.
Below are the list of files
Interface files :- node.h , abstractList.h , singleLinkedList.h
Implementation files: singleLinkedList.cpp
node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
struct nodeType {
int data;
struct nodeType *next;
}listNode;
#endif
abstractList.h
#ifndef ABSTRACT_LIST_H
#define ABSTRACT_LIST_H
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
class abstractList {
public:
virtual ~abstractList();
virtual bool isEmpty(Node* ) = 0;
virtual int get(const int&) = 0;
virtual int indexOf(const int& ) = 0;
virtual Node insert(const int& , const int& ) = 0;
virtual void delete(const int& ) = 0;
};
#endif
singleLinkedList.h
#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H
#include <iostream>
#include "node.h"
#include "abstractList.h"
class singleLinkedList : public abstractList {
public:
singleLinkedList();
~singleLinkedList();
Node populateList( );
private:
void checkIndex();
int data;
Node head;
};
#endif
So far i have just coded the populateList() function in the implentation file, here goes the implementation file.
singleLinkedList.cpp
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
#include "abstractList.h"
Node singleLinkedList :: populateList()
{
Node temp;
int data;
temp = head;
char ch;
std::cout<<"Enter Data? (y/n) " << std::endl;
std::cin>>ch;
while(ch == 'Y' || ch == 'y')
{
std::cout<<"Enter the data that you would like to store.\n"<<std::endl;
std::cin>>data;
temp = new Node();
temp->data = data;
temp->next = head;
head = temp;
std::cout<<"Enter more data?"<<std::endl;
std::cin>>"\n">>ch;
}
return temp;
}
When i give g++ -c singleLinkedList.cpp , i am getting lot of errors. I am pretty sure i have done something stupid. Can anyone please pin point my error?
EDIT: Error Log With specfic issues.
struct nodeType {
int data;
struct nodeType *next;
}listNode;
virtual listNode *insert();
Is the above statement correct?
Thanks
Kelly
delete is a keyword in C++, you can't use it as a method name. You need to use a different name here:
class abstractList {
public:
//...
virtual void delete(const int& ) = 0;
//-----------^^^^^^ rename this.
};
The problem is in your typedef:
typedef listNode *Node;
means that all instances of Node will essentially replaced by listnode*
temp = new Node();
actually reads
temp = new listnode*();
But new Foo() would return a Foo* (because new returns a pointer to memory allocated for an object), meaning that new listnode*() would return a listnode**. temp being a listnode* has no Idea what a listnode** is and complains.
what you want to do is:
Node temp = new listnode();
or forget the typedef altogether:
listnode* temp = new listnode();