how would i know the size of a character array created on heap...
thanks!Code:char * x = new char[90]; std::cout << sizeof(x); //returns 4 assume the size of pointer // dereferencing returns 1...
how would i know the size of a character array created on heap...
thanks!Code:char * x = new char[90]; std::cout << sizeof(x); //returns 4 assume the size of pointer // dereferencing returns 1...
sizeof() returns the amount of memory occupied by the variable or value--in multiples of the amount of memory a char type occupies.Code:int size = 90; char* x = new char[size]; cout<<size<<endl;
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.
@7studCode:char x[90]; std::cout << sizeof(x); //will result to 90 the size of x
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
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
The size of the value a char pointer points to is always 1.
That only works on null terminated char arrays.If you want to know the length of a string, use strlen.
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.
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.
@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!
@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];
@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];
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().
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'.how can i find the size of the array which the pointer points to
Last edited by 7stud; 02-09-2006 at 02:03 AM.
Originally Posted by 7studThat code outputs 4.Code:#include <iostream> using namespace std; int main(){ char* MyVar; cout << sizeof(MyVar) << endl; return 0; }
Yes as noted in the op:That code outputs 4.
Reread this carefully: "on the value a char pointer points to"std::cout << sizeof(x); //returns 4 assume the size of pointer
// dereferencing returns 1...
Sorry but just to clarify, it is assuming the size of it because it does not know? I just want to make sure I understandOriginally Posted by 7stud
That code there outputs 1 like you said. I think I need to get used to the dereference operator a bit more.Code:#include <iostream> using namespace std; int main(){ char* MyVar; cout << sizeof(*MyVar) << endl; return 0; }
I recognize the words, but I can't make any sense of that statement. You'll have to rephrase your question.Sorry but just to clarify, it is assuming the size of it because it does not know?
I am just asking why it outputs 4, hopefully in laymens terms so I can understand it