How would I go about calling Length() on items on the heap? I tried stuff like:
pointer.length();
pointer->length(); pointer.length(); pointer->length();
&pointer.length()
&pointer->length();
As of now I am kind of stuck so please RSVP if possible
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself. This means that if you pass the array to a function, the function cannot get the size of the array itself since all it has is a pointer to something; in this case, your function needs to have another parameter that gives the array size.
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>>Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself.<<
good point...to the OP...what we are talking about is the following...it applies to my first example...on the stack. On the heap, you always have to do it the pointer way.
How would I go about calling Length() on items on the heap? I tried stuff like:
pointer.length();
pointer->length();
pointer.length();
pointer->length();
&pointer.length()
&pointer->length();
As of now I am kind of stuck so please RSVP if possible
what type of pointer do you have, and what do you mean by length?
Adrian
its a pointer to an char[5000] and isn't length a member function of all types of objects?
NO!
you are thinking of java or javascript or actionscript or even ecmascript.
native types in c/c++ are just that. native. they know nothing. they are not even smart enough to know about themselves.
obag
In my C++ book its shows that you can call .length()...
So what would I use to find the length of an array?
try the following to see...
include <iostream>
using namespace std;
int main()
{
int array[10];
cout << sizeof(array) << endl
<< sizeof(array[0]) << endl
<< sizeof(array) / sizeof(array[0]) << endl;
}
It will work on either heap or stack...
This will print
1)total bytes in array
2)bytes per array element
3)array elements in the array
HTH
not the memory size, but the number of elements
aha! thank you very much!
Sorry...I misspoke...if you do it on the heap like this, it doesn't work...as the sizeof() a pointer is not the same as as the sizeof() an array.
However...since it's on the heap, you must have called new at some point, right??
Therefore you have all the info you need...
include <iostream>
using namespace std;
int main()
{
int size = 10;
int *array = new int[size];
}
Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself. This means that if you pass the array to a function, the function cannot get the size of the array itself since all it has is a pointer to something; in this case, your function needs to have another parameter that gives the array size.
qWake
>>Be careful though, sizeof will not ALWAYS give the size of the array. If I recall the rule correctly, the array variable must be a non-parameter for sizeof to do what you want, otherwise it will just return the size of the pointer itself.<<
good point...to the OP...what we are talking about is the following...it applies to my first example...on the stack. On the heap, you always have to do it the pointer way.
include <iostream>
using namespace std;
void funcCalledIncorrectly(int* anArray){
cout << "I'm in funcCalledIncorrectly" << endl;
cout << sizeof(anArray) << endl
<< sizeof(anArray[0]) << endl
<< sizeof(anArray) / sizeof(anArray[0]) << endl;
}
void funcCalledTheRightWay(int* anArray, int arraySize){
cout << "I'm in funcCalledTheRightWay" << endl;
cout << arraySize * sizeof(anArray[0]) << endl
<< sizeof(anArray[0]) << endl
<< arraySize << endl;
}
int main()
{
int size = 10;
int array[size];
cout << "I'm in main" << endl;
cout << sizeof(array) << endl
<< sizeof(array[0]) << endl
<< sizeof(array) / sizeof(array[0]) << endl;
funcCalledIncorrectly(array);
funcCalledTheRightWay(array, size);
cin.get();
return 0;
}
if it's a zero-terminated string then you can safely use the strlen function
otherwise you can't know the size just from the pointer
Adrian