Getting the length of an array

This is a discussion on Getting the length of an array within the C Programming forums, part of the General Programming Boards category; Dear friends. When I allocate memory for an array, can I afterwards get an information about the size of the ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Munich, Germany
    Posts
    1

    Question Getting the length of an array

    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:
    Code:
    vectorsum(float* vector,int length)
    {
        int i=0;
        float    sum=0;
        for(i=0;i<length;i++)
            sum+=vector;
        return sum;
    }
    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.

    Pharao

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    When I allocate memory for an array, can I afterwards get an information about the size of the allocated memory?
    Generally, no. You have to keep track of the array size, and/or use some specially designated value as a terminator (e.g., the approach with null terminated strings). One exception is with sizeof on an array (but not a pointer to somewhere in the array), where you can compute the number of elements by dividing that by sizeof an element of the array... but then you often already know the size in such a case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you are using a custom memory manager you can typically get the size of a given dynamic block of memory from the memory header. Reading the headers used by malloc() would not necessarily be a very portable solution, however since different malloc()s are designed different ways with entirely different types of headers.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you are using a custom memory manager you can typically get the size of a given dynamic block of memory from the memory header. Reading the headers used by malloc() would not necessarily be a very portable solution, however since different malloc()s are designed different ways with entirely different types of headers.
    Well, yes, the implementation has to know how much memory you've allocated, if only to copy it to a new place if necessary during a realloc() call. I wouldn't suggest going looking for this value yourself, though.

    I find it a bit stupid, that I have to give the length to the function
    That's what you have to do in C. (BTW: it's a good idea to use size_t when you're dealing with the number of elements in an array, since that's what type sizeof evaluates to.)

    and it is also bad, that it is restricted to one data type (float*).
    Well, in C++ you could use templates, but in C you basically have to live with that too. Unless you want to use a macro or something, but that's messy. Don't worry about it too much -- even the standard C library has cos() for doubles, cosf() for floats, and cosl() for long doubles. http://www.opengroup.org/onlinepubs/...tions/cos.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Pharao View Post
    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*).
    You picked the wrong language, friend!
    But if you want something close to hardware, efficient and such (built on C), and more flexibility, I do echo the suggestion to look into C++.
    (Yes, it can solve both your problems.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Unless you want to use a macro or something, but that's messy.

    Not too messy for C99; see tgmath.h.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    @Elysia: how can it solve the problem of having to pass the size of the array to the function? The only way I can see around that is to create a class (or use an existing class like std::vector), but you can make structs in C as well.

    Not too messy for C99; see tgmath.h.
    Yes, I suppose so. But if you can figure out a way to declare a sum variable of the same type as a macro's parameter, and have the macro evaluate to that value, let me know.

    The way I see it, you'd probably have to pass "float" or "double" to the macro, which makes it no better than creating separate functions with different names. Of course, there may be another way to do it, because presumably tgmath.h does.

    But still, templates would be so much simpler in this case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by dwks View Post
    @Elysia: how can it solve the problem of having to pass the size of the array to the function? The only way I can see around that is to create a class (or use an existing class like std::vector), but you can make structs in C as well.
    Well, that was the point. Using std::vector or std::string.
    Because the original need was not to have to keep track of the size yourself, no?

    Plus macros are hard to debug, no? They weren't really made very well for long functions of code.
    Last edited by Elysia; 09-05-2008 at 01:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well, first of all you keep giving the OP C++ specific solutions to his C project. Which doesn't help them a whole lot in the grand scheme of things. And what further makes the point mood is that if the OP were using C++ I would simply tell them to use an STL vector anyway. As would you.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The point is that C can't do all the things the OP wants. It wasn't built for it. C++ is and was therefore given as a suggestion not only by me, but others as well.
    We have provided the facts, and it's up to the OP to decide what to do with that information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was actually speaking more directly to dwk, Elysia. You just kind of snuck a post in while I was writing mine. I agree with what you said 100&#37;. Indeed I would go the C++ route on this one. But I try not to assume the OP knows how to program in C or C++ with any level of confidence until I observe otherwise.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Because the original need was not to have to keep track of the size yourself, no?
    No, the original problem was that the size needed to be passed to the function. There's no way around this. Either you pass in the size somehow, as a separate parameter or as part of a class or struct, or through some obscure method like global variables. Or you hard-code it. Basically, there's no way around it, C or C++ nonwithstanding.

    [edit] Speaking of snuck-in posts -- two happened to me!

    As for me giving the OP C++-specific solutions . . . I already said "not to worry about it too much". People, even the designers of the original C89 standard library, accepted the necessity of having to create different functions for different variable types.

    But since the OP was looking for ways around this, I supplied the only two I could think of. Macros, and templates. Neither of which is much use here.

    So -- don't worry about it too much. Make different versions if you have to. [/edit]
    Last edited by dwks; 09-05-2008 at 01:18 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by master5001 View Post
    I was actually speaking more directly to dwk, Elysia. You just kind of snuck a post in while I was writing mine...
    Oh! Right, sorry about that :)

    Quote Originally Posted by dwks View Post
    No, the original problem was that the size needed to be passed to the function. There's no way around this. Either you pass in the size somehow, as a separate parameter or as part of a class or struct, or through some obscure method like global variables. Or you hard-code it. Basically, there's no way around it, C or C++ nonwithstanding.
    While I don't agree, I won't further argue it ;)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Elysia View Post
    While I don't agree, I won't further argue it ;)
    Ditto.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You guys type too fast for me.

    I guess I'll just finish off by mentioning this . . . if you know that the argument you're passing is in fact an array (and not a pointer), you can use something like this.
    Code:
    #define vectorsum_array(vector) \
        vectorsum(vector, sizeof vector / sizeof *vector)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. the length of my array is not what I expected
    By luca in forum C Programming
    Replies: 7
    Last Post: 12-05-2006, 02:14 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21