How to implement sizeof api. Means sizeof is already there but i do not want to use that.

Major question is ....

If i want to use size of int. than i can get by pointer but do not know in the case of array of int.

I think that we are passing "array" which is pointer to an integer (or first element of array) to f. How does f know that array has 3 elements? . I think there is something very basic here that I am not getting. Thanks a lot.

You can't implement what sizeof does because it is done by the compiler at compile time, the only way to get sizeof behaviour is to use sizeof.

It you pass an array to a function

void function1(int* parameter)
{
    // Some code using parameter but element count in parameter not known
}

int array[5];

function1(array);

Then the function does not know how many elements where in the array unless you also pass that it, there is no way of knowing this.

void function2(int* array, int size)
{
    // Some code using array, element count given by size
}

int array[5];

function2(array, 5);

In C++ the way to get round this is to use a std::vector. This is an object that encasulates an array and one of the things it maintains is the array size so it does not need to be passed separately

void function3(std::vector<int> vec)
{
    // Some code using vec, vec knows its own element count
}

std::vector<int> array(5);

function3(vec);

Then the function does not know how many elements where in the array unless you also pass that it, there is no way of knowing this.

Then how come compiler knows what is the size of array. Means Till where it will calculate. Is there any flag sort of thing at last of array.

How to implement sizeof api. Means sizeof is already there but i do not want to use that.

You can get the size of a given type T by doing by adding 1 to a T null pointer and then converting the result to a size_t. You can get the size of a variable by taking the address of the variable and the address of the variable + 1, convert both to char* and then calculate then subtract them.

Note that that this technically invokes undefined behavior because it creates pointers to illegal memory, but to the best of my knowledge there is no commonly used compiler on which this would cause a problem. That said there is no reason to do this instead of sizeof other than intellectual curiosity.

I think that we are passing "array" which is pointer to an integer (or first element of array) to f. How does f know that array has 3 elements?

It doesn't. If p is a pointer to int, sizeof(p) will return sizeof(int*), it will not give you the size of the array that p points to. The only context where sizeof tells you the size of an array is if you have an actual array (not a pointer) of static size or, in C99 and above, a VLA. If you pass an array to a function, it will be passed as a pointer and it will not be possible to access the arrays size inside the function.

As an example:

#include <stdio.h>

void f(int arr[]) { /* same as int* arr in this context */
    /* This will print sizeof(int*), not the size of the array */
    printf("%d\n", sizeof(arr));
}

int main(void) {
    int arr[] = {1,2,3};
    /* This will give you the size of the array */
    printf("%d\n", sizeof(arr));

    /* This will not */
    f(arr);
    return 0;
}

Note that in those cases in which sizeof will tell you the size of an array, the pointer trick will as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.