malloc arithmetic??

This is a discussion on malloc arithmetic?? within the C Programming forums, part of the General Programming Boards category; I am tring to build a program that deals with large numbers of 600+ digits, I know that I could ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    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:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    int main()
    {
    
         int *a = malloc(2048);
              if(a==NULL){exit(EXIT_FAILURE);}
    
         int *b = malloc(2048);
              if(b==NULL){exit(EXIT_FAILURE);}
    
         int *c = malloc(2048);
              if(c==NULL){exit(EXIT_FAILURE);}
    
         scanf("%i", &a);
         scanf("%i", &b);
    
         c=a/b;     //This is where it fails on compile
    
         printf("%i /n", c);
    
         free(a);
         free(b);
         free(c);
    
         system("PAUSE");
         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!

    Phil

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    Location
    Canada
    Posts
    8,048
    Code:
    printf("%i /n", c);
    You probably meant
    Code:
    printf("%i \n", c);
    Code:
    scanf("%i", &a);
    This isn't going to work since a is already a pointer . . . .
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    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!

    Phil

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, 04:46 PM
  2. Wierd Malloc Problem
    By mohankarthik in forum C Programming
    Replies: 11
    Last Post: 09-17-2008, 03: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, 06:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21