3

I want to define an integer variable in C/C++ such that my integer can store 10 bytes of data or may be a x bytes of data as defined by me in the program. for now..! I tried the

int *ptr;
ptr = (int *)malloc(10);

code. Now if I'm finding the sizeof ptr, it is showing as 4 and not 10. Why?

Martin York
  • 257,169
  • 86
  • 333
  • 562
1s2a3n4j5e6e7v
  • 1,243
  • 3
  • 15
  • 29
  • 1
    Because you're taking the size of a pointer, not what it points to. It could point to anything, there's no way to get the size. You need to track it yourself. Also, C/C++ is not a language, pick one. What you have is C. In C++ we use `std::vector`. – GManNickG Oct 15 '10 at 20:51
  • I'm trying in both C and C++. So mentioned that way!! – 1s2a3n4j5e6e7v Oct 15 '10 at 20:55
  • Why sizeof is returning 4 has been answered by others. But if you want to know how to have larger integers, that is answered here: http://stackoverflow.com/questions/269268/how-to-implement-big-int-in-c – Benjamin Lindley Oct 15 '10 at 20:56
  • You are allocating an array of 10 integers. On most popular desktop platforms (e.g. Windows and GNU/Linux), sizeof(int) is 4 bytes. So what you have are 40 bytes across 10 integers, not even close to what you are looking for. You probably need a bitset. –  Oct 15 '10 at 20:59
  • @1s2: Why? Pick one. Programming in both is not exactly practical in most situations. – GManNickG Oct 15 '10 at 21:04
  • printf("%d",sizeof(long int)); returns 4 too!!! why??? – 1s2a3n4j5e6e7v Oct 15 '10 at 21:05
  • @John: Actually, if his integers are 4 bytes, then he is allocating 2 and a half integers. – Benjamin Lindley Oct 15 '10 at 21:05
  • 2
    @John Gaughan: it's worse than that; he's allocating an array of 10 *bytes* and trying to treat it as an `int`. – John Bode Oct 15 '10 at 21:08
  • Look for a "big integer" library. The OpenSSL library has one that you can set the "width" to. – Thomas Matthews Oct 15 '10 at 21:13
  • @John Bode -- you're right, I don't do much regular C so I glossed over the fact that it was "10" and not "10 * sizeof(int)". His code will work, but not in the way in which he expects. –  Oct 15 '10 at 21:29

7 Answers7

3

C and C++ compilers implement several sizes of integer (typically 1, 2, 4, and 8 bytes {8, 16, 32, and 64 bits}), but without some helper code to preform arithmetic operations you can't really make arbitrary sized integers.

The declarations you did:

int *ptr;
ptr = (int *)malloc(10);

Made what is probably a broken array of integers. Broken because unless you are on a system where (10 % sizeof(int) ) == 0) then you have extra bytes at the end which can't be used to store an entire integer.

There are several big number Class libraries you should be able to locate for C++ which do implement many of the operations you may want preform on your 10 byte (80 bit) integers. With C you would have to do operation as function calls because it lacks operator overloading.

Your sizeof(ptr) evaluated to 4 because you are using a machine that uses 4 byte pointers (a 32 bit system). sizeof tells you nothing about the size of the data that a pointer points to. The only place where this should get tricky is when you use sizeof on an array's name which is different from using it on a pointer. I mention this because arrays names and pointers share so many similarities.

nategoose
  • 12,054
  • 27
  • 42
2

Because on you machine, size of a pointer is 4 byte. Please note that type of the variable ptr is int *. You cannot get complete allocated size by sizeof operator if you malloc or new the memory, because sizeof is a compile time operator, meaning that at compile time the value is evaluated.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
2

It is showing 4 bytes because a pointer on your platform is 4 bytes. The block of memory the pointer addresses may be of any arbitrary size, in your case it is 10 bytes. You need to create a data structure if you need to track that:

struct VariableInteger
{
    int *ptr;
    size_t size;
};

Also, using an int type for your ptr variable doesn't mean the language will allow you to do arithmetic operations on anything of a size different than the size of int on your platform.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
1

Because the size of the pointer is 4. Try something like:

typedef struct
{
    int a[10];
} big_int_t;

big_int_t x;

printf("%d\n", sizeof(x));

Note also that an int is typically not 1 byte in size, so this will probably print 20 or 40, depending on your platform.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Per the requirement of "integer can store 10 bytes", won't `a` be `unsigned char a[ 10 ];`? – Arun Oct 15 '10 at 22:31
0

Integers in C++ are of a fixed size. Do you mean an array of integers? As for sizeof, the way you are using it, it tells you that your pointer is four bytes in size. It doesn't tell you the size of a dynamically allocated block.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
0

Few or no compilers support 10-byte integer arithmetic. If you want to use integers bigger than the values specified in <limits.h>, you'll need to either find a library with support for big integers or make your own class which defines the mathematical operators.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • replace the 8 with `CHAR_BIT` and remember padding bits and trap values and stuff ... the limit, in C99, is `LLONG_MIN` to `LLONG_MAX` or `0` to `ULLONG_MAX` – pmg Oct 15 '10 at 21:19
0

I believe what you're looking for is known as "Arbitrary-precision arithmetic". It allows you to have numbers of any size and any number of decimals. Instead of using fixed-size assembly level math functions, these libraries are coded to do math how one would do them on paper.

Here's a link to a list of arbitrary-precision arithmetic libraries in a few different languages, compliments of Wikipedia: link.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115