C ++ char array with stdin

I am trying to get the size of an array filled with stdin:

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

      

This returns 4 when I enter a string larger with a length greater than 4, for example. "40905898"

Where am I going wrong?

+1


source to share


8 answers


Operator

sizeof

statically estimates the size of the thing you pass to it. A char*

is a pointer that, depending on the architecture of the machine, has a certain size (4 bytes on 32-bit systems and 8 bytes on 64-bit machines). To accomplish what you are trying to do, I suggest using a type string

that can be used by adding #include <string>

along with using namespace std;

to the 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 were trying to do is really dangerous. In fact, when you use cin >> myArray

, you should already allocate some memory for myArray

which you haven't already. This will lead to memory corruption that can cause your program to crash and possibly lead to a buffer overflow.



A simple array in C ++ has no idea about its size. You can sizeof

only use if the array is statically assigned and you are using sizeof

in the array itself and not in another pointer to it, for example this will not 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 in count. You can use sizeof(x)/sizeof(*x)

to find out the number of elements in static arrays. This is not possible for dynamically allocated arrays with new

. In fact, C ++ arrays are very error prone and you have to take extra care when working with them, and in most cases it is better to use vector

and string

.

+11


source


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.

+2


source


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

+1


source


You seem to have a lot of problems:

myArray is not initializing - where will the input go?

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

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

Try using this version:

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

      

This is not without problems (I had to delete myArray), so you should try the answers here using string myArray

0


source


This is 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 (last element is zero or "\ 0"), you can use

strlen(myArray)

      

to get the number of items (minus one). For example:.

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

      

You can also use a statically allocated array, for example:

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

      

0


source


Like others have suggested, myArray is a pointer.

But why don't you use it std::string

? You won't need to do the buffer allocation yourself, which you are doing wrong in your example (pointer myArray

points to nothing)

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

      

You can optionally get a representation of the string pointer with string::c_str()

.

0


source


As others have said, sizeof

returns the size of the object passed to it, in the case of a pointer, the size of the pointer. sizeof

does not follow the pointer to see what size the object it points to (how can he know he can point to one char

or an array, which has no good way of knowing).

Also, when you read from cin

, you need to allocate some space for the data to be read into, cin

won't allocate space for you. You can allocate space either on the stack or on the heap:

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

      

It should be noted that sizeof(stack_line) == 1024

while sizeof(heap_line) == 4

[on 32-bit machines] you must therefore be careful when using the operator sizeof

.

In practice, it is best to use std::string

one who knows how to allocate space.

0


source


Ok if you are going to use sizeof (myArray) you should have done sizeof (* myArray); because with pointers, no star is an address, not a value. Like this:

char *myArray;
cin >> *myArray;
cout << *myArray;

      

0


source







All Articles