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 7576847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:01:24+00:00 2026-05-30T17:01:24+00:00

I have written the following code for my project. The code is scattered across

  • 0

I have written the following code for my project. The code is scattered across various files and quite long so i am posting the minimal code.

#include<stdio.h>
#include<stdbool.h>
struct TwoPoint
{
    int width;
    int value;
};

struct Module 
{
    int categ;
    void *ptr;
};

struct Rect
{
    struct TwoPoint val;
    struct TwoPoint val_new;
    bool is_changed;
};

struct S
{
    int numInstances;
    struct Module instances[20];
    struct Rect RectList[40];
    int numRect;
}s1;

struct Test
{
    int categ;
    struct Rect state;
};

struct TwoPoint initPVal(int v,int w)
{
struct TwoPoint temp;
temp.value=v;
temp.width=w;
return temp;
}
int getValue(struct TwoPoint *b)
{
    return (b->value);
}

struct TwoPoint get(struct Rect *r)
{
    return (r->val);
}

void initialize()
{
     s1.numInstances=0;
     s1.numRect=0;
}

void addRect(struct Rect *r)
{
     if(s1.numRect<40)
     {
     s1.RectList[s1.numRect].val=r->val;
     s1.RectList[s1.numRect].val_new=r->val_new;
     s1.RectList[s1.numRect].is_changed=r->is_changed;
     s1.numRect++;
     }
}

struct Rect initRect(struct TwoPoint initval) 
{
struct Rect temp;
struct TwoPoint tempP;
tempP=initPVal(0,0);
temp.val=initval;
temp.val_new=tempP;
temp.is_changed=false;
addRect(&temp);
return temp;
}

void copy(struct Rect *r)
{
    if(r->is_changed)
    {
    r->val= r->val_new;
    r->is_changed=false;
    }
}
void copyRect()
{
   int i=0;
   for(i=0;i<s1.numRect;i++)
   {    
    copy(&s1.RectList[i]);
   }
}

void setInstance(struct Module *m)
{
    s1.instances[s1.numInstances].categ=m->categ;
    s1.instances[s1.numInstances].ptr=m->ptr;
    s1.numInstances++;
    if (s1.numInstances >= 20)
    {
    printf("Too many instances");
    }
}

void setModule(struct Test *t)
{
    struct Module m;
    m.categ=t->categ;
    m.ptr=&t;
    setInstance(&m);
}



void init(struct Test *t)
{
    t->categ=2;
    struct Rect tr;
    struct TwoPoint tb1=initPVal(0,5);
    tr=initRect(tb1);
    t->state=tr;
}

void actions(struct Test *t)
{
    struct TwoPoint tb=get(&t->state);
    int y=getValue(&tb);
    printf("%d\n",y);
    unsigned int x=getValue(&tb);
    printf("%u\n",x);
    switch(y)
    {
      ....
    }
}

void initS() 
{   
     init(s1.instances[0].ptr);
}   

void act() 
{       
     actions(s1.instances[0].ptr);
}

void setup()
{
    struct Test t;
    initialize();
    init(&t);
    setModule(&t);
}

void run()
{ 
    initS();
    act();
    copyRect();
}


int main()
{
    printf("foo\n");
    setup();
    printf("bar\n");
    run();
    return 0;
}

There are two errors:

  1. The init() function when called through initS() function leads to Stack Overdumped error whereas it worked fine while i called it in setup(). I think Call is correct as action() function is being executed.
  2. The second problem is in actions() function. When i am calculating the value of y to be used as switch condition instead of the value being 0,1,2 or 3 it is some memory address which i found by printing it while trying to debug.
  • 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-30T17:01:26+00:00Added an answer on May 30, 2026 at 5:01 pm

    The problem is:

    void setup()
    {
        struct Test t;
        initialize();
        init(&t);
        setModule(&t);
    }
    

    A Test structure is allocated as a local stack variable and then the address of it assigned to some variable which is accessed later. The next time this variable is accessed is in (init):

    void init(struct Test *t)
    {
        t->categ=2;
        struct Rect tr;
        struct TwoPoint tb1=initPVal(0,5);
        tr=initRect(tb1);
    
        t->state = tr;
    }
    

    At this point the pointer points to a variable that has fallen out of scope, resulting in undefined behaviour. What actually happens is that the stack gets smashed because of the structure assignment which is attempted. This is why it’s also difficult to get a backtrace.

    One solution is to allocate the memory with malloc like so:

    void setup()
    {
        struct Test * t = malloc(sizeof (struct Test));
        initialize();
        init(t);
        setModule(t);
    }
    

    Another problem existed, namely a semantic bug in setModule:

    void setModule(struct Test *t)
    {
        struct Module m;
        m.categ=t->categ;
        m.ptr=&t;
        setInstance(&m);
    }
    

    m.ptr=&t should actually be m.ptr = t. The assignment of a pointer to a Test structure was intended. Instead, what happened was that the address of a stack variable holding a pointer to a Test structure (double pointer to a Test structure) was assigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code in my .net project. I've never used the
I have written a following code in my project: final IndexSearcher indexSearcher = new
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
i have written the following code: As you can see there is a for
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
In physics library written in C# I have the following code: (in ContactManager.cs) public
I have the following code in a project that write 's the ascii representation

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.