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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-08-2006
what3v3r
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! :D
• 02-09-2006
7stud
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.
• 02-09-2006
what3v3r
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 :D
• 02-09-2006
joni
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```
• 02-09-2006
7stud
The size of the value a char pointer points to is always 1.

Quote:

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.
• 02-09-2006
joni
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.
• 02-09-2006
what3v3r
@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!
• 02-09-2006
what3v3r
@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... :D

i just found out that this is not equal
Code:

```  char * x = new char[90];   char x[90];```
• 02-09-2006
what3v3r
@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... :D

i just found out that this is not equal
Code:

```  char * x = new char[90];   char x[90];```
• 02-09-2006
7stud
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().
Quote:

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'.
• 02-09-2006
John_
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.
• 02-09-2006
7stud
Quote:

That code outputs 4.
Yes as noted in the op:
Quote:

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"
• 02-09-2006
John_
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.
• 02-09-2006
7stud
Quote:

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.
• 02-09-2006
John_
I am just asking why it outputs 4, hopefully in laymens terms so I can understand it ;)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last