user1135137
user1135137

Reputation:

Confused with results of sizeof() function

I'm trying to write a function that converts a base 10 integer to a binary number.

I am constructing the function like this:

void dec2bin(int dec, char * bin);

The function will store the binary result by means of the pointer in the argument. It is passed a variable declared elsewhere as follows:

char *r1 = (char *)malloc(24);

The first thing the function has to do is fill the "24 bit result" with all zeros. It works great now since I've coded it based on me knowing the "size," but I can't figure out how to determine the "size" of an arbitrary "r1". Every way I can think to use the sizeof function does not give me 24.

I am confused at this point, and not connecting the relationship between the respective sizes of a char, char*, etc and how I can use those sizes to get what I'm looking for with respect to "r1".

Would someone mind giving me a pointer? Pun intended.

Thanks!

Upvotes: 0

Views: 234

Answers (6)

Doug Kavendek
Doug Kavendek

Reputation: 3694

As others have said, if it's just a pointer, you won't be able to tell anything (unless it's zero-terminated, in which case you can just iterate until you hit an element equal to zero (not the character '0')).

However, if you set it up as follows, you can actually see the buffer size after calling sizeof()... at least until you send it to a function, where the array decays to a plain char* pointer.

void test( char* s )
{
   int i2 = sizeof( s ) / sizeof( char );
}

char c1[] = "Testing!";
int i1 = sizeof( c1 ) / sizeof( char ); // i1 is 9 (size of buffer)
test( c1 );                             // i2 is 4 (size of pointer)

Upvotes: 1

user184968
user184968

Reputation:

You may just pass the size as another parameter:

void dec2bin(int dec, char * bin, size_t max_chars);

Upvotes: 4

Karoly Horvath
Karoly Horvath

Reputation: 96306

When you allocate memory, you get back a pointer, which has a fixed size, so there's no way to get the size of the allocated memory with sizeof.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477494

r1 is a variable of pointer type, and its size is fixed and always the same (just like the size of any variable of a given type), which is no larger than (and usually equal to) sizeof(void *). What you really want to know is the size of the array that's sitting at *r1 (note the asterisk), and that is something you cannot know. You have to keep track of it yourself, typically like this:

size_t const n = get_some_number();
foo * p = calloc(n, sizeof(foo));

Now you use the pair (p, n) to describe your dynamic array.

(Note that C99 and C11 have the notion of a variable-length array, in whose context sizeof does actually (sometimes) give you the actual array size, but that's a somewhat murky affair, and in any case it isn't what you asked for.)

Upvotes: 1

Mr Lister
Mr Lister

Reputation: 46599

The type of r1 is a char* (char pointer), so, like all pointers, its size is 4 (or 8, depending on your situation). No matter the size of the memory block you're allocating to it.

You can't retrieve the size of the allocated block from a pointer, so if you need the size later, you will have to remember the size yoursef (e.g. by storing it in a different variable).

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

I can't figure out how to determine the "size" of an arbitrary "r1".

You cannot do that: the information about the size that you passed to malloc is irreversibly lost after the call. All you have at that point is a pointer to the chunk of at least 24 bytes of memory. If you need the size, you need to store it somewhere else - for example, in a separate variable that you carry around.

Two typical work-arounds to this issue include (1) storing a zero-terminated string in the allocated space, and using the length of that string as the proxy to the size of the allocated chunk, and (2) defining your own structure that contains both the size and the allocated pointer. Neither solution is ideal, but at least the choice is with you.

Upvotes: 3

Related Questions