sizeof: determining the size of char array problems...

This is a discussion on sizeof: determining the size of char array problems... within the C++ Programming forums, part of the General Programming Boards category; how would i know the size of a character array created on heap... Code: char * x = new char[90]; ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    sizeof: determining the size of char array problems...

    how would i know the size of a character array created on heap...

    Code:
    char * x = new char[90];
    std::cout << sizeof(x);  //returns 4 assume the size of pointer
                                         // dereferencing returns 1...
    thanks!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    int size = 90;
    char* x = new char[size];
    
    cout<<size<<endl;
    sizeof() returns the amount of memory occupied by the variable or value--in multiples of the amount of memory a char type occupies.

    A char pointer points to a single char, so if you use sizeof() on the value a char pointer points to, you will get 1.
    Last edited by 7stud; 02-09-2006 at 01:15 AM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    Code:
        char x[90];
        std::cout << sizeof(x); //will result to 90 the size of x
    @7stud
    lol. what if a function has an argument of pointer to a char and i need to determine the size of this char. i need to get the size of the char without knowing the size of char at first.

    thanks

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    If you want to know the length of a string, use strlen. For example:
    Code:
    char x[] = "Hello!";
    int length = strlen(x); // gives 6
    int byte_length = length + 1; // but the char[] has one char more, for the final \0

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The size of the value a char pointer points to is always 1.

    If you want to know the length of a string, use strlen.
    That only works on null terminated char arrays.

    If you create an array on the heap, then you know it's size already.
    Last edited by 7stud; 02-09-2006 at 01:26 AM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    if you are really asking what you are asking, it can't be done.

    A pointer carries no information about how many things it points to. If you need to know the size of an array you need to store it somewhere.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    @joni
    unfortunately i will only work when the character pointer has values. i need is to know the size of the character array so i could determine the maximum number of character that i could place in the array.

    thanks!

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    @joni,

    ok there is no information, but how can i find the size of the array which the pointer points to?

    or really, it can't be done...

    i just found out that this is not equal
    Code:
       char * x = new char[90];
       char x[90];

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    @joni,

    ok there is no information, but how can i find the size of the array which the pointer points to?

    or really, it can't be done...

    i just found out that this is not equal
    Code:
       char * x = new char[90];
       char x[90];

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can ask your question as many times as you want, but at the time you dynamically allocate the memory for your array, you know its size, and that's as good as it's going to get--unless you put a '\0' at the end, in which case you can use strlen().
    how can i find the size of the array which the pointer points to
    A pointer to an array points to the first element of the array, and that's about all you know. One other thing you know is that the memory for the other elements is contiguous to the first element, but you have no idea where that memory ends. If you put a '\0' at the end of your array, then strlen() will step through the contiguous memory until it finds a '\0, and then it will return how many elements it found between the start of the array and the '\0'.
    Last edited by 7stud; 02-09-2006 at 02:03 AM.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by 7stud
    ...

    A char pointer points to a single char, so if you use sizeof() on the value a char pointer points to, you will get 1.
    Code:
    #include <iostream>
    
    using namespace std;
     
    int main(){
     
    	char* MyVar;
    
    	cout << sizeof(MyVar) << endl;
     
    	return 0;
    }
    That code outputs 4.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    That code outputs 4.
    Yes as noted in the op:
    std::cout << sizeof(x); //returns 4 assume the size of pointer
    // dereferencing returns 1...
    Reread this carefully: "on the value a char pointer points to"

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by 7stud
    Yes as noted in the op:

    Reread this carefully: "on the value a char pointer points to"
    Sorry but just to clarify, it is assuming the size of it because it does not know? I just want to make sure I understand

    Code:
    #include <iostream>
    
    using namespace std;
     
    int main(){
     
    	char* MyVar;
    
    	cout << sizeof(*MyVar) << endl;
     
    	return 0;
    }
    That code there outputs 1 like you said. I think I need to get used to the dereference operator a bit more.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Sorry but just to clarify, it is assuming the size of it because it does not know?
    I recognize the words, but I can't make any sense of that statement. You'll have to rephrase your question.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    I am just asking why it outputs 4, hopefully in laymens terms so I can understand it

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

Similar Threads

  1. dynamically setting size of 2d char array
    By waxydock in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 11:58 PM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 08:45 PM
  3. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 07:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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