EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: Picuino on October 30, 2021, 07:07:46 pm

Title: Memory error in C program (SOLVED)
Post by: Picuino on October 30, 2021, 07:07:46 pm
I was trying to compare speeds between 2 programs in C and in Python.
The program in C gives me a memory error when malloc() is greather than 33000 bytes.

Can anyone help me?
I was trying to compile with Borland C++ command line and with TinyC compiler (both raise an error)

Code: (C) [Select]
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

char *eratosthenes(long n) {
   char *nums, *p;
   long i, x;

   /* Initialize */
   nums = malloc(n+1);
   p = nums;
   *p++ = 0;
   *p++ = 0;
   for(i=2; i<=n; i++)
      *p++ = 1;
   
   /* Find primes */
   for(i=2; i<=n; i++) {
      if(nums[i] == 1) {
         x = i*i;
         while(x<=n) {
            nums[x] = 0;
            x += i;
         }
      }
   }
   return nums;
}

int main() {
   char *nums;
   int i;
   clock_t timeit;
   float t_total;
   
   timeit = clock();
   for(i=0; i<1000; i++) {
      nums = eratosthenes(100000);
      free(nums);
   }
   timeit = clock() - timeit;
   t_total = (float)(timeit) / CLOCKS_PER_SEC;
   printf("time = %f\n", t_total);
}

The program works fine with eratosthenes(10000) and raise memory error with eratosthenes(100000)
Title: Re: Memory error in C program
Post by: Nusa on October 30, 2021, 07:45:16 pm
Good practice to check to see if a malloc succeeded. That'll eliminate the run-time error.

Code: [Select]
nums = malloc(n+1);
if (!nums){ // malloc failed
   return NULL;
}

It would appear that 32K (2**15) is likely the limit for malloc() in your environment or using those compilers.
Title: Re: Memory error in C program
Post by: Andy Watson on October 30, 2021, 07:52:02 pm
What is the size of the long int on your system? If it is four bytes, you might be running into problems when you square i (x = i * I). If the value returned for x is greater than maximum for a signed long int, it can wrap-araound and become negative. If this happens, the test " x <=  n" is going to return true.
Title: Re: Memory error in C program
Post by: Picuino on October 30, 2021, 07:54:18 pm
Thanks, that is the problem.

running program:

Code: (c) [Select]
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

char *eratosthenes(unsigned long n) {
   char *nums, *p;
   unsigned long i, x;

   /* Initialize */
   nums = malloc(n+1);
   p = nums;
   *p++ = 0;
   *p++ = 0;
   for(i=2; i<=n; i++)
      *p++ = 1;

   /* Find primes */
   for(i=2; i<=n; i++) {
      if(nums[i] == 1) {
         x = i*i;
         if (x > n)
            break;
         while(x<=n) {
            nums[x] = 0;
            x += i;
         }
      }
   }
   return nums;
}

int main() {
   char *nums;
   int i;
   clock_t timeit;
   float t_total;
   
   timeit = clock();
   for(i=0; i<100; i++) {
      nums = eratosthenes(1000000);
      free(nums);
   }
   timeit = clock() - timeit;
   t_total = (float)(timeit) / CLOCKS_PER_SEC;
   printf("time = %f\n", t_total);
}
Title: Re: Memory error in C program
Post by: Andy Watson on October 30, 2021, 07:59:54 pm
Going to unsigned long int is only going to get you one more bit of range. If you want to push the boat out a bit further you will have to go to long long ints.
Title: Re: Memory error in C program
Post by: Picuino on October 30, 2021, 08:10:08 pm
Thanks, but that would slow down the program.

The problem is solved with this sentence:
Code: [Select]
         x = i*i;
         if (x > n)
            break;
Title: Re: Memory error in C program
Post by: emece67 on October 30, 2021, 08:10:23 pm
.
Title: Re: Memory error in C program
Post by: Picuino on October 30, 2021, 08:14:57 pm
I was using Borland C for 32 bits. Only have one memory model for 32 bits.

Now, with corrections, the program compile and run correctly with Borland C, TinyC Compiler and GCC.
Title: Re: Memory error in C program
Post by: SiliconWizard on October 31, 2021, 12:32:58 am
Yes, of course, there were Borland C and C++ compilers for Windows, and Windows 32 bits, with just a flat memory model.

16-bit code on x86 required a segmented memory model. A real pain. And so compilers usually supported several "memory models" depending on your projected use of memory, with various tradeoffs.
Title: Re: Memory error in C program
Post by: brucehoult on October 31, 2021, 01:00:05 am
I was trying to compare speeds between 2 programs in C and in Python.
The program in C gives me a memory error when malloc() is greather than 33000 bytes.

Are you running on something older than a 386?
Title: Re: Memory error in C program
Post by: Picuino on October 31, 2021, 08:49:39 am
I thought the same the first time, that it was a memory segmentation problem.
But no, it was a problem with the square of a long number.
It is already solved. Thanks.
Title: Re: Memory error in C program (SOLVED)
Post by: DiTBho on October 31, 2021, 09:32:17 am
ah sweet memories ... Borland Turbo C on an Olivetti Quaderno 33 (PT-AT-60) that my uncle bought abroad and gave me on his return.

I was so excited because it was my first laptop, today it doesn't sound too great ... Intel 80386, 20MHz, 4MB of RAM (later expanded to 12MB by a PCMCIA RAM card), 60MB Hard disk, and MS-DOS v5.0

I remember never using "malloc()" with Turbo C because it was too problematic. I remember using just a static ram construct, kind of "make your malloc on the heap" or something.

~ 25 years ago, I don't remember details  :-//