Thread: Combine alike functions

  1. #16
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    To Quzah,

    But the tabs are all inserted by editors, like MSVC or UltraEditor, not by myself. It's from so-called auto-indent function. Do you mean that I need replace them all with spaces by hand?

  2. #17
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    To Narf, sorry, I don't get your point. If memset is not needed, why calloc does the same thing?
    Sorry, I guess I'm not good at expressing myself. You need to put your instance in a predictable state, for example:
    Code:
    struct node {
      int contents;
      struct node *next;
    };
    
    struct node *p = malloc(sizeof *p);
    
    /* Initialize to something predictable */
    p->contents = 0;
    p->next = NULL;
    What you can't do safely is initialize the node with memset():
    Code:
    struct node {
      int contents;
      struct node *next;
    };
    
    struct node *p = malloc(sizeof *p);
    
    /* Unsafe! */
    memset(p, 0, sizeof *p);
    Likewise, you can't use calloc() alone to create and initialize the node because the initialization that calloc() does is identical to memset():
    Code:
    struct node {
      int contents;
      struct node *next;
    };
    
    /* Unsafe! */
    struct node *p = calloc(1, sizeof *p);
    The reason is that memset() and anything that works like memset() copy the bit pattern 00000000--assuming eight bit bytes--to each byte of the destination memory. But, most types don't guarantee that this bit pattern is analogous to a zero value for that type. Which means that 0.0 for double isn't necessarily a bit pattern with all bits zero. That means that the variables aren't in a predictable state because you don't know what to test for without much awkwardness.
    It's from so-called auto-indent function.
    Any good editor will give you the option to replace tabs with spaces automatically. Which means that Notepad isn't a good editor.
    Just because I don't care doesn't mean I don't understand.

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by thinhare
    To Quzah,

    But the tabs are all inserted by editors, like MSVC or UltraEditor, not by myself. It's from so-called auto-indent function. Do you mean that I need replace them all with spaces by hand?
    MSVC has an option to use either spaces or tabs. Select Tools ==>Options, then select "Tabs" tab. There you will see the radio buttons.

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Thanks, Narf & Ancient Dragon.

    Narf, since you just commented out _memset_ and didn't do anything about initialization, can I take that for it's not necesary to do so and can just leave it for further use.

    (BTW, you've made it very very clear.)

  5. #20
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >But, most types don't guarantee that this bit pattern is analogous to a zero value

    I'm not certain that is correct. The C standards for calloc()
    The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.
    It doesn't restrict the kinds of arrays that may be allocated -- arrays of any type, including user-defined types such as structures, can be allocated. Of course, this assumes the standards to not contridict itself someplace else.

  6. #21
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Narf, since you just commented out _memset_ and didn't do anything about initialization, can I take that for it's not necesary to do so and can just leave it for further use.
    I didn't comment it out, I just commented that it wasn't a good idea:
    Code:
    // memset() probably isn't a good idea
    memset(tmp->ptr, 0, sizeof *tmp->ptr);
    If I knew the declaration of DFNCOL then I could have given you a more appropriate initialization, because the only safe way to do it in this case is to initialize each member in turn. Like my first example with the node structure. But I'm sure you can figure that part out on your own.
    Just because I don't care doesn't mean I don't understand.

  7. #22
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    The C standards for calloc()
    And the actual standard says:
    The space is initialized to all bits zero. 253)
    Where the footnote 253 is exceptionally clear:
    253) Note that this need not be the same as the representation of floating-point zero or a null pointer constant.
    Just because I don't care doesn't mean I don't understand.

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > I don't think redefining a variable time and again and again is a good idea.
    It won't - this is C not C++.
    I personally go with restricting variables to the smallest possible scope consistent with the intended usage. With the variable declared inside the loop, there's no temptation (or error) of trying to use that variable outside the loop, where it's likely to have some undefined value.
    In addition, I find it useful when extracting blocks of code from within functions that have grown too big, to place them in a new function.

    > Actually, MSVC initials new created pointer as NULL for me.
    This is only true in debug builds. With release builds (and perhaps other compilers), different answers would result. Initialising things yourself is a good idea (especially initially setting pointers to NULL).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #24
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Salem, the image in your profile is so much fun. I really like it.

    As for the difference between debug and release build of MSVC, I have to say that it is the same for pointer initialization because I did test it in release build. There's something weird with debug build is that I wrote a small program and compiled it as debug build, it worked, however, when I tried to build it to release, it failed, even could not run. Since then, I abandoned the debug build, only worked on the release build.

  10. #25
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Narf
    And the actual standard says:

    Where the footnote 253 is exceptionally clear:
    fair enough

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > As for the difference between debug and release build of MSVC....
    Then you have bugs in your code which need to be fixed. Apart from being easier to debug (and being larger and slower), there should be no functional difference between the debug and release builds of your code. If there is, then you've got problems.

    > I have to say that it is the same for pointer initialization
    I think you'll find (if you study the generated code) that debug goes out of it's way to specifically initialise things even if you don't, whereas in release the value you find is down to pure dumb luck. Sure your stack may start all nice and clean, but after some function calls what you find in your uninitialised local variables is the junk just left their from earlier activity in the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM