0

Not only that debugging is out of the question for me now, but the gmp library decided to stop working too.

Because I changed the compiler (see link above), I lost the pre-built gmp library. I successfully configured, rebuild and installed the gmp from the sources (no errors, no warnings, gmp.a and gmp.h generated), adjusted the paths in my code and... abort. Literally.

I use the following kind of lines for "debugging":

printf("\n\n%d\n", __LINE__);

to understand what is being executed.

And the program just aborts at the call of mpz_init2() (line 2362 in the code) in the init() function at the beginning:

printf("\n\n%d\n", __LINE__);
//mp_set_memory_functions ( customGmpMalloc, customGmpRealloc, customGmpFree );

printf("\n\n%d\n", __LINE__);
snprintf ( strForLOG, STR4LOG_LEN, "%s (line %4d): Initializing the MPZ_T numbers (allocating memory) of the bufPSEUDO structure... ", getTimeStamp(0), __LINE__ ); OUT2LOG();

printf("\n\n%d\n", __LINE__);
mpz_init2 ( bufPSEUDO.firstValueInBuf_MPZ        , DIGITS2BITS ( mConfigDigits[ BUF_CNT_BIG - 1 ].cntDigits ) / 2 + 1     ); /* GNU MP */

printf("\n\n%d\n", __LINE__);
mpz_init2 ( crtPseudo_MPZ                        , DIGITS2BITS ( mConfigDigits[ BUF_CNT_BIG - 1 ].cntDigits ) / 2 + 1 + 64); /* GNU MP */

printf("\n\n%d\n", __LINE__);

NOTE: The previous line, as well as the rest of the software were running OK with the pre-built library provided by one of the MinGW installations I have.

It fails equally whether I call or not the mp_set_memory_functions ().

The 3 functions cannot be much simpler. The printf()s there are poor man's debugging, remember? And while(1) is just a pause in the software, so I have a chance to see the output while debugging.

void* customGmpMalloc ( size_t size )
{
    void *ret;

    printf("\n\nmalloc() 1 %d\n\n", __LINE__ );
    ret = malloc (size);
    printf("\n\nmalloc() 2 %d\n\n", __LINE__ );

    if (ret == 0)
    {
        fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
        while ( 1 );
        abort ();
    }

    return ret;
}

void* customGmpRealloc ( void *oldptr, size_t old_size, size_t new_size )
{
    void *ret;

    printf("\n\nrealloc() 1 %d\n\n", __LINE__ );
    ret = realloc (oldptr, new_size);
    printf("\n\nrealloc() 2 %d\n\n", __LINE__ );

    if (ret == 0)
    {
        fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
        while ( 1 );
        abort ();
    }

    return ret;
}

void customGmpFree ( void *blk_ptr, size_t blk_size )
{
    printf("\n\nfree()\n\n");

    free (blk_ptr);
}

None of the printf()s in the custom allocation functions gets executed, so my assumption is that there is a problem with the access to the library itself, rather than being a problem of allocation. To make things even more confusing, mp_set_memory_functions () gets executed.

Codeblocks only returns me:

Process returned 2048 (0x800) execution time : 0.384 s

And I did not find that error on the net.

The actual output is this:

2025-07-23 07:16:18 (line 2317): Preparing log file... OK
2025-07-23 07:16:18 (line 2334): NOTE: Timestamps in GMT (UTC) time, returned by gmtime()...
2025-07-23 07:16:18 (line 2335): GMP version = 6.3.0


2357


2359
2025-07-23 07:16:18 (line 2360): Initializing the MPZ_T numbers (allocating memory) of the bufPSEUDO structure...

2361

Process returned 2048 (0x800)   execution time : 0.384 s
Press any key to continue.

EDIT:

I just made a minimal example, and it works as expected :(

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

mpz_t a;

void* customGmpMalloc  ( size_t size );
void* customGmpRealloc ( void *oldptr, size_t old_size, size_t new_size );
void  customGmpFree    ( void *blk_ptr, size_t blk_size );


void* customGmpMalloc ( size_t size )
{
    void *ret;

    printf("\n\nmalloc() 1 %d\n\n", __LINE__ );
    ret = malloc (size);
    printf("\n\nmalloc() 2 %d\n\n", __LINE__ );

    if (ret == 0)
    {
        fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
        while ( 1 );
        abort ();
    }

    return ret;
}

void* customGmpRealloc ( void *oldptr, size_t old_size, size_t new_size )
{
    void *ret;

    printf("\n\nrealloc() 1 %d\n\n", __LINE__ );
    ret = realloc (oldptr, new_size);
    printf("\n\nrealloc() 2 %d\n\n", __LINE__ );

    if (ret == 0)
    {
        fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
        while ( 1 );
        abort ();
    }

    return ret;
}

void customGmpFree ( void *blk_ptr, size_t blk_size )
{
    printf("\n\nfree()\n\n");

    free (blk_ptr);
}


int main()
{
    printf("\n\n%d\n", __LINE__);

    //mp_set_memory_functions ( customGmpMalloc, customGmpRealloc, customGmpFree );

    printf("\n\n%d\n", __LINE__);

    mpz_init2 ( a, 3321928093 ); /* GNU MP */

    printf("\n\n%d\n", __LINE__);

    return(0);
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.