Garbbage in the variable

This is a discussion on Garbbage in the variable within the C++ Programming forums, part of the General Programming Boards category; Hello everyone... I am having a problem with a couple of variables. I declare the variable as char jar[100]=" ", ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    Garbage in the variable

    Hello everyone... I am having a problem with a couple of variables.

    I declare the variable as char jar[100]=" ", tmp[6]=" ", cup[6]=" ";

    In the watch window the value for tmp & cup looks like this: " \000\000\000", but not jar.
    As a consequence the program is not running properly. Any thoughts about this problem? Let me know them...

    I am learning how to program in c++ win32 application on Code::Blocks 10.05 running on win xp... Thanx
    Last edited by Secured; 03-02-2011 at 10:31 PM. Reason: garbbage

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    The problem is probably somewhere else. Since the three variables are the same in every respect except size, jar may have been displayed differently just to look nice, but it should still say exactly what the value is. What about the rest of the code?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    What about the rest of the code?
    Hi Whiteflags,
    Well, the garbbage in cup and tmp are transferred in to the bucket, and that messes up the flow of the code.
    The goal of the code is to sort the largest string two char at the time by extracting and inserting them with the strncpy() like this...

    Code:
    .
    .
    .
    if( strcmp( jar, cup ) > 0 ) {
       strncpy( tmp, &bucket[m], 2 ); /* I am aware tmp is not NULL terminated */
       strncpy( &bucket[m], cup, 2 ); /* The gabbage moves in and destroys the string */
       strncpy( &bucket[n], tmp, 2 ); /* and more garbbage is added */
    }
    .
    .
    .
    Here is the line where the variables are declared locally to the WindProc()...
    char bucket[100]="", track1[]="654321", jar[100]=" ", cup[6]={""}, tmp[6]="";

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    OK, one thing we need to note here.

    char *strncpy(char *dest, const char *src, size_t n);

    The strncpy() function is similar, except that not more than n bytes of
    src are copied. Thus, if(3,n) there is no null byte among the first n bytes
    of src, the result will not be null-terminated.
    http://swoolley.org/man.cgi/3/strcpy

    So for the first strncpy, if there is not a '\0' in bucket[m + 1] or bucket[m + 2] tmp is not terminated.

    For the third strncpy, since tmp is not terminated within the first two characters, neither is the string at bucket + n.

    So bucket is probably terminated somewhere in the middle of bucket[m] and bucket[n].

    I can only say you should use strncpy right, or if you want to attach a (zero terminated!) string to an empty string, you should really be using strncat().

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    So bucket is probably terminated somewhere in the middle of bucket[m] and bucket[n].
    The bucket is holding the content of track1 three times. So there is no terminating '\0' NULL string in between.
    The problem is with cup and tmp having the '\0' in them.

    Quote Originally Posted by whiteflags View Post
    I can only say you should use strncpy right, or if you want to attach a (zero terminated!) string to
    an empty string, you should really be using strncat().
    Cool! strncat(), but I need strncpy() at the moment to place characters between an array's content.

    I think I may have found a solution... declaring cup and tmp as pointers and using malloc(). I no longer see the garbage
    I was seeing in before. Will run it few more times and then on to correct a problem with the sorting loop.

    But I am still at a loss as to what was going on in the codes to cause this terrible side effect. Will keep an eye on google
    for anything similar to my problem...

    Thanx Whiteflags...
    Last edited by Secured; 03-02-2011 at 10:29 PM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Cool! strncat(), but I need strncpy() at the moment to place characters between an array's content.
    So that is what you are doing! To do that you're actually supposed to shift elements over to make room. If you don't then what happens is that the string you want to insert will actually overwrite the part you should have moved before.

    memmove is just the function for this. Make sure that your destination string is big enough to accommodate this move:

    Code:
    #include <string.h>
    #include <stdio.h>
    int main(void)
    {
        char bucket[100] = "hello world";
        size_t x = 6;
        char cup[] = "sick, sad ";
        size_t cuplen = strlen(cup);
        size_t afterlen = strlen(bucket + x) + 1; /* x is where you want the cup string to be. */
        memmove(bucket + x + cuplen, bucket + x, afterlen);
        /* move afterlen bytes of stuff from bucket + x to bucket + x + cuplen */
        strncpy(bucket + x, cup, cuplen);
        /* fill in the hole */ 
        printf("'%s'\n", bucket);
        return 0;
    }
    Just an example, but I think you can apply it in reality. In any case, all of the strings are zero terminated. It's never OK if they aren't really.
    Last edited by whiteflags; 03-02-2011 at 11:07 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Are you using C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    So that is what you are doing!
    I am using two loops to move around the characters. The inner loop does the moving
    with this
    Code:
    if( strcmp( jar, cup ) > 0 ) {
       strncpy( &bucket[m], cup, 2 );
       strncpy( &bucket[n], jar, 2 );
       strcpy( jar, cup );
    }
    Anyway, will dissect your code to figure out how it works, and increase my knowledge, thanx.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by Elysia View Post
    Are you using C or C++?
    Learning C++ win32 GUI with Code::Blocks

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Then why are you using C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by Elysia View Post
    Then why are you using C?
    Um! It looks like I need more help than I thought.
    Please point me in the right direction so that I can learn the
    correct way of C++ programming for a GUI or Windows... Will appreciate it - a lot!

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    He probably means you should use the std::string class to do whatever you are doing. It makes things easier.
    Code:
    std::string bucket("hello world");
    bucket.insert(6, "sick, sad ");
    std::cout <<'\'' << bucket << '\'' << std::endl;
    See? There is a starting tutorial over here: Cprogramming.com - C++ Standard Library - String Class. That said, it wasn't really wrong of you to learn character arrays first. People have different approaches to learning C++. But std::string is basically the preferred type.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Basically there is no point in using char arrays and str(n)cpy et all in C++, because we have better tools that usually work all the time.
    There is nothing wrong in knowing or learning them--especially for educational purposes--but a C++ programmer really should be using the C++ standard library and all its contents, or we'll get what is called a "C+" programmer. A programmer which is basically stuck in C with using some C++ features.

    What is the point of a language if you don't use it to its fullest extent, as fully possible?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Well, using a language "to its fullest extent" will change meaning depending on for what you use it. For example, if you use C++ to learn about character arrays, but don't use character arrays in order to learn them, then you aren't using the language to its fullest extent.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    There is a starting tutorial over here: Cprogramming.com - C++ Standard Library - String Class.
    To delete an entire string, you could use
    str.erase(0, str.length());
    I like this part... wiping a string...
    Code:
    using namespace std::string cup;
    .
    .
    .
    cup.erase( 0, cup.length() );
    .
    .
    .
    Is that correct?
    With std::string may help avoid getting garbage into the cup variable... amazing! Awhile ago I could not grasp
    the concept behind std::xxxxxxx ... I take it that I am ready now... Do you have any links to sample code of
    simple programs designed for windows?

    Thank you Whiteflags, and Elysia... looking forward not being a C+ programmer.... hehehe

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc problem, environment variables Linux C
    By yodakohl in forum C Programming
    Replies: 4
    Last Post: 01-05-2011, 06:04 PM
  2. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 03:32 PM
  3. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM

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