Click here to Skip to main content
13,768,884 members

C / C++ / MFC

 
JokeRe: Want to learn C. Newbie. Where to start? Pin
CPallini17-Sep-18 23:31
mvpCPallini17-Sep-18 23:31 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Victor Nijegorodov18-Sep-18 0:05
memberVictor Nijegorodov18-Sep-18 0:05 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Dar Brett18-Sep-18 5:54
memberDar Brett18-Sep-18 5:54 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
CPallini18-Sep-18 11:28
mvpCPallini18-Sep-18 11:28 
AnswerRe: Want to learn C. Newbie. Where to start? Pin
Joe Woodbury18-Sep-18 8:09
professionalJoe Woodbury18-Sep-18 8:09 
GeneralRe: Want to learn C. Newbie. Where to start? Pin
Quickbeam121318-Sep-18 8:17
memberQuickbeam121318-Sep-18 8:17 
AnswerRe: Want to learn C. Newbie. Where to start? Pin
Dar Brett18-Sep-18 21:17
memberDar Brett18-Sep-18 21:17 
QuestionCant call an assembly function in c++ code Pin
meerokh15-Sep-18 0:09
membermeerokh15-Sep-18 0: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 22:16
protectorRichard MacCutchan15-Sep-18 22:16 
AnswerRe: Cant call an assembly function in c++ code Pin
meerokh16-Sep-18 0:29
membermeerokh16-Sep-18 0:29 
GeneralRe: Cant call an assembly function in c++ code Pin
Richard MacCutchan16-Sep-18 2:13
protectorRichard MacCutchan16-Sep-18 2:13 
AnswerRe: Cant call an assembly function in c++ code Pin
Joe Woodbury16-Sep-18 20:06
professionalJoe Woodbury16-Sep-18 20:06 
GeneralRe: Cant call an assembly function in c++ code Pin
Richard MacCutchan16-Sep-18 23:04
protectorRichard MacCutchan16-Sep-18 23:04 
GeneralRe: Cant call an assembly function in c++ code Pin
meerokh18-Sep-18 7:10
membermeerokh18-Sep-18 7:10 
Questionlaunch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie14-Sep-18 0:27
memberzigie14-Sep-18 0:27 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
Victor Nijegorodov14-Sep-18 1:41
memberVictor Nijegorodov14-Sep-18 1:41 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 18:48
memberzigie16-Sep-18 18:48 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
Victor Nijegorodov16-Sep-18 21:25
memberVictor Nijegorodov16-Sep-18 21:25 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
_Flaviu14-Sep-18 1:42
member_Flaviu14-Sep-18 1:42 
GeneralRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 19:04
memberzigie16-Sep-18 19:04 
QuestionRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
David Crow14-Sep-18 4:01
memberDavid Crow14-Sep-18 4:01 
AnswerRe: launch a word processor(MS Word) in an MS Visual C++ dialog based application Pin
zigie16-Sep-18 19:14
memberzigie16-Sep-18 19:14 
QuestionThe mismatch between Print preview and the real printing Pin
Member 1388734912-Sep-18 1:47
memberMember 1388734912-Sep-18 1:47 
AnswerRe: The mismatch between Print preview and the real printing Pin
Richard MacCutchan12-Sep-18 1:57
protectorRichard MacCutchan12-Sep-18 1:57 
GeneralRe: The mismatch between Print preview and the real printing Pin
Member 1388734912-Sep-18 3:03
memberMember 1388734912-Sep-18 3: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.


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181117.1 | Last Updated 18 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid