Thread: Joining strings.

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    68

    Joining strings.

    Hello all,

    Having a weird problem with joining some strings. Wondering if anyone could help.

    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    struct Configuration {
    	char *Nickname;
    	char *RealName;
    };
    
    struct Configuration Configuration;
    
    int main() {
    	Configuration.RealName = "Test, from DaveHope";
    	Configuration.Nickname = "davehopetest";
    
    	char *tmp;
    	tmp = "USER ";	// USER test localhost localhost :Test Bot\n\r";
    	strcat(tmp, Configuration.Nickname);
    	strcat(tmp, " localhost localhost :");
    	strcat(tmp, Configuration.RealName);
    	strcat(tmp, "\n\r");
    	
    	printf(tmp);
    	
    	return 0;
    }
    Even when compiled with -Wall, I still get no warnings. It seems to segfault on the strcat() stuff, no idea why. So, erm. Any help would be apprecated!

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You ain't got no memory for your strings!

    Try:

    Code:
    struct Configuration {
    	char Nickname[BUFSIZ];
    	char RealName[BUFSIZ];
    };
    Note that BUFSIZ size varies with implemenation but it usually around 512. You may need larger or smaller or dynamic size.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Do I need to define a size though ? Surely if it's a pointer I don't need it? (If I actually comment out the lines referencing the struct and just append "\n\r" to tmp, it still segfaults. So perhaps that's not the issue ?

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Whilst that wasn't the right bit, I needed a buffsize for tmp, so the correct (or, at least the working code) is:
    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    struct Configuration
    {
    
    	char *Nickname;
    
    	char *RealName;
    
    };
    
    
    struct Configuration Configuration;
    
    int main() {
    	/* Setup the configration options. */
    
    	Configuration.RealName = "Test, from DaveHope";
    	Configuration.Nickname = "davehopetest";
    
    	char tmp[50] = "USER ";	// USER test localhost localhost :Test Bot\n\r";
    	strcat(tmp, Configuration.Nickname);
    	strcat(tmp, " localhost localhost :");
    	strcat(tmp, Configuration.RealName);
    	strcat(tmp, "\n\r");
    	
    	printf(tmp);
    	
    	return 0;
    }

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You don't have to trust me, you can continue along hoping for the best, but I am telling you with extensive professional experience that the code you have is dead wrong and will cause no end of problems. The fact that you just happened to get it to work as expected is a pure accident (I could detail why it works, but that isn't likely to make you any smarter, particularlly when you are convinced my advice is not helpful anyway), any number of events (inside and outside of your control) could cause the program's behavior to change.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Quote Originally Posted by mitakeet
    You don't have to trust me, you can continue along hoping for the best, but I am telling you with extensive professional experience that the code you have is dead wrong and will cause no end of problems. The fact that you just happened to get it to work as expected is a pure accident (I could detail why it works, but that isn't likely to make you any smarter, particularlly when you are convinced my advice is not helpful anyway), any number of events (inside and outside of your control) could cause the program's behavior to change.
    Hrm. Bizare comment that. I appreciate your comment, it helped me locate the issue. My read wonder though, is if -Wall -pedantic doesn't pick it up, what hope is there for someone new to C/C++ ? I'd love to hear the reason why it's now working whereas it wasn't before. I was merely trying to point out that the immediate problem was not with my configuration struct, as the problem was occuring when using strcat() on tmp and a text string (i.e. "\n\r").

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Your character pointers are pointing to fixed-length strings that will appear permanant because you only have the main(). Some compilers will put those strings on the stack, others will put them in other memory locations, some will even put the strings in read-only memory. The variables themselves have no memory associated with them, the memory is 'borrowed'. Further, when you start to use strcpy and strcat you are stepping on memory locations beyond the ends of the fixed-length strings and all bets are off on what happens next. You can get lucky and nothing bad happens, you can get a program crash. Recompiling the program, changing from debug to release, changing optimization flags and certainly changing compilers and/or OSs will cause the code layout to change resulting in different behavior. You continue to address the symptom of the problem, not the cause. I have told you what the cause is.

    BTW, the use of strcpy and strcat are not very safe, you should always use strncpy and strncat or just switch to strings and be done with the entire memory management issue all together.

    WRT why you get no compiler complaints, C/C++ has a very thin safety net and what you are doing is legal code (just wrong in your instance), so there is no way the compiler can read your mind to know what you really want to do.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks very much. Will certainly look at moving to strncpy() and strncat(). Will read up on the pointer issue too so as to better understand the issue before I go trying to correct what I evidently do not understand.

  9. #9
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    A friend wrote this tutorial on pointers, you may find it useful: http://www.daweidesigns.com/pointers.php.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks, will take a look. I've recently invested in C++ for dummies, and have had C++ programming made simple sitting on my bookshelf for over a year now. Although, I must say "Absolute beginners guide to C" by Gregg Perry is an amazing book, although its coverage of pointers is very limited.

  11. #11
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Pointers separate the 'men' from the 'boys', but it is possible to do lots of amazing coding without them. In C++ pointers give way (mostly) to references where the compiler is able to make cleaner determinations about what is going to work, but if you are going to work with large sets of data and don't want to learn how to use the STL container classes (I highly recommend learning them, they will save you hours, if not days, of debugging) you are almost certainly going to have to learn to use pointers and dynamic memory allocation.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. trouble joining strings
    By readi83 in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 03:10 PM
  4. Strings joining
    By Lord CyKill in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2003, 05:02 AM
  5. joining strings with pointers?
    By sworc66 in forum C Programming
    Replies: 6
    Last Post: 09-03-2002, 05:02 PM