Thread: 1st post by 1st time C coder

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's not that hard to spot, in some cases it can improve readability, consider:

    Say I wanted to fill each dimension of 'x' with '10'.
    Code:
    #define DIM 5
    /* ... */
    
    int x[DIM][DIM][DIM][DIM];
    int a, b, c, d;
    
    for(a = 0; a < DIM; a++)
        for(b = 0; b < DIM; b++)
            for(c = 0; c < DIM; c++)
                for(d = 0; d < DIM; d++)
                    x[a][b][c][d] = 10;
    
    /* OR */
    
    for(a = 0; a < DIM; a++)
    {
        for(b = 0; b < DIM; b++)
        {
            for(c = 0; c < DIM; c++)
            {
                for(d = 0; d < DIM; d++)
                {
                    x[a][b][c][d] = 10;
                }
            }
        }
    }
    It's just an example, there are easier ways to fill that array

    Anyway it proves to be more compact and I'd say is readable at a first glance, not to mention the potential bug of leaving off a closing brace (and accidentally closing it later).

    My point is, the author has done nothing wrong - it really comes into coding style, after all that's what tab-indenting is really supposed to help solve. Plus you should be able to recognize that if there is no braces the first statement after will be run, (keep going until you find a ';').
    Last edited by zacs7; 09-05-2007 at 04:41 AM.

  2. #17
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Plus you should be able to recognize that if there is no braces the first statement after will be run, (keep going until you find a ';').
    I can, I'm saying it's a bad thing to teach a beginner. Make it easy for beginners, use braces for all if and for loops, put parantheses around everything. When they get more advanced, they can play it fast and loose

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #18
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I agree if he doesn't explain it in depth then it's bad, making assumptions are generally bad

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by iMalc View Post
    First bug is using strcat on an uninitialised variable. Use strcpy(Blank, "0"); instead.
    Actually, Bytes is a global variable. Global arrays are automatically initialized to all zeros. This means that the first time this function is called, it will work as expected; but subsequent calls to it would cause a buffer overrun.

    The simplest thing to do, of course, is to replace the first strcat() with strcpy().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6
    Hey Guys!

    Remember me? I'm the guy that originally posted for help. Well I figured it out on my own. The solution was so simple that I have to wonder about those whom replied.

    In case you have forgot, the problem was that after the first return the string would begin to compound in length by the strlen.

    OK, granted my defaced code wasn't to cool, but you all clearly picked up on the use of strcat() so you knew that I was building an array of chars for the purpose of returning a string.

    That stated, the two that suggested replacing strcat() with strcpy() weren't too sharp because the variable would have only contain the last copied content.

    All that it took to solve my problem was:

    Code:
    BSTR GetIOReadings()
    {
    if(strlen(Bytes)>2)
      {
       SysFreeString((OLECHAR*)Bytes);
      }
    strcpy(Bytes,"0");
    Servo

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's no need to insult anyone. They were trying to help you, and they certainly weren't obligated to do so. There's a chance that the people in this thread won't feel like helping you in the future.

    That stated, the two that suggested replacing strcat() with strcpy() weren't too sharp because the variable would have only contain the last copied content.
    Besides the tone, didn't you do just that in your final code? I see a strcpy(), not a strcat().

    Code:
    BSTR GetIOReadings()
    {
    if(strlen(Bytes)>2)
      {
       SysFreeString((OLECHAR*)Bytes);
      }
    strcpy(Bytes,"0");
    So, if the length of Bytes is more than two, you free the data allocated by Bytes, and then proceed to assign a value to the memory? You're overwriting memory that has been freed. This is an exceedingly bad idea. You'll likely get segmentation faults and random buffer overruns and other annoying bugs.

    That's assuming that Bytes was dynamically allocated in the first place. If it's declared like this, then you have even bigger problems.
    Code:
    char Bytes[22];
    Arrays aren't meant to be freed by the program.

    Your indentation's not the best, either. It could use some working on.

    Plus you're missing a closing curly brace, }.
    Last edited by dwks; 09-07-2007 at 02:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6
    dwks,

    There's more to the code then what I illustrated. There are seven occurances of strcat() in the code below where I copied. As I stated, replacing strcat() with strcpy() would not work due to the previosly mentioned reason.

    Believe me, the driver, dll and debug program all work, but iIf you need additional proof then I will attach a copy of the compiled driver, dll, and the debug.exe program.

    Servo

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, if you think it works, than that's all that matters.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  2. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  3. Good news, and bad news... (meaningless post, do not wast your time reading it)
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-05-2002, 04:27 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM