Implementing space allocated for a character array

This is a discussion on Implementing space allocated for a character array within the C++ Programming forums, part of the General Programming Boards category; I hope the title did a decent job of summarizing my issue. I am trying to extend a two dimensional ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    50

    Implementing space allocated for a character array

    I hope the title did a decent job of summarizing my issue. I am trying to extend a two dimensional algorithm to a 3 dimensional solution, and am having some trouble storing my slice files. I have an image object I, which has a member pt that is a pointer to an array of unsigned chars. My code looks like this:

    Code:
    istream& operator>>(istream& is, Image& I)
    {
            if(I.pt) {
                    cout << "test\n";
                    delete [] I.pt;
                    I.width = 0;
                    I.height = 0;
            }
    
            .
            .
            .
    
            // Now we are ready to read the data
            I.pt = new unsigned char [I.getDataSize()];
            I.pt[1000] = (unsigned char)1;
            cout << I.pt[1000] << endl;
            cout << sizeof(I.pt) / sizeof(unsigned char) << endl;
            cout << I.getDataSize() << endl;
            cout << sizeof(I.pt) << endl;
            cout << sizeof(unsigned char) << endl;
    and, my output is:
    Code:
    8
    94633984
    8
    1
    This is some pointless code that I am displaying hoping to resolve a larger issue. I am expecting to see:
    Code:
    1
    94633984
    94633984
    94633984
    1
    What am I not understanding about the size of space pointed to by I.pt after the new statement?
    Last edited by nicoeschpiko; 04-29-2011 at 03:53 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    cout << sizeof(I.pt) / sizeof(unsigned char) << endl;
    Prints the size of a pointer, divided by the size of an unsigned char. Since sizeof(unsigned char) = 1, the answer depends on the pointer size on your system. Commonly 4 or 8. In your case, it seems to be 8, so I'd be guessing you're on a 64-bit system.

    cout << I.getDataSize() << endl;
    Probably gonna get the size of the array pointed to by pt. Your output seems to corroborate this.

    cout << sizeof(I.pt) << endl;
    Prints the size of a pointer, thus 8.

    cout << sizeof(unsigned char) << endl;
    Prints the size of an unsigned char, thus 1.

    So, the output you are getting is correct.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    The first cout statement is what is confusing me the most (i.e. why I'm seeing a blank line rather than '1'). Could you address that output, or lack thereof? Thanks

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You are sending an unsigned char to cout, hence it is interpreted as a printable character and will be printed as such.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    50
    Thanks a bunch! My pointer had the correct values stored all along.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting the size of allocated space
    By cs32 in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 07:18 AM
  2. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  3. Converting space character in string to integer
    By boostage in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2006, 08:40 PM
  4. returning allocated space from function
    By BrownB in forum C Programming
    Replies: 4
    Last Post: 07-13-2004, 12:06 PM
  5. How do I tell what character is in a space?
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21