Thread: Strdup function is eating a letter

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Strdup function is eating a letter

    Hey guys,
    I have this piece of code :

    Code:
     printf("%s\n",cmds1);
     		ficheiro = strdup(cmds1);
      		aux = insereFicheiro(ficheiro,aux);
      		printf("\n *** Ficheiro %s adicionado com sucesso! ***\n\n",ficheiro);
    And the output is the following :

    Code:
    cao
    
    
     *** Ficheiro 
    ao adicionado com sucesso! ***
    Any idea why strdup is eating a letter from the word "cao" ?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    To be able to compile this you would need both my server and client code.
    I'll show you the full function code :

    Code:
    void filemon(char *cmds, char *cmds1)
    {       NFicheiros aux;
    	aux=cria();
            char *ficheiro = NULL;
    	if(strncmp(cmds,"addfile",7)== 0)
     	{       printf("%s\n",cmds1);
     		ficheiro = strdup(cmds1);
      		aux = insereFicheiro(ficheiro,aux);
      		printf("\n *** Ficheiro %s adicionado com sucesso! ***\n\n",ficheiro);
                    showLib(aux);
    		//daemon1(cmds1);
    	}
    It prints well cmds1 but when printing ficheiro it only prints the two last letters.

    Any idea what it might be ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeanWinchester
    To be able to compile this you would need both my server and client code.
    Hence you should come up with the smallest and simplest compilable program that demonstrates this problem.

    Quote Originally Posted by DeanWinchester
    Any idea what it might be ?
    You probably have a buffer overflow problem somewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Thing is,
    if I instead write this piece of code :
    Code:
    void filemon(char *cmds, char *cmds1)
    {       NFicheiros aux;
    	aux=cria();
            char *ficheiro = NULL;
    	if(strncmp(cmds,"addfile",7)== 0)
     	{       printf("%s\n",cmds1);
     		ficheiro = strdup(cmds1);
    		printf("%s\n",ficheiro);
      		aux = insereFicheiro(ficheiro,aux);
    		printf("%s\n",ficheiro);
      		printf("\n *** Ficheiro %s adicionado com sucesso! ***\n\n",ficheiro);
                    showLib(aux);
    		//daemon1(cmds1);
    	}
    }

    It works fine.
    Look at the output :
    Code:
    cao
    cao
    cao
    
    
     *** Ficheiro cao adicionado com sucesso! ***
    
    
    Os ficheiros a serem monitorizados neste momento são : 
    cao
    Isn't it strange ?

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You have a bug somewhere in the ether when adding unrelated code to a function where a bug is first visible seems to change the behavior.

    My guess would be `insereFicheiro'.

    Soma

  7. #7
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Hmm, thanks for your help phantom.
    I was just looking at my insereFicheiro code and there shouldn't be a problem because these functions (insereFicheiro,removeFicheiro) are all coming from an include of "array.c" which I tested myself adding files, making prints and they all worked fine.

    Take a look at the function insereFicheiro :

    Code:
    NFicheiros insereFicheiro(char *ficheiro, NFicheiros aux) {
            int d;
    	Ficheiro novo = (Ficheiro)malloc(sizeof (Node));
    	novo->ficheiro=(char*)malloc(sizeof(ficheiro)+1);
    	novo->nbits= read(d,ficheiro,strlen(ficheiro));
    	novo->proximo=NULL;
    	novo->ficheiro=strdup(ficheiro);
    	if(aux.lista==NULL)
    	{
    	aux.lista=novo;
    	aux.numFicheiros=1;
    	}
    	else
    	{	
    		novo->proximo=aux.lista;
    		aux.lista=novo;
    		aux.numFicheiros++;
    	}
    	return aux;
    }
    Thanks!

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    strdup() does not eat a character, something else you wrote does. Try adding this:

    Code:
            ficheiro = strdup(cmds1);
            printf("cmds1: %s ficheiro: %s\n", cmds1, ficheiro);

    Btw, line 4 and 7 are wrong, you are allocating sizeof(char*)+1 (probably 5 or 9 bytes), then you overwrite that in line 7 with new memory created by strdup.
    Last edited by Subsonics; 05-31-2012 at 04:57 AM.

  9. #9
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by Subsonics View Post
    strdup() does not eat a character, something else you wrote does. Try adding this:

    Code:
            ficheiro = strdup(cmds1);
            printf("cmds1: %s ficheiro: %s\n", cmds1, ficheiro);
    Thanks Subsonics, your advice worked just well.

    I got the following output :
    Code:
    cmds1: cao ficheiro: cao
    
    
    cmds1: cao ficheiro: 
    ao
    which means you were all correct.
    insereFicheiro ain't doing the job properly but which should that be, if I run it separated from the rest it works fine

  10. #10
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    The problem is most likely here :

    Code:
    Ficheiro novo = (Ficheiro)malloc(sizeof (Node));
    	novo->ficheiro=(char*)malloc(sizeof(ficheiro)+1);
    Since it's the first one to be added and most likely it's a problem of memory allocation

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    No. That's not really the right way of looking at it.

    Unless you are overwriting memory you don't own, allocating more chunks of memory doesn't invalidate an existing chunk.

    Just allocating more memory isn't sufficient to cause `strdup' to appear to eat part of the input.

    Soma

  12. #12
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Indeed, makes sense Soma.
    What do you believe the problem is then ?

  13. #13
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Managed to solve my problem.
    Instead of printing ficheiro, I started printing aux.lista->ficheiro

    Works fine, thanks for the help everyone!

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What do you believe the problem is then ?
    I have a very good idea, but without having all the code I can only speculate. O_o

    I don't have all that code, but even with all the code I'd still just tell you to do it yourself. ^_^

    Also, I don't speak whatever language in which the source and comments are written. That tends to make things difficult.

    You are using `sizeof' on a character pointer which is always going to be the same as (`sizeof') that of a void pointer. That's probably wrong.

    Listen though, you are getting caught up in details that aren't necessarily relevant. If you are lucky the problem is directly related to functions ultimately called from `filemon'; that's isn't necessarily the case. Don't trust what you think you know; you need to examine what you have. This is a great time to learn to use a debugger. Setup a "watch" watching the relevant memory and see what is really happening.

    Soma

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Still refusing to learn to debug, instead expecting us to do the work for you. That's sad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capitalize first letter of a word (function)
    By xwielder in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2011, 11:11 PM
  2. Replies: 15
    Last Post: 11-11-2007, 10:40 AM
  3. How eating effects your sleep
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-28-2005, 01:32 PM
  4. Memory Eating - Dangerous?
    By Machewy in forum C++ Programming
    Replies: 23
    Last Post: 12-31-2003, 03:28 AM
  5. function: strdup()
    By cjtotheg in forum C Programming
    Replies: 3
    Last Post: 02-02-2002, 10:08 AM