why my backtrace() only finds the path of a dynamic library - linux

void testfunc3(){
char* funcname[20];
int n = 0 ;
n = backtrace((void**)funcname,20);
char** p_need_free = (char**)backtrace_symbols((void**)funcname,n);
int i = 0;
for(i=0;i<n;i++){
printf("funcname is : %s\n",p_need_free[i]);
}
free(p_need_free);
}
void testfunc2(){
testfunc3();
}
void testfunc1(){
testfunc2();
}
/** in another file **/
int main(){
testfunc1();
return 0;
}
this project has 2 files , main() takes 1, other functions take
another, the latter file is compiled as a dynamic library.

Related

Constructing a child object from parent object in C++

I am trying to construct a child class object from a base class object. I have tried the below code.
class A
{
public:
A();
A(A&& objectName) = default;
virtual void setint(int i);
virtual void getint();
int var;
};
class B: public A
{
public:
virtual void getint();
B(A&& objectName);
int j= 20;
};
A::A()
{
}
void A::setint(int i)
{
var = i;
}
void A::getint()
{
qDebug()<<"From A Var"<<var;
}
void B::getint()
{
qDebug()<<"From B j"<<j;
qDebug()<<"From B Var"<<var;
}
B::B(A&& objectName): A(std::move(objectName))
{
}
And in my Main.cpp I am doing this
#include <memory>
int main(int argc, char *argv[])
{
A *obj = new A();
obj->setint(10);
obj->getint();
A *obj1 = new B(std::move(*obj));
obj->getint();
obj1->getint();
return 0;
}
The result I get is
From A Var 10
From A Var 10
From B j 20
From B Var 10
My question is why am I getting the value of Var after A *obj1 = new B(std::move(*obj)); this line. I thought the object pointed by obj must have been destructed.
Let me copy paste from this answer: https://stackoverflow.com/a/15663912/512225
std::move doesn't move from the object. It just returns an rvalue reference whose referand is the object, making it possible to move from the object.
Anyway your code is terrible. I hope you know. If you don't, ask for a review.

I'm not Sure why the incompatible intializer is not compatible with the parameter type int

Basically, I want to display my test scores and the average of them but I am unable to because of these errors
I've tried to take void display and put it in the class and declare it in main but that didn't work
#include <iostream>
#include <string>
using namespace std;
class TestScore
{
public:
TestScore() {};
TestScore(int arr[], int SIZE) {};
void testAvg(int arr[], int SIZE);
void displayArray(int arr[], int Size);
};
void TestScore::testAvg(int arr[], int SIZE)
{
int sum = 0;
for (int i = 0; i < SIZE; i++)
{
sum = sum + arr[i];
try
{
if ((arr[i] > 100) || (arr[i] < 0))
{
throw(1);
}
}
catch (int n)
{
cout << "Error" << endl;
}
}
int average = sum / SIZE;
}
void TestScore::displayArray(int arr[], int SIZE)
{
for (int i = 0; i < SIZE; i++)
{
cout << arr[i] << endl;
}
}
void main()
{
const int SIZ = 5;
int Grade[SIZ] = { 89,65,99,100,81 };
TestScore T(int Grade, int SIZ);
T(Grade, SIZ).testAvg(Grade, SIZ);
T(Grade, SIZ).displayArray(Grade, SIZ);
system("pause");
}
I expect it to display the average of my score so basically, I want to have an array of 5 test scores displaying and then the average of them.
TestScore T(int Grade, int SIZ); declares a function named T, taking two parameters of type int. You then call that function, passing a parameter of type int[5] - not an int. Hence the error message.
Further, that function is not implemented anywhere. Frankly, I don't understand what you are trying to do with that function declaration; it makes little sense.

FLTK Fl_Choice obtain selected item

I want to get the selected item of users but it gives an error on this code:
choice->value();
hopes you kind guys can help me.
Fl_Choice* choice;
int i = 0;
void but_cb(Fl_Button* obj, void*)
{
i = choice->value();
cout<<i;
}
int main (int argc, char* argv[]) {
Fl_Double_Window* win = new Fl_Double_Window(400,400,"Sample");
win->begin();
Fl_Choice* choice = new Fl_Choice(100,100,100,100,"Name");
choice->add("Peter");
choice->add("Tom");
choice->add("Mary");
Fl_Button* but = new Fl_Button(300,300,50,50,"Selected");
but->callback((Fl_Callback*)but_cb);
win->show();
return (Fl::run());
}
I did not spend much time reading the code, so the modified, and commented code below may not be solution to your problem. I believe choice is NULL in but_cb() as you are redefining choice in your main() function, hiding the global 'choice' pointer.
Fl_Choice* choice;
int i = 0;
void but_cb(Fl_Button* obj, void*) {
i = choice->value(); // uses global `choice` pointer to Fl_Choice.
cout << i;
}
int main (int argc, char* argv[]) {
Fl_Double_Window* win = new Fl_Double_Window(400,400,"Sample");
win->begin();
/* You are redefining choice here! BAD, because but_cb uses the global one.
Fl_Choice* choice = new Fl_Choice(100,100,100,100,"Name");
*/
// Good, assigns to the global `choice`, so it won't be NULL when but_cb is called
choice = new Fl_Choice(100,100,100,100,"Name");
choice->add("Peter");
choice->add("Tom");
choice->add("Mary");
Fl_Button* but = new Fl_Button(300,300,50,50,"Selected");
but->callback((Fl_Callback*)but_cb);
win->show();
return (Fl::run());
}

boost::shared_array assignment crashes application(VC++ 2010)

Modified the below circular queue code for my app.
This queue can hold 32 elements max and I have declared the elements as a structure array inside the class. For adding an element to the queue you have to call CreateElement() functions, which checks for a free element and returns an index. When I reuse an element after processing the following line in the CreateElement functions crashes
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
As per documentation, the assignment operator is supposed to destroy the earlier object and assign the new one. Why is it crashing? Can someone tell me where am I screwing?
#include "boost/thread/condition.hpp"
#include "boost/smart_ptr/shared_array.hpp"
#include <queue>
#define MAX_QUEUE_ELEMENTS 32
typedef struct queue_elem
{
bool inUse;
int index;
int packetType;
unsigned long compressedLength;
unsigned long uncompressedLength;
boost::shared_array<char> data;
}Data;
class CQueue
{
private:
int m_CurrentElementsOfQueue;
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
Data m_QueueStructure[MAX_QUEUE_ELEMENTS];
public:
CQueue()
{
m_CurrentElementsOfQueue = 0;
for(int i = 0; i < MAX_QUEUE_ELEMENTS; i++)
{
m_QueueStructure[i].inUse = false;
m_QueueStructure[i].index = i;
}
}
~CQueue()
{
for(int i = 0; i < m_CurrentElementsOfQueue; i++)
{
int index = wait_and_pop();
Data& popped_value = m_QueueStructure[index];
popped_value.inUse = false;
}
m_CurrentElementsOfQueue = 0;
}
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
int wait_and_pop()
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
Data& popped_value=the_queue.front();
the_queue.pop();
return popped_value.index;
}
int CreateElement(int bufferSize, unsigned long _compressedLength,
unsigned long _uncompressedLength, int _packetType) /* Send data length for this function */
{
int queueElems = 0;
if(m_CurrentElementsOfQueue == 32)
{
CCommonException ex(QERROR, QUEUE_FULL, "Circular Buffer Queue is full");
throw ex;
}
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)
{
if(m_QueueStructure[queueElems].inUse == false)
break;
}
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
m_QueueStructure[queueElems].inUse = true;
m_QueueStructure[queueElems].compressedLength = _compressedLength;
m_QueueStructure[queueElems].uncompressedLength = _uncompressedLength;
m_QueueStructure[queueElems].packetType = _packetType;
m_CurrentElementsOfQueue++;
return queueElems;
}
Data& GetElement(int index)
{
Data& DataElement = m_QueueStructure[index];
return DataElement;
}
void ClearElementIndex(Data& delValue)
{
m_CurrentElementsOfQueue--;
delValue.inUse = false;
}
};
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++) after looping queueElems has value 32 but in your m_QueueStructure only 32 elements so you trying to access m_QueueStructure[queueElems].data to 33rd element. That the problem.
EDIT: try use m_QueueStructure[queueElems].data.reset(new char[bufferSize]);
Solved the problem. Two changes I did. In the wait_and_pop function, I was returning an index rather than a Data&. When I returned Data&, that solved the assignment problem. Another crash was happening due to a memset of a shared_array.get(). Lesson learnt, never memset a shared_array or a shared_ptr.

Convert .Net ref (%) to native (&)

How can I convert a C++/CLI int %tmp to native C++ int &tmp?
void test(int %tmp)
{
// here I need int &tmp2 for another pure C++ function call
}
Neither of the existing answers properly handle in/out parameters, let alone any advanced use cases.
This should work for all cases where other_func does not keep the reference after it returns:
void test(int %tmp)
{
pin_ptr<int> pinned_tmp = &tmp;
other_func(*pinned_tmp);
}
Just tried this, works fine:
//in the C++ dll
void testFunc( int& n )
{
n = 5;
}
//in the CLI app
[DllImport( "my.dll", EntryPoint = "?exported_name_here",
CallingConvention = CallingConvention::StdCall )]
void TestFunc( int& );
void test( int% tmp )
{
int n;
TestFunc( n );
tmp = n;
}
void your_function(int *);
void your_function2(int &);
void test(int %tmp)
{
int tmp2;
your_function(&tmp2);
your_function2(tmp2);
tmp=tmp2;
}

Resources