Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Useful Function or Junk?

Useful Function or Junk?

Scheduled Pinned Locked Moved The Lounge
questionsysadmindata-structures
22 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J John Aldrich

    this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

    /////////////////////////////////////////////
    //
    // Liquid Plumber: Buffer Cleanup Function
    //
    // function takes 1 parameter which is the
    // variable name of the char array to act on.
    // this is just a simple wrapper around the
    // memset function to allow "hands-free" use
    //
    /////////////////////////////////////////////

    void LiquidPlumber( char * szBufferName )
    {
    memset(szBufferName, 0, sizeof(szBufferName));
    }

    My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


    It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

    J Offline
    J Offline
    James Brown
    wrote on last edited by
    #3

    Errmm...it's junk, sorry! You have problem with the way you calculate the size of the buffer: sizeof(szBuffer) will reduce to sizeof(char *) which is the size of a pointer - NOT the size of the buffer that it points to. So, you have to introduce a new parameter to specify the size at runtime. You might as well use ZeroMemory defined in windows.h which does this exact thing: VOID ZeroMemory(VOID *buf, SIZE_T length); And it's clearer what it does too :-) Regards, James
    http://www.catch22.uk.net

    1 Reply Last reply
    0
    • R Ryan Johnston 0

      Personally I think a function this simple is better suited to be a macro (or an inline function). As it stands right now, you have introduced the added overhead of an additional function call, without any additional benifit. Nothing wrong with the idea in my oppinion though.

      J Offline
      J Offline
      John Aldrich
      wrote on last edited by
      #4

      the reason why I have the function cast that way is because without the char * szBufferName convention, you woudl have to defin ethe size of the buffer within memset itself, when it's far simpler to just use the pointer and still get the same effect. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

      R 1 Reply Last reply
      0
      • R Ryan Johnston 0

        Personally I think a function this simple is better suited to be a macro (or an inline function). As it stands right now, you have introduced the added overhead of an additional function call, without any additional benifit. Nothing wrong with the idea in my oppinion though.

        R Offline
        R Offline
        Ryan Johnston 0
        wrote on last edited by
        #5

        After reading a following post I went back and saw what you did. I think what you wanted was strlen(), but that only works for null terminated strings (Rarely what you are dealing with in the situation you described).

        J 1 Reply Last reply
        0
        • J John Aldrich

          the reason why I have the function cast that way is because without the char * szBufferName convention, you woudl have to defin ethe size of the buffer within memset itself, when it's far simpler to just use the pointer and still get the same effect. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

          R Offline
          R Offline
          Ryan Johnston 0
          wrote on last edited by
          #6

          What effect are you talking about? All your function does is sets the first 4 (in the case of 32bit pointers) bytes of your buffer to 0. sizeof(szBufferName) does not give you the size of the string pointed to by szBufferName, it gives you the size in bytes of the pointer itself.

          1 Reply Last reply
          0
          • R Ryan Johnston 0

            After reading a following post I went back and saw what you did. I think what you wanted was strlen(), but that only works for null terminated strings (Rarely what you are dealing with in the situation you described).

            J Offline
            J Offline
            John Aldrich
            wrote on last edited by
            #7

            I used sizeof because theres no guarentee that your -going- to get a null terminated string, so i figured it made sense, at least on my level of understanding. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

            R 1 Reply Last reply
            0
            • J John Aldrich

              I used sizeof because theres no guarentee that your -going- to get a null terminated string, so i figured it made sense, at least on my level of understanding. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

              R Offline
              R Offline
              Ryan Johnston 0
              wrote on last edited by
              #8

              Well sizeof does not give you what you want.

              1 Reply Last reply
              0
              • J John Aldrich

                this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                /////////////////////////////////////////////
                //
                // Liquid Plumber: Buffer Cleanup Function
                //
                // function takes 1 parameter which is the
                // variable name of the char array to act on.
                // this is just a simple wrapper around the
                // memset function to allow "hands-free" use
                //
                /////////////////////////////////////////////

                void LiquidPlumber( char * szBufferName )
                {
                memset(szBufferName, 0, sizeof(szBufferName));
                }

                My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                T Offline
                T Offline
                Todd Smith
                wrote on last edited by
                #9

                This only works if the buffer size is know at compile time. Works:

                char buf[256];
                memset(buf, 0, sizeof(buf));

                This does NOT work:

                Zero(char* buf)
                {
                memset(buf, 0, sizeof(buf));
                }

                main()
                {
                char buf[256];
                Zero(buf);
                }

                Todd Smith

                J 2 Replies Last reply
                0
                • J John Aldrich

                  this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                  /////////////////////////////////////////////
                  //
                  // Liquid Plumber: Buffer Cleanup Function
                  //
                  // function takes 1 parameter which is the
                  // variable name of the char array to act on.
                  // this is just a simple wrapper around the
                  // memset function to allow "hands-free" use
                  //
                  /////////////////////////////////////////////

                  void LiquidPlumber( char * szBufferName )
                  {
                  memset(szBufferName, 0, sizeof(szBufferName));
                  }

                  My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                  It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                  D Offline
                  D Offline
                  Daniel Turini
                  wrote on last edited by
                  #10

                  Is it a function for cleaning up 4-byte strings (Visual C++ assumed) ? Use ZeroMemory Win32 macro instead. Concussus surgo. When struck I rise.

                  1 Reply Last reply
                  0
                  • J John Aldrich

                    this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                    /////////////////////////////////////////////
                    //
                    // Liquid Plumber: Buffer Cleanup Function
                    //
                    // function takes 1 parameter which is the
                    // variable name of the char array to act on.
                    // this is just a simple wrapper around the
                    // memset function to allow "hands-free" use
                    //
                    /////////////////////////////////////////////

                    void LiquidPlumber( char * szBufferName )
                    {
                    memset(szBufferName, 0, sizeof(szBufferName));
                    }

                    My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                    It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                    D Offline
                    D Offline
                    Daniel Turini
                    wrote on last edited by
                    #11

                    Oh, almost forgot: you can do this (not my invention, this is a classic one): template class CWin32Struct : public T { public: CWin32Struct() { ZeroMemory(this, sizeof(T)); }; }; template class CWin32StructCB : public T { public: CWin32StructCB() { ZeroMemory(this, sizeof(T)); cb = sizeof(T); }; }; then use: CWin32Struct pi; or CWin32StructCB pmc;

                    1 Reply Last reply
                    0
                    • T Todd Smith

                      This only works if the buffer size is know at compile time. Works:

                      char buf[256];
                      memset(buf, 0, sizeof(buf));

                      This does NOT work:

                      Zero(char* buf)
                      {
                      memset(buf, 0, sizeof(buf));
                      }

                      main()
                      {
                      char buf[256];
                      Zero(buf);
                      }

                      Todd Smith

                      J Offline
                      J Offline
                      John Aldrich
                      wrote on last edited by
                      #12

                      I'm sorry, but that's simply not correct. I've tested it both ways and it works if the array is defined as szSomeArray[17] = "testtesttesttest" or szSomeArray[] ="testtesttesttest". The effect is the same. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                      D T 2 Replies Last reply
                      0
                      • J John Aldrich

                        I'm sorry, but that's simply not correct. I've tested it both ways and it works if the array is defined as szSomeArray[17] = "testtesttesttest" or szSomeArray[] ="testtesttesttest". The effect is the same. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                        D Offline
                        D Offline
                        Daniel Turini
                        wrote on last edited by
                        #13

                        That's because you are testing it the wrong way: printf(szSomeArray). Try printf(szSomeArray + 6) instead Concussus surgo. When struck I rise.

                        1 Reply Last reply
                        0
                        • J John Aldrich

                          this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                          /////////////////////////////////////////////
                          //
                          // Liquid Plumber: Buffer Cleanup Function
                          //
                          // function takes 1 parameter which is the
                          // variable name of the char array to act on.
                          // this is just a simple wrapper around the
                          // memset function to allow "hands-free" use
                          //
                          /////////////////////////////////////////////

                          void LiquidPlumber( char * szBufferName )
                          {
                          memset(szBufferName, 0, sizeof(szBufferName));
                          }

                          My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                          It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #14

                          Just do it when you declare your array:

                          TCHAR szSomeString[14] = {0};

                          That zeros out the entire array. (Actually, it sets the first element in the array to 0 (the value specified) and then C initializes the remainder of the array to 0 (regardless of the value specified).) --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm

                          1 Reply Last reply
                          0
                          • J John Aldrich

                            this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                            /////////////////////////////////////////////
                            //
                            // Liquid Plumber: Buffer Cleanup Function
                            //
                            // function takes 1 parameter which is the
                            // variable name of the char array to act on.
                            // this is just a simple wrapper around the
                            // memset function to allow "hands-free" use
                            //
                            /////////////////////////////////////////////

                            void LiquidPlumber( char * szBufferName )
                            {
                            memset(szBufferName, 0, sizeof(szBufferName));
                            }

                            My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                            It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #15

                            Personally, I never use char *, because I program C++ rather than C, and I never need the performance that would justify the clunkiness and error prone nature of char *. Apart from that, I agree with the person who said that if I needed to use something so ugly as a char*, I'd be too worried about efficiency to be adding the cost of a function call ( although it's concievable that the compiler would inline it ), and I would just call memset rather than add the cost of another function. It's one line vs. one line. Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

                            1 Reply Last reply
                            0
                            • J John Aldrich

                              I'm sorry, but that's simply not correct. I've tested it both ways and it works if the array is defined as szSomeArray[17] = "testtesttesttest" or szSomeArray[] ="testtesttesttest". The effect is the same. It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                              T Offline
                              T Offline
                              Todd Smith
                              wrote on last edited by
                              #16

                              I just tested it and it failed. Without the right test you can't get the correct answer :)

                              void LiquidPlumber(char * szBufferName)
                              {
                              int size = sizeof(szBufferName);
                              memset(szBufferName, 0, size);
                              }

                              main()
                              {
                              char buf[256];
                              LiquidPlumber(buf);

                              char\* array = new char\[256\];
                              LiquidPlumber(array);
                              

                              }

                              Step through and see what size is. Todd Smith

                              1 Reply Last reply
                              0
                              • T Todd Smith

                                This only works if the buffer size is know at compile time. Works:

                                char buf[256];
                                memset(buf, 0, sizeof(buf));

                                This does NOT work:

                                Zero(char* buf)
                                {
                                memset(buf, 0, sizeof(buf));
                                }

                                main()
                                {
                                char buf[256];
                                Zero(buf);
                                }

                                Todd Smith

                                J Offline
                                J Offline
                                John Aldrich
                                wrote on last edited by
                                #17

                                Try this.

                                /////////////////////////////////////////////
                                //
                                // Liquid Plumber: Buffer Cleanup Function
                                //
                                // function takes 1 parameter which is the
                                // variable name of the char array to act on
                                // this is just a simple wrapper around the
                                // memset function to allow "hands-free" use
                                //
                                /////////////////////////////////////////////

                                void LiquidPlumber(char * szBufferName)
                                {
                                int size = sizeof(szBufferName);
                                memset(szBufferName, 0, size);
                                }

                                int APIENTRY WinMain(HINSTANCE hInstance,
                                HINSTANCE hPrevInstance,
                                LPSTR lpCmdLine,
                                int nCmdShow)
                                {
                                char strDataToEncode[] = "testtesttesttest";

                                MessageBox(NULL, strDataToEncode, "Clogged Pipe", MB\_OK);
                                
                                LiquidPlumber(strDataToEncode);
                                
                                MessageBox(NULL, strDataToEncode, "Cleaned Pipe", MB\_OK);
                                
                                return 0;
                                

                                }

                                It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                T S 2 Replies Last reply
                                0
                                • J John Aldrich

                                  this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                                  /////////////////////////////////////////////
                                  //
                                  // Liquid Plumber: Buffer Cleanup Function
                                  //
                                  // function takes 1 parameter which is the
                                  // variable name of the char array to act on.
                                  // this is just a simple wrapper around the
                                  // memset function to allow "hands-free" use
                                  //
                                  /////////////////////////////////////////////

                                  void LiquidPlumber( char * szBufferName )
                                  {
                                  memset(szBufferName, 0, sizeof(szBufferName));
                                  }

                                  My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                                  It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                  J Offline
                                  J Offline
                                  Jim A Johnson
                                  wrote on last edited by
                                  #18

                                  Well, except for the fact that it doesn't work, has a non-descriptive name, has no error checking facilities.. no, it's not of much value. You don't work as a professional programmer I hope.

                                  1 Reply Last reply
                                  0
                                  • J John Aldrich

                                    Try this.

                                    /////////////////////////////////////////////
                                    //
                                    // Liquid Plumber: Buffer Cleanup Function
                                    //
                                    // function takes 1 parameter which is the
                                    // variable name of the char array to act on
                                    // this is just a simple wrapper around the
                                    // memset function to allow "hands-free" use
                                    //
                                    /////////////////////////////////////////////

                                    void LiquidPlumber(char * szBufferName)
                                    {
                                    int size = sizeof(szBufferName);
                                    memset(szBufferName, 0, size);
                                    }

                                    int APIENTRY WinMain(HINSTANCE hInstance,
                                    HINSTANCE hPrevInstance,
                                    LPSTR lpCmdLine,
                                    int nCmdShow)
                                    {
                                    char strDataToEncode[] = "testtesttesttest";

                                    MessageBox(NULL, strDataToEncode, "Clogged Pipe", MB\_OK);
                                    
                                    LiquidPlumber(strDataToEncode);
                                    
                                    MessageBox(NULL, strDataToEncode, "Cleaned Pipe", MB\_OK);
                                    
                                    return 0;
                                    

                                    }

                                    It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                    T Offline
                                    T Offline
                                    Todd Smith
                                    wrote on last edited by
                                    #19

                                    Failed. Todd Smith

                                    1 Reply Last reply
                                    0
                                    • J John Aldrich

                                      Try this.

                                      /////////////////////////////////////////////
                                      //
                                      // Liquid Plumber: Buffer Cleanup Function
                                      //
                                      // function takes 1 parameter which is the
                                      // variable name of the char array to act on
                                      // this is just a simple wrapper around the
                                      // memset function to allow "hands-free" use
                                      //
                                      /////////////////////////////////////////////

                                      void LiquidPlumber(char * szBufferName)
                                      {
                                      int size = sizeof(szBufferName);
                                      memset(szBufferName, 0, size);
                                      }

                                      int APIENTRY WinMain(HINSTANCE hInstance,
                                      HINSTANCE hPrevInstance,
                                      LPSTR lpCmdLine,
                                      int nCmdShow)
                                      {
                                      char strDataToEncode[] = "testtesttesttest";

                                      MessageBox(NULL, strDataToEncode, "Clogged Pipe", MB\_OK);
                                      
                                      LiquidPlumber(strDataToEncode);
                                      
                                      MessageBox(NULL, strDataToEncode, "Cleaned Pipe", MB\_OK);
                                      
                                      return 0;
                                      

                                      }

                                      It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                      S Offline
                                      S Offline
                                      Steve T
                                      wrote on last edited by
                                      #20

                                      Hi, You will very easiliy see where this goes wrong if you try to initialize your array to something other than zero, for example change it to 'A' and the result becomes "CLEANED PIPE" "AAAAtesttesttest" Steve T.

                                      1 Reply Last reply
                                      0
                                      • J John Aldrich

                                        this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                                        /////////////////////////////////////////////
                                        //
                                        // Liquid Plumber: Buffer Cleanup Function
                                        //
                                        // function takes 1 parameter which is the
                                        // variable name of the char array to act on.
                                        // this is just a simple wrapper around the
                                        // memset function to allow "hands-free" use
                                        //
                                        /////////////////////////////////////////////

                                        void LiquidPlumber( char * szBufferName )
                                        {
                                        memset(szBufferName, 0, sizeof(szBufferName));
                                        }

                                        My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                                        It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                        V Offline
                                        V Offline
                                        Vagif Abilov
                                        wrote on last edited by
                                        #21

                                        It's very convenient to have a function that takes less arguments than memset. But since it does not do the job (note that sizeof(char*) is always 4 under Win32, no matter what is the length of the actual string), then you could skip the first argument as well: void OptimizedLiquidPlumber(void) { // Nothing to do, still almost as good as the original LiquidPlumber } ;P Vagif Abilov MCP (Visual C++) Oslo, Norway

                                        1 Reply Last reply
                                        0
                                        • J John Aldrich

                                          this is a little something I wrote because I had idle hands a little bit ago and needed something to do. Excuse the, how should I say, rather droll nature of the function name, but that happened to be the commercial airing at the time I was working on another project, hence the naming. I figured this might have some use so I went ahead and posted it, however simplistic it may seem.

                                          /////////////////////////////////////////////
                                          //
                                          // Liquid Plumber: Buffer Cleanup Function
                                          //
                                          // function takes 1 parameter which is the
                                          // variable name of the char array to act on.
                                          // this is just a simple wrapper around the
                                          // memset function to allow "hands-free" use
                                          //
                                          /////////////////////////////////////////////

                                          void LiquidPlumber( char * szBufferName )
                                          {
                                          memset(szBufferName, 0, sizeof(szBufferName));
                                          }

                                          My question is this, is this a useful function by way of that facts that it brings the amount of work down to just specifying the variable name to act on, or is it simpler and more "syntax" correct to simply call memset? The function handles calculating the size of the buffer internally and also specifies a zero value as the character to fill the buffer with. IMHO, I think it's easier to use this function as it saves repeated calls to memset in programs where multiple buffers are used, and in most cases, filling the buffer with a zero value, especially if it's in a program like a server app thats using a buffer as temp storage for commands recieved from a tcp/ip port. It would be easier to call this than to have to manually code the values. Comments / Suggestions Appreciated.


                                          It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now... [Shog9 on Kid Programmers]

                                          T Offline
                                          T Offline
                                          Todd Hoop
                                          wrote on last edited by
                                          #22

                                          Use the following and be done with it: *szBufferName = '\0'; I see code all the time when folks zero out strings for no reason. They waste processor time and their own. I had a friend who told me once that he worked with a contractor that zero'ed out every string by using a for-loop. Clever...

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups