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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:59:11+00:00 2026-05-30T18:59:11+00:00

I’m implementing some polynomial arithmetics in C. I use a dynamic struct to store

  • 0

I’m implementing some polynomial arithmetics in C. I use a dynamic struct to store integer factors and the polynomial degree.
Besides other functions I need the operation p[X]*X, so I try to implement some kind of right shift.

But after a few shifts realloc() crashes my program. In this example it’s at the third call, but if I try to shift 2 and 4 times, it crashes after the second.

/* Simple polynom type with variable length and degree n-1. */
typedef struct {
    int n;
    int *factors;
} polynom_t;


polynom_t *poly_init(int n) {
    polynom_t *p_new = malloc(sizeof(polynom_t));
    p_new->n = n;
    p_new->factors = calloc(n, sizeof(int));

    return p_new;
}

void poly_clear(polynom_t *p) {
    free(p->factors);
    free(p);
    p = NULL;
}


void poly_set(polynom_t *p, int a[], int len){
    memcpy(p->factors, a, sizeof(int)*p->n);
}


void _poly_rsz(polynom_t *p, int n) {
    if (n != p->n) {
        p->n = n;

        // This realloc() seems to fail
        p->factors = realloc(p->factors, sizeof(int) * n);
    }
}


void _poly_lsr(polynom_t *p, int i) {
    _poly_rsz(p, p->n + i);
    memmove(p->factors + i, p->factors, sizeof(int)*(p->n));
    memset(p->factors, 0, sizeof(int)*i);
}


int main(int argc, char **argv) {
    polynom_t *p2 = poly_init(11);
    int a2[11] = {1, 2, 0, 2, 2, 1, 0, 2, 1, 2, 0};
    poly_set(p2, a2, 11);
    _poly_lsr(p2, 1);  // works as expected
    _poly_lsr(p2, 1);  // works as expected
    _poly_lsr(p2, 1);  // crash
    poly_clear(p2);

    return 0;
}
  • 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-30T18:59:12+00:00Added an answer on May 30, 2026 at 6:59 pm

    The problem is with this block of code here:

    void _poly_lsr(polynom_t *p, int i) {
        _poly_rsz(p, p->n + i);
        memmove(p->factors + i, p->factors, sizeof(int)*(p->n));  // the problem is here!
        memset(p->factors, 0, sizeof(int)*i);
    }
    

    When you resize your polynomial, you reset it’s count, which means, when you are adding 1, you are overflowing the bounds of your polynomial’s array by 1. To fix, simply subtract i from the memmove count:

    memmove(p->factors + i, p->factors, sizeof(int)*(p->n - i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.