views:

2019

answers:

8

I am trying to get the size of an array populated by stdin:

char *myArray;
cin >> myArray
cout << sizeof(myArray);

This returns 4 when I enter a string greater with a length greater than 4 e.g. "40905898"

Where am i going wrong?

+2  A: 

sizeof(pointer) will always return 4. You want to use strlen().

Edit: IIRC, sizeof is evaluated at compile time, it only cares about the type, not the content.

Leeor Aharon
You assume a 32-bit CPU. On a 64-bit CPU it'll return 8.
csl
@csl it will return 4 on 64 bit CPUs if you compile it as 32 bit ;)
Mehrdad Afshari
A: 

This is because myArray is a pointer that occupies 4 bytes. If you want to get the length of your string, use strlen or something similar.

schnaader
+11  A: 

sizeof operator statically evaluates to the size of the thing you are passing to it. A char* is a pointer which, depending on the machine architecture has a specific size (4 bytes on 32 bit systems and 8 bytes on 64 bit machines). To accomplish what you are trying to do, I suggest you use the string type which you can use by adding #include <string> along with using namespace std; to your source file.

string line;
cin >> line;
cout << line.length() << endl;

It's less error prone and easier to use.

By the way, the thing you've tried to do is really dangerous. In fact, when you use cin >> myArray, you should have already allocated some memory for myArray which you haven't done. This will cause memory corruption which might crash your program and possibly put it to buffer overrun attacks.

A simple array in C++ has no idea about its size. You can use sizeof only if the array is statically allocated and you are using sizeof on the array itself, not another pointer to it, for example this won't work as you might expect:

int x[5];
int *a = &x[0];
// a[i] is now the same as x[i] but:
cout << sizeof(x) << endl; // prints 20, assuming int is 32 bits long
cout << sizeof(a) << endl; // prints 4, assuming a pointer is 32 bits long

Note that the total size of the array is printed on the first line, not the element count. You can use sizeof(x)/sizeof(*x) to find out element count in static arrays. This thing is not possible for dynamically allocated arrays using new. In fact C++ arrays are very error prone and you should take extreme care when working with them and you'd better use vector and string in most cases instead.

Mehrdad Afshari
my idea exactly!
xtofl
A: 

You seem to have lots of problems here:

myArray is not initialised - where is the input going to live?

You usually use: cin >> myArray; (Note the direction of the chevrons and the semi-colon)

sizeof(myArray) will always return the same value (4 on your platform)

Try this version instead:

char* myArray= new char[50];
cin >> myArray;
cout << myArray;
cout << strlen(myArray);

Its not without its own problems (I should have deleted myArray), so you should try the answers here that use string myArray

quamrana
delete myArray; // !!
xtofl
Well, that's right - that's what you'd do if you were calling a function. I was testing this in main(), so it was cleaned up.
quamrana
A: 

It's because you are using sizeof() on a pointer, which is 4 bytes on your 32-bit computer:

printf("Pointer size: %d\n", sizeof(void*));

If your array is a null-terminated string (the last element being a zero-byte, or '\0'), then you can use

strlen(myArray)

to get the number of elements (minus one). E.g.:

myArray = "Hello, world!";
printf("Number of characters: %d\n", strlen(myArray));

You could also use a statically allocated array, like this:

 char array[128];
 printf("sizeof(array) = %d\n", sizeof(array));
 // prints 128
csl
A: 

As others said, myArray is a pointer.

But why wouldn't you use std::string? You won't need to do the buffer allocation yourself, which you do wrongly in your example (pointer myArray points to nothing)

std::string myValue;
std::cin >> myValue;
std::cout << myValue.length();

If needed, you can get to a pointer representation of the string by using string::c_str().

xtofl
A: 

As others said sizeof returns the size of the object passed to it, in the case of a pointer it's the size of the pointer. sizeof does not follow the pointer to see what size the object it points to is (how can it know it could point to one char or to an array there's not good way to know).

Additionally when you read from cin you have to allocate some space into which the data must be read, cin will not allocate space for you. You can allocate space either on the stack or the heap:

char stack_line[1024]; // This will overflow if more than 1024 chars are needed
char heap_line*  = new char[1024]; // ditto

The thing to note here is that sizeof(stack_line) == 1024 while sizeof(heap_line) == 4 [on 32 bit machines] so you must be careful when using the sizeof operator.

In practice it's better to use an std::string which knows how to allocate the space itself.

Motti
A: 

This has been very helpful, thanks to you all.