Thread: Array Size confusion

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    Array Size confusion

    Hey everyone, I have a problem that is really puzzling...

    I have the following code:
    Code:
    	// Determine size of transfer
    	packageSize = strlen( TxBuffer );
    	cout << "packageSize "<< packageSize << endl;
    	char* RxBuffer = new char [ packageSize ];
    	cout << "RxBuffer " << strlen ( RxBuffer ) << endl;
    	// Zero out the Rx buffer
    	memset( RxBuffer, 0xa5, packageSize );
    	cout << "packageSize "<< packageSize << endl;
    	cout << "RxBuffer " << strlen(RxBuffer) << endl;
    When executed with some TxBuffer length 16 i get this:
    packageSize 16
    RxBuffer 32
    packageSize 16
    RxBuffer 32

    When I change my code to

    Code:
    	// Determine size of transfer
    	packageSize = strlen( TxBuffer );
    	cout << "packageSize "<< packageSize << endl;
    	char* RxBuffer = new char [ packageSize/2 ];
    	cout << "RxBuffer " << strlen ( RxBuffer ) << endl;
    	// Zero out the Rx buffer
    	memset( RxBuffer, 0xa5, packageSize/2 );
    	cout << "packageSize "<< packageSize << endl;
    	cout << "RxBuffer " << strlen(RxBuffer) << endl;

    The output is like this:

    packageSize 16
    RxBuffer 24
    packageSize 16
    RxBuffer 24

    I'm not sure why the sizes are even different, and even more strangly, why is dividing a 32 size by 2 giving me 24?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The number you get from strlen bears no relationship to the amount of memory you set aside. The number you get from strlen is "how many bytes is it from here to the next \0 character?" and if that happens to be 24 bytes away, then there you go.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Ok that makes sense. Then how do I set it so the RxBuffer will have the same size as the TxBuffer that is already filled with data of unknown size? Is there a better method to use then strlen?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are already setting the size of the buffer when you create it.

    Once you create it, there is no way to retrieve the size of the buffer, so don't try. It's up to you to remember what it was.

    Another thing to remember: if you are using C-string utilities, many of them require (just as strlen does) the last character of the data to be \0. Presumably the way you are getting TxBuffer makes sure that \0 is there. strlen requires the \0 character, but it does not count it -- so when you create the new buffer you must add one extra character to account for it, if you intend for Tx and Rx to "be the same size".

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In order to 0 out an array you need to set it's values to 0, not 0xa5.

    Also, reading passed the end of your allocated space can in rare cases cause your program to crash.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You might find std::vector useful here. It allows you to find out its size and dynamically reallocate it easily. It also takes care of freeing memory.
    Memory is also contiguous, so it works exactly like a buffer.
    To get a pointer to the buffer, you might simply do &vec[0].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confusion in array concept...
    By xterminator in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 11:51 AM
  2. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  3. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  4. array and char confusion
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2004, 05:59 AM
  5. Confusion inputing custom size variables?
    By thenrkst in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2003, 08:02 AM