using strcat(Segmentation Fault)

This is a discussion on using strcat(Segmentation Fault) within the C Programming forums, part of the General Programming Boards category; ok, so I am writing this code, and I keep getting a seg fault. I went into gdb and it ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    using strcat(Segmentation Fault)

    ok, so I am writing this code, and I keep getting a seg fault. I went into gdb and it told me that it was seg faulting on the strcat() function.
    here is my code

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    const char* soh="00000001";
    const char* eot="00000100";
    const char* esc="00011011";
    const char* soh_e="0001101101000001";
    const char* eot_e="0001101101000010";
    const char* esc_e="0001101101000011";
    const char*z="0";
    const char*o="1";
    unsigned char bin[]={1,2,4,8,16,32,64,128};
    char  bowling[4096];
    char  goofy[4096];
    int main(int argc, char*argv[]){
    	if (argc < 2){
    		printf("You need to enter a filename");
    		exit(1);
    	}
    	int fd=open(argv[2], O_RDWR, 0);
    	read (fd, goofy, 4096);
    	int count;
    	char string[4096];
    	int leng=strlen(goofy);
    	int i;
    	printf("Hello");
    	for (i=0; i<leng; i++){//This portion of code is paraphrased from klausi's code
    				char c=*string; /* the character string points to at the moment */
    				for(count=7;count>=0;count--){ 
    					if(c>=bin[count])
    					{
    						strcat(string, o);
    						c-=bin[count];
    					}
    					else{
    						strcat(string,z);
    					}
    			}
    	}
    	int j;
    	for (j=0; j<strlen(string)-8; j++){
    		size_t ch=strncmp(&string[j],soh, 8);
    		size_t cha=strncmp(&string[j], eot, 8);
    		size_t chan=strncmp(&string[j], esc, 8);
    		if (ch==0){
    			strcat(bowling, soh_e);
    			j+=7;
    		}
    		else if (cha==0){
    			strcat(bowling, eot_e);
    			j+=7;
    		}
    		else if (chan==0){
    			strcat(bowling, esc_e);
    			j+=7;
    		}
    		else{
    			strcat(bowling, &string[j]);
    		}
    	}
    		int fd2=open("fdhjmk.txt", O_WRONLY, O_CREAT|O_TRUNC);
    		write(fd2, bowling, 4096);
    		close(fd);
    		close(fd2);
    		}

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    There are too many things wrong with your code...as far as the segfaulting its because you are attempting to add on more to the string than the string is large...you're doing a strcat for every letter in string, which is defined as 4096, the bowling buffer is also 4096 and you're catting on a string that is like 20+ chars in length so you're easily going over..

    Now I don't know what you're trying to accomplish with these strncmp's you're running.....first of all strncmp compares two STRINGS, ie you pass it 2 char * pointers pointing to a buffer of characters terminating in a \0.

    When you say string[j] that is a "char" you need to pass a "char *" which is just: "string". Thats just at first glance, I'm sure there are more problems.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you do not check return value of read...
    read could left your goofy array not-nulterminated - you cannot call strlen on such array

    you string array is not initialized - you cannot call strcat on such array as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Would increasing the size of the buffer that I am using work. Oh and I was using this on a file that was around 5+/- a little bytes, so the size of the arrays that I was using should have been sufficient. And as far as the strncamp, I am trying to iterate along a string. eg say I have the string Hello, my name is and the const string y na. I want the loop that the strncmp to first compare Hell, then ello, then llo then lo m, until it got to the matching array(or the end of file)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cloudsword View Post
    Would increasing the size of the buffer that I am using work. Oh and I was using this on a file that was around 5+/- a little bytes, so the size of the arrays that I was using should have been sufficient. And as far as the strncamp, I am trying to iterate along a string. eg say I have the string Hello, my name is and the const string y na. I want the loop that the strncmp to first compare Hell, then ello, then llo then lo m, until it got to the matching array(or the end of file)
    Did you even read vart's reply?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    I thought that strcat() when it got done, ended the cocatenation with the NULL character.
    By the way, how do you "initialize" an array?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by cloudsword
    I thought that strcat() when it got done, ended the cocatenation with the NULL character.
    Yes, it does. But first, it has to find the null character in order to begin concatenation. Missing null character => problem.

    Quote Originally Posted by cloudsword
    By the way, how do you "initialize" an array?
    You could write:
    Code:
    char string[4096] = "";
    Although at the very least:
    Code:
    char string[4096];
    string[0] = '\0';
    Last edited by laserlight; 03-30-2010 at 09:01 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by cloudsword View Post
    I thought that strcat() when it got done, ended the cocatenation with the NULL character.
    By the way, how do you "initialize" an array?
    when done strcat appends nul-character... but it should start with nul-terminated string

    Code:
    char buf[100] = "";
    or

    Code:
    char buf = {0};
    or

    Code:
    char buf[100];
    buf[0] = 0;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cloudsword View Post
    I thought that strcat() when it got done, ended the cocatenation with the NULL character.
    By the way, how do you "initialize" an array?
    Congratulations, you managed to read half of his reply.
    Now go read the other half.

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    When you do strcat on your string[4096] it "initalizes" the string, so in fact your string is being initialized, just not in a very safe way.

    What you need to understand is that when you want to add on data to a string or copy data into a string you have to make DAMN sure that string has enough space to fit the data, otherwise you get undefined behavior (likely just a seg fault).

    In order to do this you simply always check that the size of the data being added is less than or equal to the size of the buffer you are adding it to. If you always check this first, even if you make a mistake and try to add more data than space available, your check will prevent it from happening.

    Ie, your loop should be something like this:
    Code:
    char s[4096], * ss = "Some string";
    int size = 0;
    size_t len = strlen(ss);
    
    while (size+len < 4096-1) { /* note we leave 1 member at the end for a possible trailing \0 */
      strcat(s,ss);
      size += len;
    }
    This is basically just pseudo code but you should get my meaning.
    Last edited by nonpuz; 03-30-2010 at 09:08 AM.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by nonpuz View Post
    When you do strcat on your string[4096] it "initalizes" the string,
    where do you get this idea?

    first thing strcat will do - it will search for the nul-char... if your array does not contain nul-char - strcat will continue to scan the memory beyond the bounds of array till the memory access error occurs or till some random memory byte will have a 0 value, then strcat will start writing from this memory address overwriting some random memory regeon with unpredictable results
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    When I run this, it works fine:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char s[4096], * ss = "Some string";
      int size = 0;
      size_t len = strlen(ss);
    
      printf("'%s'\n",s);
    
      while (size+len < 4096-1) { /* note we leave 1 member at the end for a possible trailing \0 */
        strcat(s,ss);
        size += len;
      }
      printf("%s\n",s);
      return 0;
    }
    So are you saying that on my machine it auto-initializes the string to "\0" ie this is not PORTABLE? Or are you mistaken? Now I'm not sure myself.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by nonpuz
    So are you saying that on my machine it auto-initializes the string to "\0" ie this is not PORTABLE?
    It is not inconceivable that the first char of s is 0, by chance.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Bleh, yeah I'm stupid.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Hey I am having issues, this time with strncmp(). I have initialized the char arrays like vart helped me to do, and now it is seg faulting for strncmp.

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

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


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