Getting the length of an array

Home Forums C Programming Getting the length of an array

This topic contains 2 replies, has 2 voices, and was last updated by  glimpse 9 years, 3 months ago.

  • Author
    Posts
  • #2132

    glimpse
    Member

    Dear friends,
    When I allocate memory for an array, can I afterwards get an information about the size of the allocated memory?
    The background is a simple function named vectorsum, that I programmed.
    That is:

    I find it a bit stupid, that I have to give the length to the function and it is also bad, that it is restricted to one data type (float*).

    I would be glad to get a fast answer from you.
    Thank you in advance.

  • #3444

    Humayan
    Member

    I don’t think you can information about data requested from the “heap” . The operating system simply returns an address of the dynamic memory from the heap area of the correct size , so I don’t think there is any way to recover this information ( so sending the length of the vector to a function is a good idea ).

  • #3445

    glimpse
    Member
    Thank you. I also can’t imagine how it works. I thought about strings and the function strlen…
    Somehow the program allocates memory and may not write anything else into this memory. The program also has to know the size of the array, when it wants to free the memory again. The information is somewhere. Maybe there is also a kind of end tag at the end of the allocated memory. But that is for sure a very sophisticated matter.

    Pharao

You must be logged in to reply to this topic.

0 Shares
Share
+1
Tweet
Share
Stumble