invalid pointer: 0x08ce6158 ***

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    invalid pointer: 0x08ce6158 ***

    I'm very close of shooting myself in the head right now. Why in hell does free(xx[0]) give me an error: *** glibc detected *** free(): invalid pointer: 0x08ce6158 ***

    Code:
    // The function returns something like the argv[] parameter in main.
    char** parser (char* ch)
    {
    	char** xx = NULL;
    	char* temp;			// contains the input which will be modified when using strtok
    	int length = strlen (ch);	// length of the string
    	char* token;
    	int numOfTokens = 0;
    	int i = 0;
    
            // copy to use it for counting tokens, since it gets modified (can't use ch)
    	temp = (char*)malloc (length + 1);
    	strcpy (temp, ch);		
    	
    	token = strtok(temp, " ");
    	while (token != NULL)
    	{	numOfTokens++;	token = strtok(NULL, " ");	}
    
    	xx = (char**)malloc (1 * numOfTokens + 1);
    	for (i = 0; i < numOfTokens; i++)
    		xx[i] = (char*)malloc (50);
    
    	token = strtok(ch, " ");
    	strcpy(xx[0],token);     // A living proof that show xx[0]is freeable
    
    	
    	i = 1;
    	while ((token = strtok(NULL, " \n")) != NULL)
    	{	
    		strcpy (xx[i], token);
    		i++;
    	}
    	xx[i] = NULL;     // Last arg is NULL
    	free (token);
    	free (xx[0]);      // my bane!!!!!
    	free (temp);
    	return xx;
    }
    Please anyone. This is driving me crazy!

    Thank you!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    The issue is the memory allocation
    xx = (char**)malloc (1 * numOfTokens + 1);
    change this to
    xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);


    Thanks
    Raghuram

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Changed thread title.

      P.S Is the gun loaded?

      Comment

      • Sebouh
        New Member
        • Feb 2007
        • 77

        #4
        I'm reloading the gun.


        It didn't work. I didn't think it would have made any difference either. A pointer is always 1 byte long.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Hi,
          What is the input string you are passing as argument to this function.?
          I tried with input "This is true" ad it is working fine.
          Thanks
          Raghuram

          Comment

          • Sebouh
            New Member
            • Feb 2007
            • 77

            #6
            Originally posted by gpraghuram
            Hi,
            What is the input string you are passing as argument to this function.?
            I tried with input "This is true" ad it is working fine.
            Thanks
            Raghuram
            I'm passing "/root/CMPS272_a2/test-batch 2 3".
            It's supposed to be an arguement ofr execvp().

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              Hi,
              I have made some minor modifications in the code and it is working fine for me.
              Initially i also got the sebmentaion fault..
              Code:
              char** parser (char* ch)
              {
              	char** xx = NULL;
              	char* temp;			// contains the input which will be modified when using strtok
              	char* token;
              	int numOfTokens = 0;
              	int i = 0;
              	int length = strlen (ch);	// length of the string
              
                      // copy to use it for counting tokens, since it gets modified (can't use ch)
              	temp = (char*)malloc (length + 1);
              	strcpy (temp, ch);		
              	
              	token = strtok(temp, " ");
              	while (token != NULL)
              	{	
              		numOfTokens++;	
              		token = strtok(NULL, " ");	
              	}
              
              	//xx = (char**)malloc (1 * numOfTokens + 1);
              	xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
              	for (i = 0; i <= numOfTokens; i++)
              		xx[i] = (char*)malloc (50);
              
              	strcpy (temp, ch);
              	//token = strtok(ch, " ");
              	token = strtok(temp, " ");
              	strcpy(xx[1],token);     // A living proof that show xx[0]is freeable
              	i = 2;
              	while ((token = strtok(NULL, " ")) != NULL)
              	{	
              		strcpy (xx[i], token);
              		i++;
              	}
              	xx[i] = NULL;     // Last arg is NULL
              	free (token);
              	free (xx[0]);      // my bane!!!!!
              	free (temp);
              	return xx;
              }
              Thanks
              Raghuram

              Comment

              • Sebouh
                New Member
                • Feb 2007
                • 77

                #8
                Originally posted by gpraghuram
                Hi,
                I have made some minor modifications in the code and it is working fine for me.
                Initially i also got the sebmentaion fault..
                Code:
                char** parser (char* ch)
                {
                	char** xx = NULL;
                	char* temp;			// contains the input which will be modified when using strtok
                	char* token;
                	int numOfTokens = 0;
                	int i = 0;
                	int length = strlen (ch);	// length of the string
                
                        // copy to use it for counting tokens, since it gets modified (can't use ch)
                	temp = (char*)malloc (length + 1);
                	strcpy (temp, ch);		
                	
                	token = strtok(temp, " ");
                	while (token != NULL)
                	{	
                		numOfTokens++;	
                		token = strtok(NULL, " ");	
                	}
                
                	//xx = (char**)malloc (1 * numOfTokens + 1);
                	xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
                	for (i = 0; i <= numOfTokens; i++)
                		xx[i] = (char*)malloc (50);
                
                	strcpy (temp, ch);
                	//token = strtok(ch, " ");
                	token = strtok(temp, " ");
                	strcpy(xx[1],token);     // A living proof that show xx[0]is freeable
                	i = 2;
                	while ((token = strtok(NULL, " ")) != NULL)
                	{	
                		strcpy (xx[i], token);
                		i++;
                	}
                	xx[i] = NULL;     // Last arg is NULL
                	free (token);
                	free (xx[0]);      // my bane!!!!!
                	free (temp);
                	return xx;
                }
                Thanks
                Raghuram
                Thanks for the effort mate, but i can't see which change solved the real problem. i think if you change the free(xx[0]) to free(xx[1]), you'll ge the same problem, thought i'm not sure since my program requires xx[0] to have the prog name, just like argv[].

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by Sebouh
                  It didn't work. I didn't think it would have made any difference either. A pointer is always 1 byte long.
                  This is quite seriously wrong. If a pointer was only 1 byte long then it would only be able to address 256 bytes of memory (at addresses 0 to 255).

                  On many systems a pointer is the same size as an int, but that is a rule of thumb rather than a specification. Pointers are no specific size except to say that they are generally large enough to access the entire memory range for the target system. Additionally pointers to different types do not have to be the same size or have the same bit pattern.

                  I do not know if this is the cause of you problem but there is still an error in

                  Code:
                  xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
                  Operator precedence is causing this to allocated sizeof(char *) -1 bytes too few so you are writing off the the end of the allocated memory and invoking undefined behaviour.

                  You need parentheses round the addition

                  Code:
                  xx = (char**)malloc (sizeof(char*) * [b]([/b]numOfTokens + 1[b])[/b]);

                  Comment

                  • Sebouh
                    New Member
                    • Feb 2007
                    • 77

                    #10
                    Originally posted by Banfa
                    This is quite seriously wrong. If a pointer was only 1 byte long then it would only be able to address 256 bytes of memory (at addresses 0 to 255).

                    On many systems a pointer is the same size as an int, but that is a rule of thumb rather than a specification. Pointers are no specific size except to say that they are generally large enough to access the entire memory range for the target system. Additionally pointers to different types do not have to be the same size or have the same bit pattern.

                    I do not know if this is the cause of you problem but there is still an error in

                    Code:
                    xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
                    Operator precedence is causing this to allocated sizeof(char *) -1 bytes too few so you are writing off the the end of the allocated memory and invoking undefined behaviour.

                    You need parentheses round the addition

                    Code:
                    xx = (char**)malloc (sizeof(char*) * [b]([/b]numOfTokens + 1[b])[/b]);
                    Damn it!
                    You're totally right Banfa. I have no idea why i though a pointer is 1 byte long. I guess it slipped my mind. I guess that's why i didn't see the precedence thing either.
                    Thanks alot!

                    Comment

                    Working...