Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:17:24+00:00 2026-05-31T15:17:24+00:00

I am trying to create a simple hello world example using swapcontext() Here is

  • 0

I am trying to create a simple hello world example using swapcontext()

Here is the code snippet:

#include <ucontext.h>
#include <stdio.h>
#include <stdlib.h>

static ucontext_t uctx_main, uctx_func1;

typedef struct test_struct 
{
    ucontext_t  context;
    int value;
}test_struct;

test_struct* array[10];

static void
func1(void)
{
    printf("func1: started\n");
    printf("func1: TESTING\n");
    printf("func1: returning\n");
}
void init()
{
    memset(array,NULL,10);

    array[0] = (test_struct*)malloc(sizeof(test_struct));
    array[0]->value = 10;

    getcontext(&array[0]->context);

    char* func1_stack = (char*)malloc(sizeof(char)*64);
    //char func1_stack[64];

    array[0]->context.uc_stack.ss_sp = func1_stack;
    array[0]->context.uc_stack.ss_size = 64;
    array[0]->context.uc_link = &uctx_main;

    makecontext(&array[0]->context, func1, 0);
}

int main(int argc, char *argv[])
{
   init();

   printf("VALUE: %d\n",array[0]->value);

   swapcontext(&uctx_main, &array[0]->context);

   printf("VALUE: %d\n",array[0]->value);
   printf("main: exiting\n");
   exit(0);
}

But the problem is when the progam executes swapcontext() a segmentation fault occurs. I have fiddled around a bit and I figured out that the problem is the stack size that I am assigning to the context but I don’t know what I am doing wrong.

P.S. I have tried assigning sizeof(func1_stack) but still got a seg fault

Can anyone give me a hint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:17:25+00:00Added an answer on May 31, 2026 at 3:17 pm

    Using 64 as the stack size as coded is consistent with the actual given stack size. However, 64 bytes is fairly small for a stack. The example here uses 16K as the stack size. It may be that the size you are using is simply too small.

    As an aside, the memset is probably not correct. It is setting the first 10 bytes of the array. It is not actually affecting anything, but it should probably be the following:

    memset(array,0,sizeof(array)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm studying webrat and cucumber and trying to create simple example. Here is my
I'm trying to create a very simple Hello, world program in RoR, but when
Hello, i'm trying to take detailed information from the database, i create a simple
Im trying to create a simple pan and zoom app using silverlight 4, but
I'm trying to create a simple example of an editable gridview, and for some
I have spend more than one day trying to create a very simple hello
i am trying to write a boot loader(hello world sort). i am using Bochs
I am trying to create a Socket Hello World program. I would ideally like
I am trying to create a simple example with JNI. I am having trouble
I am trying to run a simple program hello world in my android mobile.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.