Thread: malloc arithmetic??

  1. #1
    Registered User
    Join Date
    May 2006

    malloc arithmetic??

    I am tring to build a program that deals with large numbers of 600+ digits, I know that I could not store these in an integer or even an unsigned long long, so I tried to use malloc() with the following syntax:

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    int main()
         int *a = malloc(2048);
         int *b = malloc(2048);
         int *c = malloc(2048);
         scanf("%i", &a);
         scanf("%i", &b);
         c=a/b;     //This is where it fails on compile
         printf("%i /n", c);
         return 0;
    The 2048 bits are just a test value, as is the function of the program (just to find out if I can use arithmetic on malloc() variables).

    The compiler complains of 'Invalid Operands to 'binary''

    Any ideas on how to solve this would be very helpful, I am using Dev C++ with the C compiler function.

    Thanks in advance!


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    You can't divide pointers because the result would not be meaningful. If you are intending to write code to do arbitrary precision arithmetic, then you'll have to implement it. Otherwise, I've just given you a term to Google.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    printf("%i /n", c);
    You probably meant
    printf("%i \n", c);
    scanf("%i", &a);
    This isn't going to work since a is already a pointer . . . .

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2006
    dwks 1: That was a copying mistake, It is correct in the code.

    dwks 2: I wondered about that but I have checked again just now and it does work!

    Dave_Sinkula: Thanks, I put that term into google and have found the GNU Multiple Precision Arithmetic library, which looks to be exactly what I want!

    Thanks Everyone!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Wierd Malloc Problem
    By mohankarthik in forum C Programming
    Replies: 11
    Last Post: 09-17-2008, 02:14 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM