How do I get the size of an array from a pointer to the array?

This is a discussion on How do I get the size of an array from a pointer to the array? within the C++ Programming forums, part of the General Programming Boards category; Actually if you know that much (seeing posts waaaay up there) your sizeof() attempt almost works; the basic tactic for ...

  1. #16
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually if you know that much (seeing posts waaaay up there) your sizeof() attempt almost works; the basic tactic for seeing the number of bytes consumed by an array is something like so:
    Code:
    double array[10]; // for example
    int size = sizeof(array[0]) * 10;
    If *all* you have is a ptr then you are screwed unless you get fancy and have debugging turned on, find the bounding array flags (how the debugger knows when you have overwritten the ends of an array), dereference the original pointer and start walking memory till you hit the marker.

    The only thing more fun than debugging with the proper tools is debugging without them.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by jeffcobb View Post
    Code:
    double array[10]; // for example
    int size = sizeof(array[0]) * 10;
    better yet,
    Code:
    double array[10]; // for example
    size_t size = sizeof(array);
    sizeof evaluates to a value of type size_t, which is an unsigned integer (int != integer, int is a kind of integer) type.

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by robwhit View Post
    better yet,
    Code:
    double array[10]; // for example
    size_t size = sizeof(array);
    sizeof evaluates to a value of type size_t, which is an unsigned integer (int != integer, int is a kind of integer) type.
    The problem with that approach is the array is a function parameter, so it defaults to a pointer. And you can't use the sizeof operator on a pointer and get the size.
    That is why I get the size of the array by iterating through it and incrementing a size variable so long as the current character is not a null-terminated character.

    Have either of you actually read the code I linked to a couple posts back? You'll see what I'm talking about.

    EDIT: I see jeffcobb probably only read the first post and responded to it...
    Last edited by Programmer_P; 06-03-2010 at 12:39 PM. Reason: noticed something

  4. #19
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    You already know the length of the string so just pass it in as a parameter if you are so intent on using cstrings in your function. There is no point wasting cycles to figure out something you already know.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    That is why I get the size of the array by iterating through it and incrementing a size variable so long as the current character is not a null-terminated character.
    Ah, so you re-wrote strlen() as well? Not such a big deal.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by syzygy View Post
    You already know the length of the string so just pass it in as a parameter if you are so intent on using cstrings in your function. There is no point wasting cycles to figure out something you already know.
    True, but that's only in my test code. In my real program which uses that function, I'm passing to it a c-style buffer string which stores characters read from a line which will be a case-by-case basis, and hence the length of the actual string(s) is not known (though I suppose I could always call ifstream::count() to get the number of characters read from a line...). Yes, I suppose I could do that.

  7. #22
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    Ah, so you re-wrote strlen() as well? Not such a big deal.
    Yeah, I suppose I did...oops.
    Oh well, it was a good learning practice.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    And the cool thing about my containsString() function is, it will handle any null-terminated strings without any extra work in the caller function. Whereas if I passed in the size of a string to the function, I would need to do that for every function call, and there would be more code in each caller function which calls it. This way, I can just pass in any null-terminated string, and rest assured that my function is able to figure out the size of the strings on its own.

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    Whereas if I passed in the size of a string to the function, I would need to do that for every function call, and there would be more code in each caller function which calls it. This way, I can just pass in any null-terminated string, and rest assured that my function is able to figure out the size of the strings on its own.
    Yeah, that would be a normal way to deal with C-strings -- use strlen() inside the called function, rather than use it first and pass an extra parameter. With most other kinds of arrays, though, you would have to supply the length since there is no way to tell the length. However, you can null terminate pointer arrays and then iterate thru them without knowing the length.
    Last edited by MK27; 06-03-2010 at 01:10 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    That is why you, in C++, never use built-in native arrays. If you really need a static array for some reason (and not std::string), then you use std::array. It interfaces neatly with the STL and even keeps track of its size.
    But I still fail to see why you need an array in the first place. Doesn't getline work for you?
    Last edited by Elysia; 06-03-2010 at 02:26 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.

  11. #26
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    That is why you, in C++, never use built-in native arrays. If you really need a static array for some reason (and not std::string), then you use std::array. It interfaces neatly with the STL and even keeps track of its size.
    But I still fail to see why you need an array in the first place. Doesn't getline work for you?
    Of course it works for me. The function expects a c-style string buffer though. I know I can use string::c_str() to get the cstring representation of the std::string, but...
    I don't know. I started out with a char array buffer before I learned of that function, and I'm too lazy to change it back now.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    ...So why did it expect a c-style buffer in the first place?
    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.

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    ...So why did it expect a c-style buffer in the first place?
    I don't know. Ask the developers of the ifstream class.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    So you are saying you were trying to call a function in the ifstream class that accepted a c-style string?
    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.

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    So you are saying you were trying to call a function in the ifstream class that accepted a c-style string?
    Yeah, ifstream::getline() does.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 10:00 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 07:14 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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