#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    Finding size of a character array?


    How would I find the size of a dynamically allocated character array? Here is my code so far:

    #include <iostream>
    using namespace std;
    void main()
    {
    char *test = new char[];
    int i = 0;

    cout << "Type in name!\n";
    cin >> test;
    for (i; i<sizeof(*test); i++)
    {
    cout << test[i];
    }
    }

    The sizeof operator doesn't seem to be working..could I use scanf()? Thanks in advance.

    -andy
    hmmm...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    Just like with a non-dynamically created array:

    strlen() in the <cstring> header file
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Is there any other way without having to add another header file?

    EDIT:: NM..STRLEN IS PART OF IOSTREAM ..Thanks stud.

    -andy
    hmmm...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    MSDN says it's in the <string.h> header file which is <cstring> under the new standard, so I don't know why you don't need to include <cstring>, but apparently it works with just <iostream> included.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222

    Re: Finding size of a character array?


    Originally posted by andy3109
    ...
    for (i; i<sizeof(*test); i++)

    ...

    The sizeof operator doesn't seem to be working..could I use scanf()? Thanks in advance.

    -andy
    test is a char pointer, so *test is a char. If you output the value of sizeof(*test), it should be 1.

    I know you're using C++, but in both C and C++ the length of a character string is returned by strlen(), whose header file is string.h in both C and C++. However, the value it returns does not include the null-terminator.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    18
    Rep Power
    0
    The strlen() function only returns the length till the '\0' appears. if you want to know the length of the allocated memory you have to save it in an extra variable. Another solution is to make char array one field larger and inserting a special character at this position. Then you could calculate the size. If the length of your char array is less than 255 you could also save this value in the first character field.

    BTW
    Code:
    char *test = new char[];
    does not allocate any memory for the content of the array. it only allocates a char-array pointer.

    Here's some code sample. (Hope the compiler get's it, because it's a long time ago since i've coded c/c++):

    Code:
    // some variables
    char *test = null;  // array pointer
    int size = 10;        // size of array
    
    // allocate memory for array dynamically
    test = new char[size+1];
    
    // solution one: save special character
    test[size] = 0xFF;
    
    // alternative: save len in field
    test[0] = (char) size;
    
    // find len of array (solution one)
    char* ptr = null;
    int len = 0;
    
    while((*ptr) != 0xFF) {
      len++; // increase length
      ptr++; // increment pointer
    }
    
    // alternative: find len of array (save len in field)
    len = (int) test[0];
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    >> If the length of your char array is less than 255 you could also save this value in the first character field.

    IIRC, this was the way that Turbo Pascal (versions <= 3.0) stored string variables. You couldn't declare strings greater than 255 chars in length. The programmer could access individual characters via string[1], string[2] etc., but the compiler wouldn't let the programmer access string[0], because this was the internal variable. You could access it by using a pointer though, but it wasn't such a good idea to modify it, for obvious reasons :D.

    The problem with using this scheme with C/C++ is that string[0] is accessible by the programmer and can be easily modified. Also, your common string handling functions, such as strcmp(), strcat(), sprintf() etc. would have to be rewritten to skip over the first character. Either that, or you have to pass pointer to the second array element to strcmp(), sprintf() etc., instead of the first element.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    18
    Rep Power
    0
    >> IIRC, this was the way that Turbo Pascal (versions <= 3.0) stored string variables. You couldn't declare strings greater than 255 chars in length. The programmer could access individual characters via string[1], string[2] etc., but the compiler wouldn't let the programmer access string[0], because this was the internal variable. You could access it by using a pointer though, but it wasn't such a good idea to modify it, for obvious reasons .

    Yes, I know. That's were i got the idea from. I thought andy3109 was searching for solutions without using an extra variable. I also thought he is not working with strings (in that case he could also string from the STL).
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0
    sizeof is a compiler calculator, it is not a funcrtion.
    sizeof(*test) probably == sizeof(char)
    strlen(test) is a standard solution
    but char* is strange idea
    if you do not whant headache use STL string,
    or MFC CString.

IMN logo majestic logo threadwatch logo seochat tools logo