Thread: Trouble with strings!

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

    Trouble with strings!

    does anybody know why strings change during the program execution?
    example. :
    cprintf("%s","This is the string i want"); <- program calls this function several times during programs execution, and the result is diferent to that i've written.

    i mean :
    This is the string i want
    This is the string i want
    "lkj24?#@\]20x223" or part of other strings that i use.


    I can mention that i use a lot of strings ( type [char *]).

    i will not paste the code because it's to big.
    if smb have an ideea pm, ill send code..

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post the smallest part of your code that demonstrates the problem...
    In particular we will need to see how you are creating/declaring your strings.

    Most likely the strings are changing because you are using them uninitilized or without memory allocations.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you've basically screwed up with your memory allocation, by doing things like this.

    char *ptr;
    gets(ptr);

    Or maybe even things like
    char *ptr = "hello";
    strcat(ptr,"world);

    I'm guessing you're a TurboC user.
    Only someone with modifyable string constants would get into such a horrible mess to begin with.

    The problem with TurboCrap is that it lets you walk way too far into the minefield before the first one goes off. By then, you have far too many obstacles in your way to make it back.

    See some of my more recent posts, to find out some recommendations for a real compiler for your real OS (you're using XP or better no doubt).
    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.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It looks like you are having memory corruption. Usually outdated compiler will allow to modify string literal.
    Use your debugger/memory checker and start working. Writing program is just part of development!
    Argh salem beats me up! I'm having slow connection here.:-|

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by CommonTater View Post
    Post the smallest part of your code that demonstrates the problem...
    In particular we will need to see how you are creating/declaring your strings.

    Most likely the strings are changing because you are using them uninitilized or without memory allocations.
    well i'm just using them like this :

    cputs(": Den. : : Nr. : :Nr. Mem. : :Conducatorul: :Orientare:");

    or

    cprintf("%s",":Partidului: :Membri: :Parlament: : Partidului :");

    do i need to use all strings like this :
    s1=(char *)malloc(40);
    strcpy(s1,"Vizualizarea Partidelor si Membrilor");

    (even if there's no need to modify, cause these strings are somethings like header of a table that i'm drawing/writing on screen )?

    and what about
    cprintf("%c",185) ?
    if it also write's parts of strings, ho do i solve, this problem?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Stop looking at where the problem shows up, and methodically look at ALL your other usage of char pointers.

    Somewhere, you have
    - not initialised a pointer
    - not allocated enough memory
    - tried to use it after freeing it
    - tried to free something twice
    - tried to free something which wasn't allocated.

    The "not enough memory" also applies to all your char arrays as well, especially static/global arrays.
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by picaso View Post
    well i'm just using them like this :

    cputs(": Den. : : Nr. : :Nr. Mem. : :Conducatorul: :Orientare:");

    or

    cprintf("%s",":Partidului: :Membri: :Parlament: : Partidului :");


    do i need to use all strings like this :
    s1=(char *)malloc(40);
    strcpy(s1,"Vizualizarea Partidelor si Membrilor");

    (even if there's no need to modify, cause these strings are somethings like header of a table that i'm drawing/writing on screen )?

    and what about
    cprintf("%c",185) ?
    if it also write's parts of strings, ho do i solve, this problem?
    Most likely you have 1 or 2 strings someplace where you've failed to initialize memory, leaving the pointers aimed at undefined locations in which case you get undefined behavior. Most often it's not the literal string you print that's causing the problem. Something is overwriting literal strings... you need to find out what that is.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by Salem View Post
    Stop looking at where the problem shows up, and methodically look at ALL your other usage of char pointers.

    Somewhere, you have
    - not initialised a pointer
    - not allocated enough memory
    - tried to use it after freeing it
    - tried to free something twice
    - tried to free something which wasn't allocated.

    The "not enough memory" also applies to all your char arrays as well, especially static/global arrays.
    i'm not looking where problem show's up, and paste it here.
    i asked about char (single caracter).

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In your loop check carefully that you don't have buffer overrun or any...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    None of the random lines you've posted so far would ever be a problem. Go on, try JUST those lines in a loop of some sort and see whether there is any corruption.

    If you focus on the effect, you'll never see the cause.

    About the only person who has any residual care about debugging masses of turboc would be Adak.
    Pretty much everyone else has neither the skill, time nor inclination to go wading in a fetid swamp of turboc code.
    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. Sorting Array of Strings
    By nair in forum C Programming
    Replies: 6
    Last Post: 10-09-2010, 11:10 AM
  2. swapping strings (which are stored in an array)
    By nair in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 01:27 PM
  3. Replies: 1
    Last Post: 09-20-2010, 11:17 AM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. trouble with strings and characters
    By mhenderson in forum C Programming
    Replies: 7
    Last Post: 08-02-2006, 01:29 PM