-
sizeof() EZ question
Hi everyone!!
Got a little problem...
First I'm passing out as a char array the following variable :
Code:
Edit1->Text.c_str()
Edit1 being a TEdit.
So I'm passing it to a second function that gets it and is supposed to give it's size by executing the following code :
Code:
int Length = sizeof(String);
ShowMessage(AnsiString(Length));
(String is the variable name I gave to the char array coming into the second function) (also, I know there's no goal by this program, this is just a test on a bigger one ;))
But what the message box is always showing out is "3"... Whatever I put in the TEdit's it's always 3 that is shown in the msgbox; any idea?
Thx in advance!!!
VV
[email protected]
-
>But what the message box is always showing out is "3"
sizeof(String), where String is a char*, is going to return the memory used to hold a pointer (not the memory size of what it points to). This would be 4 on under a 32bit environment, as in 4 bytes. Don't know why you're finding it to be 3.
If you want the size need for a char* array, do the following:
Code:
int Length = strlen(String) + 1;
ShowMessage(Length);
A char is always 1 byte. The +1 is to take account of the NULL terminator.
-
yup, okay, thanks for the info about the fact that sizeof was getting only the pointer's size...
it helped...
but BTW... now I'm wanting a way to know the number of integer an array that is passed to that function contains... I know an integer is two byte...
so it should return me .. say 20 if there was 10 integers in it... obviously it returns me 2 cauz I did the same error I did with my char array up this page.
So how should I proceed to know how many integers are on an integer array then?!!??! how do I bring sizeof to calculate not only the size of the pointer but the size of the data stocked under the pointer?!?!
Thx in advance!!
VV
-
An integer is 4 bytes. A short is 2 bytes.
Here is some information on getting number of elements in an array
Code:
// Assuming proper headers and such..
#define SSIZE(n) sizeof(n)/sizeof(n[0])
int nArray[ 4 ] = { 1, 2, 3, 4 };
cout << SSIZE(nArray) << " Elements in the array" << endl;
cout << "One element is " << sizeof( nArray[0] ) << " bytes long" << endl;
cout << "The entire array is " << sizeof( nArray ) << " bytes long" << endl;
The SSIZE macro takes the size in bytes of the entire array and divides out the size of an individual element to determine the number of total elements. Hope this helps you.
-
ohh.. this is fun :)
hehe thanks a lot!
I'll try this! :D
see ya l8r!
VV
-
doh
Returns one.... here's a paste of a part of the code so it could maybe give a clue...
Code:
#define SSIZE(n) sizeof(n)/sizeof(n[0])
int SortInt(int *Array)
{
int Temp;
int Length = SSIZE(Array);
ShowMessage(AnsiString(Length));
Gives always "1" now...
Any other idea???
BTW, this is how my int array is declared in another function :
Code:
int Array[] = {StringToInt(Edit1->Text.c_str()), StringToInt(Edit2->Text.c_str()),
StringToInt(Edit3->Text.c_str()), StringToInt(Edit4->Text.c_str()),
StringToInt(Edit5->Text.c_str()), StringToInt(Edit6->Text.c_str()),
StringToInt(Edit7->Text.c_str()), StringToInt(Edit8->Text.c_str())};
And as far as I know, it's okay, I tested it with some ShowMessage()'s
-
Re: doh
Quote:
Originally posted by V1P3R V3N0M
Returns one.... here's a paste of a part of the code so it could maybe give a clue...
Code:
#define SSIZE(n) sizeof(n)/sizeof(n[0])
int SortInt(int *Array)
{
int Temp;
int Length = SSIZE(Array);
ShowMessage(AnsiString(Length));
Gives always "1" now...
Any other idea???
BTW, this is how my int array is declared in another function :
Code:
int Array[] = {StringToInt(Edit1->Text.c_str()), StringToInt(Edit2->Text.c_str()),
StringToInt(Edit3->Text.c_str()), StringToInt(Edit4->Text.c_str()),
StringToInt(Edit5->Text.c_str()), StringToInt(Edit6->Text.c_str()),
StringToInt(Edit7->Text.c_str()), StringToInt(Edit8->Text.c_str())};
And as far as I know, it's okay, I tested it with some ShowMessage()'s
Because you're passing your array to a funtion as a pointer. So when you use my macro you say sizeof a pointer ( always 4bytes on 32-bit machines ) divided by size of the first element ( also 4 bytes in this case, integer ) so the result is 1.
-
If a pointer to an array is actually a pointer to the first object in the array, the macro is taking the size of the first element and dividing by the size of the first element...yielding one in all cases. It seems that macro is ineffective at reporting the number of elements in an array, if I'm correct.
You're already building your array by fetching numbers from eight edit controls; why not assume that there are eight numbers in the array?
-
cauz I want this function to be portable to other programs that will eventually need a function like this one...
so I want it to guess the maximum and be said the minimum...
VV
-
One solution would be to step through the array until you hit some sort of stop flag, keeping track of the number of elements you've passed. Afterwards, you'd have the size of the array (not counting the stop flag entry). The problem with this approach is determining a sufficient value for the stop flag, as there don't appear to be any limitations on the number the user may input into the text fields.
A solution is to declare the array as type signed double (eight bytes) and with one more slot than the number of edit fields. Then, the array would be filled with the results of the StringToInt() for all edit fields (casting if necessary). The remaining slot would then be set to the maximum or minimum value of a signed double, which would serve as the stop flag value. Really, any value outside the range of a signed int would work, since the point is to specify a stop flag value that the user cannot input into the text fields.
-
As you're using BCB, & if you want deal with arrays of stuff, take a look at the TList class.
-
Looking back at my post, I have no idea why I thought up such a complicated solution. You could store the number of edit fields in the first element of the array and have the function begin its processing by reading back the number.
-
Why not use a vector of ints and call the size() member function to return how many elements it has.
Code:
#include <vector>
...
using namespace std;
vector<int> nums;
nums.push_back(5);
nums.push_back(6);
nums.push_back(7);
nums.size(); // returns 3
-
Thanks all of you, I'll try these solution sunday evening or monday... I'll tell you about it!
thx again
VV
[email protected]
-
TCHAR *pszString;
// Assume you've assigned it to something
for (int i = 0; *(pszString + i * sizeof (TCHAR)) !=' \0'; i++);
// i is now the number of elements
/*
Unicode, here I come! Unicode is a two byte character designed for international compatability. TCHAR represents a character data type, if you define "UNICODE" before you include windows.h, TCHAR will be a two byte character, otherwise a standard char. Sooooo, we've got our pointer, pszString (hungarian notation for "pointer to a string terminated with a zero, String"), and theoretically assigned it to an array. Now, we get a for loop ready, and in the condition block, put into English(kinda):
terminate if the value of (pszString plus (the size of the character times the number of elements so far)) is the null character :þ.
*/