1

Possible Duplicate:
Sizeof an array in the C programming language?

I have an array of char and I want to process it in a function, I tried code like this:

int main(){
    char *word = new char [5];      
    /*here we make this word
    ....
    */

    process(word);
    puts(word);
}

void process(char *word){
    int sizeOfWord = sizeof(word)-1;
    /* here is cycle that process the word, I need it lenght to know how long cycle must be
    .....
    */
}

But I can't get the length of array with sizeof. Why? And how can I get that?

Community
  • 1
  • 1
mishkapp
  • 79
  • 1
  • 6
  • [Here's](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) answers to your same question, albeit for C. – benjer3 Jun 15 '12 at 08:41

9 Answers9

3

You can't. With a pointer, there is no way to know the size.

What you should do is, pass the length of word to your process also.


You should know that there is a difference between arrays and pointers. Arrays are indeed a number of elements and therefore sizeof of the array gives its size (in bytes). A pointer on the other hand is just an address. It may not even point to an array. Since the sizeof operator is computed at compile time (except for variable length arrays), it cannot know what you mean.

Think of this example:

void process(char *word);

int main(){
    char *word = new char [5];
    char *word2 = new char [10];

    process(word);
    process(word2);
    /* ... */
}

void process(char *word){
    int sizeOfWord = sizeof(word)-1;  // what should this be?
    /* ... */
}

Now, knowing that sizeof in this case is computed at compile time, what value do you think it should get? 5? 10?


Side note: It looks like you are using this array as a string. In that case, you can easily get its length with strlen.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
2

U should use strlen() instead.

EDIT: Assuming your word is valid word with \0 at the end.

Anyway, you use c++ so you should use some container like std::string.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • `strlen` won't convey the true size of his char array if the `\0` is at an index less than the size. – Jay D Jun 15 '12 at 08:42
  • sure, but OP named his variable `word` so I assume there will be valid word, without white chars. – IProblemFactory Jun 15 '12 at 08:43
  • 2
    @ProblemFactory: but if the word is "the", then the length of the word (even including nul terminator) is less than the size of the array that the word is stored in. So whether the length of the word is any use depends why the questioner wants to know the size of the array. – Steve Jessop Jun 15 '12 at 08:44
2

If your array is always char* or const char* and contains null terminated strings, you can use strlen, or even more easily use the std::string class and its appropriate methods.

If it is not a null terminated string (eg. its a plain byte array, or your question is about arrays in general and the char was just an example), use std::vector<char> or std::array<char, 5> and call their .size() methods.

Rook
  • 5,734
  • 3
  • 34
  • 43
0

No, it won't work. sizeof a pointer returns only the size of the pointer type, not that of the array. When you pass an array as parameter to a function, the array decays into a pointer, which means that the compiler won't know anymore what exactly it points to, and if it is an array, how long it can be. The only ways for you to know it inside the function is

  • pass the length as an additional parameter, or
  • add an extra terminator element to the array.

Note that arrays in C and C++ don't store their size in any way, so it is the programmer who must keep it in mind and program accordingly. In the code above, you know that the actual size of a is 5, but the compiler (and the runtime) has no way of knowing it. If you declared it as an array, i.e.

char[] word = new char [5];

the compiler would keep track of its size, and sizeof would return this size (in bytes) while in the scope of this declaration. This allows one to apply a well known idiom to get the actual size of an array:

int size = sizeof(a) / sizeof(char);

But again, this works only as long as the compiler knows that a is actually an array, so it is not possible inside function calls where a is passed as a (pointer) parameter.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
0

Since arrays "decay" into pointers to their first elements in code like this, you can't. The pointer doesn't "know" how large a block of memory it points at. You need to pass the size separately, e.g.

void process(char *word, size_t length);
unwind
  • 391,730
  • 64
  • 469
  • 606
0

There is only one way to know the length of an array in C/C++: already knowing it. You will need to pass the length of the array as another parameter.

If you are talking specifically about strings, search for the null character in the array and then use its index + 1 as the length (which is basically what the strlen() function does).

Otherwise, perhaps you could just use std::string instead and then get the length property of it?

dodexahedron
  • 4,584
  • 1
  • 25
  • 37
0

You either have to pass the length of the array to the process function, or use the null (\0) character to mark the end.

Assuming the latter you can use

void process(char *word)
{
  while (*word)
  {
    //do stuff with the character *word
    ++word;
   }
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

On Windows there is _msize function to get the allocated memory size on heap in bytes. In VS2005 and VS2008 it worked perfectly with operator new, because it used malloc to allocate the memory. I have to note:

It is not standard way, so you should NOT use it!

Object * addr = new Object[xxx];
size_t number_of_objects = _msize(addr) / sizeof(Object);
Naszta
  • 7,560
  • 2
  • 33
  • 49
0

The other answers have explained in detail why you cannot get the size of a classical C-array, but I want to point out the following.

Your example is missing a

delete [] word;

at the end of main. Otherwise you have a memory leak.

That shows why the preferred way of working with arrays in C++ is using std::vector or -- in case of a fixed length -- the new std::array and in case of character strings std::string. The compiler will manage the underlying storage for you. All of these classes provide a size function.

bjhend
  • 1,538
  • 11
  • 25