Thread: sizeof() EZ question

  1. #1
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22

    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]

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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.

  3. #3
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22
    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

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.

  5. #5
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22
    ohh.. this is fun

    hehe thanks a lot!
    I'll try this!

    see ya l8r!

    VV

  6. #6
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22

    Unhappy 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
    Last edited by V1P3R V3N0M; 01-10-2003 at 02:19 AM.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: doh

    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.

  8. #8
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  9. #9
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22
    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

  10. #10
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  11. #11
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    As you're using BCB, & if you want deal with arrays of stuff, take a look at the TList class.

  12. #12
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    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.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  13. #13
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    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

  14. #14
    Registered User V1P3R V3N0M's Avatar
    Join Date
    Jan 2003
    Posts
    22
    Thanks all of you, I'll try these solution sunday evening or monday... I'll tell you about it!

    thx again

    VV
    [email protected]

  15. #15
    arctidog
    Guest
    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 :þ.

    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. quick question about sizeof operator
    By *ClownPimp* in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2002, 07:35 AM