Topic
4 replies Latest Post - ‏2012-04-03T13:52:06Z by SystemAdmin
Maksymas
Maksymas
9 Posts
ACCEPTED ANSWER

Pinned topic Equivalent for ALGOL SIZE function in C

‏2010-10-07T10:23:37Z |
Dear All

Can you suggest me a equivalent SIZE function in ALGOL for C language
In ALGOL- The SIZE function returns a single dimentional size of passed pointer
In C- While we pass the array as a argument to another function - we are not getting the size exactly from the main caller
example:
fun(char arr[]) {printf("\n %d", sizeof (arrg));}
main
{
char arrg50 = "LALALA";
fun(arrg);
}

output is
4
it returns the sizeof the base address
the expected o/p is 50

The data passed from the caller may have nulls and lot of hex values in the middle- hence it hinders the use of strlen
can you please suggest the solution for this

Thanks
maksymas
Updated on 2012-04-03T13:52:06Z at 2012-04-03T13:52:06Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    196 Posts
    ACCEPTED ANSWER

    Re: Equivalent for ALGOL SIZE function in C

    ‏2010-10-08T06:54:19Z  in response to Maksymas
    Unfortunately there is no C equivalent to the ALGOL SIZE function. Under the covers, in order to implement something like SIZE, the variable "arg" in your example would have to somehow record it's size (50) along with the starting address of it's contents. That way arg could be passed from function to function, and a query by SIZE would be able to determine it's original declared length.

    The C language was originally designed to allow efficient code generation with even a simple compiler, so there are very few bells and whistles. When passed as a parameter to functions, just the starting address of the array is passed--there is no meta information passed around.

    So, what can you do? As you say, strlen() will not work since these arrays may contain nulls. Once possible solution is to reserve some space at the beginning of the array to contain the original size. The downside is that you would have to take this into account whenever you created an array, and manually assign the size. Here is a possible example:

    #define SETSIZE(a,value) {char *_ptr = (char *)(a); _ptr = _ptr-4; *((int*)_ptr) = (int)value;}
    #define GETSIZE(a,theSize) {char *_ptr = (char *)(a); _ptr = _ptr-4; theSize = *((int*)_ptr);}
    extern void foo(char *);
    int main(int argc,char *argv[]) {

    char *arg;
    char rawarray50+4; /* reserve 4 bytes for the size */
    arg = &(rawarray[4]); /* arg points to storage of size 50 bytes */
    SETSIZE(arg,50) ; /* must explicitly set the size by hand */
    strcpy(arg,"LALALA");
    foo(arg);
    return 0;
    }

    void foo(char *arr){
    int size;
    GETSIZE(arr,size);
    printf("\n %d",size);
    }
    void foo(char *arr){
    printf("\n %d",SIZE(arr));
    }
  • Maksymas
    Maksymas
    9 Posts
    ACCEPTED ANSWER

    Re: Equivalent for ALGOL SIZE function in C

    ‏2010-10-08T08:27:30Z  in response to Maksymas
    Thanks a lot for the response

    Yeah it works fine...
    But we will be having a call from program to library - That is seperate files- linked by sysdefsds

    instead of applying the solution above, I suppose we can directly pass the size as a third parameter..

    Is there any other efficient way to get size of a array from caller program
    Please suggest
    Thanks
    Maksymas
    • brataj
      brataj
      11 Posts
      ACCEPTED ANSWER

      Re: Equivalent for ALGOL SIZE function in C

      ‏2010-10-09T01:45:31Z  in response to Maksymas
      I think Chris has explained the situation admirably--the callee can't tell the caller's array size unless it's been told.

      With some compilers you can derive the number of elements in array with the expression
      sizeof(array) / sizeof(typeof(array))
      

      but not all compilers support typeof().
      Updated on 2014-03-24T22:18:07Z at 2014-03-24T22:18:07Z by iron-man
  • SystemAdmin
    SystemAdmin
    196 Posts
    ACCEPTED ANSWER

    Re: Equivalent for ALGOL SIZE function in C

    ‏2012-04-03T13:52:06Z  in response to Maksymas
    @Maksymas:

    I'm curious as to how you arrived at this problem - ALGOL hasn't been used by many people for a very long time...