-1

I am trying to find out how to find the length of an ADT array type of uint64_t in C.

The code that provides the function for find length of array:

void func(uint64_t *arr , const char *restrict sep){
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("Size of  vektor: %d", size);
}

I am not able to change any variables because it is prohibited by its creator of whole code I can only implement some functions.

code in vector.h file

/* Exported types --------------------------------------------------------------------------------*/
/*! Data type that is stored in the vector. */
typedef uint64_t Vector_DataType_t;

/*! Structure that describes the vector. */
typedef struct {
  /*! Internal pointer to allocated memory. */
  Vector_DataType_t *items;

  /*! Number of currently allocated cells. */
  size_t size;

  /*! Pointer to the next empty cell. */
  Vector_DataType_t *next;

  /*! Number of cells allocated during expanding. */
  size_t alloc_step;
} Vector_t;

main.c file in which I am suppose to implament functionality (I can't post here whole bcse there is more than 200 lines)


int main(void)
{
  bool run = true;
  Vector_DataType_t n;
  printf("Vector test program\n"
         "Type the default size of array and the size of incrementation.\n"
         "Size:\n");
  Vector_t *vector;
  {
    size_t size;

    if (io_utils_get("%zu", &size) == true) { // Change from "false" to true
          return 0;
    }

    printf("Incrementation:\n");

    size_t step;
    if (io_utils_get("%zu", &step) == false) {
      return 0;
    }

    vector = Vector_Create(size, step);
  }

  if (vector == NULL) {
    printf("Memory for vector was not allocated successfully.\n");
    return 0;
  }
Leviathan
  • 5
  • 1
  • 7
  • What you wrote makes no sense. For example what is sizeof(uint64_t[0])? – Vlad from Moscow Mar 08 '22 at 22:36
  • @VladfromMoscow Hi it is trying to divide lenght I suppouse that part is not mine I found it somewhere it also seems like complete nonsene to me – Leviathan Mar 08 '22 at 22:38
  • What is an "STL array in C"? – Fred Larson Mar 08 '22 at 22:39
  • 1
    It doesn't make sense because 1) STL is a C++ thing, not a C thing, and 2) this code uses keywords (actually typedef aliases) for variable and function names. – dbush Mar 08 '22 at 22:39
  • I am sorry guys I just edited that thing it is ADT vector – Leviathan Mar 08 '22 at 22:40
  • An ADT for an array type that doesn't provide a way to find the size of the array is a feeble ADT (not really worthy of the name. The whole point of creating an ADT is to make it easy to use the type — and a vector ADT that doesn't let you know the length of the vector is worse than pathetic. – Jonathan Leffler Mar 08 '22 at 22:51
  • @JonathanLeffler I know I didnt write that code my task is only to implament some functions into it. It also has it's own tests but just btw. – Leviathan Mar 08 '22 at 22:53
  • You need to provide a proper [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that showcases how this function is defined, how its arguments are constructed, and how it is called. Give us something that compiles, or at least comes close. This is a broken snippet currently. – Oka Mar 08 '22 at 22:59
  • You've not given us the details of the ADT which means it is hard to help. A simple pointer to `uint64_t` is not adequate to be an ADT — C provides that level of abstraction. An ADT normally uses a structure type. – Jonathan Leffler Mar 08 '22 at 23:21
  • @JonathanLeffler I edited it so it will be more clear. – Leviathan Mar 08 '22 at 23:40

1 Answers1

1

You cannot find the size of an array passed as a pointer into a function.

The compiler has no way of knowing how big it is, either

  • pass in the length as an arg
  • have a sentinel value, 0, -1, MAX_INT etc

I have ignored the fact that your code very messsed up , I assume you are trying to do this

Note NOTE ==== this code does not work ===================

 void func(uint64_t *arr , const char *restrict sep){
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("Size of  vektor: %d", size);
}
pm100
  • 48,078
  • 23
  • 82
  • 145