Click here to Skip to main content
15,881,172 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
GeneralRe: Cant call an assembly function in c++ code Pin
meerokh18-Sep-18 6:10
meerokh18-Sep-18 6:10 
Questionlaunch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie13-Sep-18 23:27
zigie13-Sep-18 23:27 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
Victor Nijegorodov14-Sep-18 0:41
Victor Nijegorodov14-Sep-18 0:41 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 17:48
zigie16-Sep-18 17:48 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
Victor Nijegorodov16-Sep-18 20:25
Victor Nijegorodov16-Sep-18 20:25 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
_Flaviu14-Sep-18 0:42
_Flaviu14-Sep-18 0:42 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 18:04
zigie16-Sep-18 18:04 
QuestionRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
David Crow14-Sep-18 3:01
David Crow14-Sep-18 3:01 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 18:14
zigie16-Sep-18 18:14 
QuestionThe mismatch between Print preview and the real printing Pin
Member 1388734912-Sep-18 0:47
Member 1388734912-Sep-18 0:47 
AnswerRe: The mismatch between Print preview and the real printing Pin
Richard MacCutchan12-Sep-18 0:57
mveRichard MacCutchan12-Sep-18 0:57 
GeneralRe: The mismatch between Print preview and the real printing Pin
Member 1388734912-Sep-18 2:03
Member 1388734912-Sep-18 2:03 

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.