Thread: I have some newbie questions

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Question I have some newbie questions

    Hello everyone!

    I am an experienced Pascal programmer but I'm new to C++. I'm trying to avoid asking stupid questions but every book, tutorial, FAQ, etc I have found always assumes that I know this stuff. Please help me if you can!

    1. What does the underscore (_) and double underscore (__) in front of a function and/or variable name mean?

    2. What is the difference (if any) between these two lines of code? I'm specifically talking about the placement of the *

    int Read(unsigned char *buff);
    int Read(unsigned char* buff);

    3. In the following function if the IF condition evaluates to true will that cause a memory leak with temp?

    int FileChunk::Read(unsigned char *buff,int maxNum)
    {
    DWORD numRead = 0;
    //allocate a buffer big enough to hold the users data plus the header
    unsigned char *temp = new unsigned char[maxNum + sizeof(cid)];

    //read in the specified number of bytes
    if (numRead = GenericFile::Read(temp,maxNum + sizeof(cid)))
    {
    ... //misc code

    //return the number of bytes read
    return numRead;
    }

    delete[] temp;

    //return false
    return 0;
    }



    I would appreciate any help you can provide. Thanks!

    Myra Mains

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    2. No difference at all. It is all a matter of style preference.

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: I have some newbie questions

    Welcome aboard, Myra...

    >>1. What does the underscore (_) and double underscore (__) in front of a function and/or variable name mean?

    It doesn't actually mean anything. Just a naming convention.

    >>2. What is the difference (if any) between these two lines of code? I'm specifically talking about the placement of the *

    >>int Read(unsigned char *buff);
    >>int Read(unsigned char* buff);

    They mean the same thing. Both mean that the function Read takes a param that is an unsigned char pointer called buff. The placement of the * is (more or less) a matter of your own style.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: I have some newbie questions

    Originally posted by Myra Mains

    1. What does the underscore (_) and double underscore (__) in front of a function and/or variable name mean?
    Can't say, never seen that.
    2. What is the difference (if any) between these two lines of code? I'm specifically talking about the placement of the *

    int Read(unsigned char *buff);
    int Read(unsigned char* buff);
    No difference, matter of preference.
    3. In the following function if the IF condition evaluates to true will that cause a memory leak with temp?

    int FileChunk::Read(unsigned char *buff,int maxNum)
    {
    DWORD numRead = 0;
    //allocate a buffer big enough to hold the users data plus the header
    unsigned char *temp = new unsigned char[maxNum + sizeof(cid)];

    //read in the specified number of bytes
    if (numRead = GenericFile::Read(temp,maxNum + sizeof(cid)))
    {
    ... //misc code

    //return the number of bytes read
    return numRead;
    }

    delete[] temp;

    //return false
    return 0;
    }
    Not for the reason you state, but you need another closing brace after delete[] temp.
    Truth is a malleable commodity - Dick Cheney

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. All symbols with leading underscores are reserved names. You should never create a symbol in your own code which begins with an underscore.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Ah! You do learn something new everyday. I never actually use an underscore in my own function/variable naming but I never knew that it is for reserved names.

    Sorry if my reply mislead anyone. Thanks for the save, Salem!

  7. #7
    Unregistered
    Guest
    Actually I think the double underscores in most symbols (a.k.a __A_SYMBOL__) is used so it would be EXTREMELY unlikely that you would have a symbol with the same name, causing a compiler error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 09:30 PM
  3. Im a newbie to C and i have some questions
    By pave1104 in forum C Programming
    Replies: 5
    Last Post: 07-05-2006, 09:48 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 02:43 PM