Overclock.net banner

writing custom data type

586 Views 7 Replies 3 Participants Last post by  rabidgnome229
okay so i was wondering about some how figuring out how to allocate a really large amount of storage for a variable....like i want to be able to find really big Fibonacci numbers...etc...

i wanted to make a 128bit allocation(an extended double)....like extend the data type header file....
1 - 8 of 8 Posts
Language?
i am sorry in C and java
malloc lets you allocate arbitrary amounts of memory in C. You could also create a suitably sized struct

In java, just make a class that does what you need. For large numbers, look at the BigInteger class
thanks....i thought it would be something like malloc....

so i would just do

Code:

Code:
#include <stdlib.h>
#define MAX_SIZE 1000

static void main(){
    static int array[MAX_SIZE];
    int i = 2;
    array[0] = 1;
    array[1] = 1;
    fib = malloc(......); <---whatever the size i wish to make fib....
    fib = array[i-1] + array[i-2];
    array[i] = fib;
}
See less See more
Quote:

Originally Posted by adramalech707 View Post
thanks....i thought it would be something like malloc....

so i would just do

Code:

Code:
#include <stdlib.h>
#define MAX_SIZE 1000

static void main(){
    static int array[MAX_SIZE];
    int i = 2;
    array[0] = 1;
    array[1] = 1;
    fib = malloc(......); <---whatever the size i wish to make fib....
    fib = array[i-1] + array[i-2];
    array[i] = fib;
}
What type is fib in that code snippet? If it's an int there's no reason to malloc it. If it isn't it's doubtful that you will be able to assign an int (the result of array[i-1] + array[i-2]) directly into it. If you're creating your own datatype you need to create your own functions to manipulate it.
See less See more
okay soo i was kindof not thinking when i wrote that little code...i think what i wanted was this....i hope this kindof half way looks like it should work...

type_def struct{
long double* num = malloc(2^128);////<---want to allocate 128bits
}fib_array;

static int main(){
static int i = 2;
fib_array array[MAX_SIZE];
fib_array fib;
array[0].num = 1;
array[1].num = 1;

while(i != MAX_SIZE){
fib.num = array[i-1].num + array[i-2].num;
array = fib.num;
i++;
}
for(i=0; i < MAX_SIZE; i++){
printf("%fib_num", array); ///<---i don't think i formatted this correctly
}
}
See less See more
Quote:


Originally Posted by adramalech707
View Post

okay soo i was kindof not thinking when i wrote that little code...i think what i wanted was this....i hope this kindof half way looks like it should work...

type_def struct{
long double* num = malloc(2^128);////<---want to allocate 128bits
}fib_array;

A few things here. Firstly, malloc takes the number of bytes you want to allocate, not the number of bits. To allocate 128 bits malloc(16) is what you would want. Also, the '^' operator in C performs XOR, it does act as an exponent. And I'm not sure, but I seriously doubt that you can put code in a struct definition. Also, here's no reason to allocate stack space on the heap for a single variable. If you want a struct that has a long double as a field, use

Code:
Code:
typedef struc{[QUOTE]long double num;[/QUOTE]}fib_array;
But if a struct has only one field, there's no reason to use a struct. Just use a long double instead of a struct with a long double in it

Quote:


static int main(){
static int i = 2;
fib_array array[MAX_SIZE];
fib_array fib;
array[0].num = 1;
array[1].num = 1;

while(i != MAX_SIZE){
fib.num = array[i-1].num + array[i-2].num;
array = fib.num;
i++;
}
for(i=0; i < MAX_SIZE; i++){
printf("%fib_num", array); ///<---i don't think i formatted this correctly
}
}




You probably want array instead of array in the highlighted line. Also, it's doesn't make any difference, but when you're iterating through an array it's better style to use a for loop than a while loop

One last point. If you're working with the fibonacci sequence you're working entirely with integers, so you should use a long instead of a double. double's are not precise, so once you get beyond a certain range it no longer stores all the digits in the number
See less See more
1 - 8 of 8 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top