Click here to Skip to main content
15,903,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer17-Sep-18 18:36
leon de boer17-Sep-18 18:36 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Dave Kreskowiak17-Sep-18 10:48
mveDave Kreskowiak17-Sep-18 10:48 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer17-Sep-18 17:24
leon de boer17-Sep-18 17:24 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Dave Kreskowiak18-Sep-18 3:00
mveDave Kreskowiak18-Sep-18 3:00 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer18-Sep-18 3:12
leon de boer18-Sep-18 3:12 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Dave Kreskowiak18-Sep-18 3:28
mveDave Kreskowiak18-Sep-18 3:28 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer18-Sep-18 3:38
leon de boer18-Sep-18 3:38 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Richard MacCutchan18-Sep-18 21:31
mveRichard MacCutchan18-Sep-18 21:31 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer18-Sep-18 21:53
leon de boer18-Sep-18 21:53 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Richard MacCutchan18-Sep-18 22:03
mveRichard MacCutchan18-Sep-18 22:03 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
leon de boer18-Sep-18 22:28
leon de boer18-Sep-18 22:28 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Victor Nijegorodov17-Sep-18 20:50
Victor Nijegorodov17-Sep-18 20:50 
JokeRe: Want to learn C. Newbie. Where to start? Pin
CPallini17-Sep-18 22:31
mveCPallini17-Sep-18 22:31 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Victor Nijegorodov17-Sep-18 23:05
Victor Nijegorodov17-Sep-18 23:05 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Dar Brett18-Sep-18 4:54
Dar Brett18-Sep-18 4:54 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
CPallini18-Sep-18 10:28
mveCPallini18-Sep-18 10:28 
AnswerRe: Want to learn C. Newbie. Where to start? Pin
Joe Woodbury18-Sep-18 7:09
professionalJoe Woodbury18-Sep-18 7:09 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Quickbeam121318-Sep-18 7:17
Quickbeam121318-Sep-18 7:17 
AnswerRe: Want to learn C. Newbie. Where to start? Pin
Dar Brett18-Sep-18 20:17
Dar Brett18-Sep-18 20:17 
QuestionCant call an assembly function in c++ code Pin
meerokh14-Sep-18 23:09
meerokh14-Sep-18 23:09 
I am trying to implement coroutines in c++ (I had to reinvent the wheel for my project).

coroutine.h
class coroutine {
public:
	void (*action1)(int);
	Stack local_stack;
	coroutine(void (*action)(int ), int id); ///< will create coroutine
	~coroutine();
	static void yield();
	};

coroutine.cpp
coroutine *global_coro;
fcontext_t context_array[2];
coroutine :: coroutine(void (*action)(int), int id)
			{
			global_coro=this;
			action1=action;
			fcontext_t f_ctx = make_fcontext(global_coro->local_stack.local_stack, 1000, global_coro->action1);//Save the current context of coroutine for the sake of making switch context.
// make_fcontext is in .S file taken from boost::context
			context_array[0]=f_ctx; // Global Array to store the coroutine context so that i can switch the context
			}

coroutine::~coroutine(){}

void coroutine::yield(){

transfer_t tr = jump_fcontext(context_of_current_corotine, context_of_next_coroutine ); //Also in .S file 

Following is my stack class which allocates a block of memory to every coroutine from a global Memory pool

stack.h
extern MemoryPool memPoolObj;

class Stack {
public:
	void *local_stack;
	MemoryPool& m_memPool=memPoolObj;
	Stack();
	~Stack();
};


Stack.cpp
#include "Stack.h"
MemoryPool memPoolObj;
Stack::Stack() {
	auto *local_stack= m_memPool.Allocate();
}
Stack::~Stack() {}


Context.h
typedef void*   fcontext_t;

struct transfer_t
{
    fcontext_t  fctx;
    void    *   data;
};

extern "C"
transfer_t jump_fcontext( fcontext_t const to, void * vp);
extern "C"
fcontext_t make_fcontext( void * sp, std::size_t size, void (* fn)( int) );


Main.cpp
#include "coroutine.h"
void workPackage(int id){
	printf("Coroutine with id %d is called\n", id);
	coroutine::yield();
	printf("Coroutine with id %d is resumed after first yield\n", id);
	coroutine::yield();
	printf("Coroutine with id %d is resumed after second yield\n", id);
}



int main() {
	coroutine Coro1(workPackage, 1);
	coroutine Coro2(workPackage, 2);
	printf("Main is finished \n");
}


My program compiles but gives segmentation fault during execution. Valgrind gives the following information and i am not able to solve the problem. I will provide the assembly file also if needed. Any help would be appreciated
Use of uninitialised value of size 8
==24145==    at 0x10922B: make_fcontext (in /home/user1/eclipse-workspace/coroutines/Debug/coroutines)
==24145==    by 0x10912D: main (main.cpp:32)
==24145== 
==24145== (action on error) vgdb me ... 
==24145== Continuing ...
==24145== Invalid write of size 8
==24145==    at 0x10922B: make_fcontext (in /home/user1/eclipse-workspace/coroutines/Debug/coroutines)
==24145==    by 0x10912D: main (main.cpp:32)
==24145==  Address 0xffffffffffffffe8 is not stack'd, malloc'd or (recently) free'd

QuestionRe: Cant call an assembly function in c++ code Pin
Richard MacCutchan15-Sep-18 21:16
mveRichard MacCutchan15-Sep-18 21:16 
AnswerRe: Cant call an assembly function in c++ code Pin
meerokh15-Sep-18 23:29
meerokh15-Sep-18 23:29 
GeneralRe: Cant call an assembly function in c++ code Pin
Richard MacCutchan16-Sep-18 1:13
mveRichard MacCutchan16-Sep-18 1:13 
AnswerRe: Cant call an assembly function in c++ code Pin
Joe Woodbury16-Sep-18 19:06
professionalJoe Woodbury16-Sep-18 19:06 
GeneralRe: Cant call an assembly function in c++ code Pin
Richard MacCutchan16-Sep-18 22:04
mveRichard MacCutchan16-Sep-18 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.