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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:07:10+00:00 2026-06-13T22:07:10+00:00

I have a struct like this: typedef struct{ size_t length; // length of the

  • 0

I have a struct like this:

typedef struct{
    size_t length; // length of the array
    size_t numbits; // number of bits allocated per val in vals
    mpz_t vals[]; // flexible array to hold some number of mpz_t array
} CoolArray;

Ok, so it’s a normal flexible array, and I should be able to use malloc to set
it’s size:

void initArray(CoolArray* array, size_t length, size_t numbits){
    assert(length>=1); // don't make arrays with a length<=0

    // first I allocate memory for vals...
    array->vals = (mpz_t*) malloc(sizeof(CoolArray)+length*sizeof(mpz_t));

    // then I allocate memory for each val in vals
    mpz_array_init(array->vals, (size_t)length, numbits);

    return;
}

but when I try to use this, I get a segmentation fault. I also get complaints
about incorrect use of mpz_array_init. But I’ve looked at the manula, and it
seems I am doing this correctly.

I also tried to use my struct like this:

typedef struct{
    size_t length; // length of the array
    size_t numbits; // number of bits allocated per val in vals
    mpz_t* vals; // pointer to start of array
} CoolArray;

and I also changed my initArray function to this:

void initArray(CoolArray* array, size_t length, size_t numbits) {
    assert(length>=1); // don't make arrays with a length<=0

    // first I allocate memory for vals...
    array->vals = (mpz_t*) calloc(length, sizeof(mpz_t));

    // then I allocate memory for each val in vals
    mpz_array_init(array->vals, (size_t)length, numbits);

    return;
}

This one doesn’t segfault, but I get complaints at compile time about
incorrect usage of mpz_array_init, and I also get a bunch of malloc errors
in my output, along with the output I want to see. Can anyone tell me where my
code is incorrect? why does the first version segfault? I did it the way
people seem to recommend. And why does the compiler complain about
mpz_array_init being used incorrectly?

This is the sort of error i get in my output:

gmpascal(80964) malloc: *** error for object 0x100801088: Non-aligned
pointer being freed *** set a breakpoint in malloc_error_break to debug

P.S. gmpascal is the name of my executable, it computes the nth row of
pascals triangle.

P.P.S. I’m compiling with gcc-4.2 on a Powermac with these flags:

-arch ppc64 -o gmpascal gmpascal.c -lgmp -Wall

Is there something I missing here?

  • 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-06-13T22:07:11+00:00Added an answer on June 13, 2026 at 10:07 pm

    Keep in mind, I do NOT program gmp, but a tail-dynamic buffer in a struct is usually implemented something like this (adapted to how I think you want to use it):

    typedef struct
    {
      size_t length;  //length of the array
      size_t numbits; //number of bits allocated per val in vals
      mpz_t vals[1];  //flexible array to hold some number of mpz_t array
    } CoolArray;
    

    Allocation strategy, knowing the the number of values and bit-depth, would be:

    CoolArray* allocArray(size_t length, size_t numbits)
    {
       CoolArray *p = malloc(sizeof(*p) + sizeof(mpz_t)*length);
       p->length = length;
       p->numbits = numbits;
       mpz_array_init(p->vals, length, numbits);
       return p;
    }
    

    Freeing it (just a wrapper for free(), but you may need to do some gmp-cleanup I’m unfamiliar with):

    void freeArray(CoolArray **pp)
    {
        if (*pp)
        {
            free(*pp);
            *pp = NULL;
        }
    }
    

    Using it:

    CoolArray *pca = allocArray(length, numbits);
    

    Freeing it when done:

    freeArray(&pca);
    

    These are just ideas, but maybe you can get something from them.

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

Sidebar

Related Questions

Ok so I have struct like this typedef struct { float x; float y;
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
If I have a struct like this: typedef struct { unsigned char c1; unsigned
Hi I have a struct like this typedef struct { string firstname; string lastname;
I have a C struct like this: typedef struct { my_domain_type_t type; my_domain_union_t u;
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have a struct that I initialize like this: typedef struct { word w;
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a structure to represent strings in memory looking like this: typedef struct
I have a struct like this: typedef struct _HEADER_IO { uint8_t field1 : 2;

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.