473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

invalid pointer: 0x08ce6158 ***

78
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 ***

Expand|Select|Wrap|Line Numbers
  1. // The function returns something like the argv[] parameter in main.
  2. char** parser (char* ch)
  3. {
  4.     char** xx = NULL;
  5.     char* temp;            // contains the input which will be modified when using strtok
  6.     int length = strlen (ch);    // length of the string
  7.     char* token;
  8.     int numOfTokens = 0;
  9.     int i = 0;
  10.  
  11.         // copy to use it for counting tokens, since it gets modified (can't use ch)
  12.     temp = (char*)malloc (length + 1);
  13.     strcpy (temp, ch);        
  14.  
  15.     token = strtok(temp, " ");
  16.     while (token != NULL)
  17.     {    numOfTokens++;    token = strtok(NULL, " ");    }
  18.  
  19.     xx = (char**)malloc (1 * numOfTokens + 1);
  20.     for (i = 0; i < numOfTokens; i++)
  21.         xx[i] = (char*)malloc (50);
  22.  
  23.     token = strtok(ch, " ");
  24.     strcpy(xx[0],token);     // A living proof that show xx[0]is freeable
  25.  
  26.  
  27.     i = 1;
  28.     while ((token = strtok(NULL, " \n")) != NULL)
  29.     {    
  30.         strcpy (xx[i], token);
  31.         i++;
  32.     }
  33.     xx[i] = NULL;     // Last arg is NULL
  34.     free (token);
  35.     free (xx[0]);      // my bane!!!!!
  36.     free (temp);
  37.     return xx;
  38. }
Please anyone. This is driving me crazy!

Thank you!
Apr 5 '07 #1
9 2153
gpraghuram
1,275 Expert 1GB
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
Apr 5 '07 #2
r035198x
13,262 8TB
Changed thread title.

P.S Is the gun loaded?
Apr 5 '07 #3
Sebouh
78
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.
Apr 5 '07 #4
gpraghuram
1,275 Expert 1GB
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
Apr 5 '07 #5
Sebouh
78
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().
Apr 5 '07 #6
gpraghuram
1,275 Expert 1GB
Hi,
I have made some minor modifications in the code and it is working fine for me.
Initially i also got the sebmentaion fault..
Expand|Select|Wrap|Line Numbers
  1. char** parser (char* ch)
  2. {
  3.     char** xx = NULL;
  4.     char* temp;            // contains the input which will be modified when using strtok
  5.     char* token;
  6.     int numOfTokens = 0;
  7.     int i = 0;
  8.     int length = strlen (ch);    // length of the string
  9.  
  10.         // copy to use it for counting tokens, since it gets modified (can't use ch)
  11.     temp = (char*)malloc (length + 1);
  12.     strcpy (temp, ch);        
  13.  
  14.     token = strtok(temp, " ");
  15.     while (token != NULL)
  16.     {    
  17.         numOfTokens++;    
  18.         token = strtok(NULL, " ");    
  19.     }
  20.  
  21.     //xx = (char**)malloc (1 * numOfTokens + 1);
  22.     xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
  23.     for (i = 0; i <= numOfTokens; i++)
  24.         xx[i] = (char*)malloc (50);
  25.  
  26.     strcpy (temp, ch);
  27.     //token = strtok(ch, " ");
  28.     token = strtok(temp, " ");
  29.     strcpy(xx[1],token);     // A living proof that show xx[0]is freeable
  30.     i = 2;
  31.     while ((token = strtok(NULL, " ")) != NULL)
  32.     {    
  33.         strcpy (xx[i], token);
  34.         i++;
  35.     }
  36.     xx[i] = NULL;     // Last arg is NULL
  37.     free (token);
  38.     free (xx[0]);      // my bane!!!!!
  39.     free (temp);
  40.     return xx;
  41. }
  42.  
Thanks
Raghuram
Apr 5 '07 #7
Sebouh
78
Hi,
I have made some minor modifications in the code and it is working fine for me.
Initially i also got the sebmentaion fault..
Expand|Select|Wrap|Line Numbers
  1. char** parser (char* ch)
  2. {
  3.     char** xx = NULL;
  4.     char* temp;            // contains the input which will be modified when using strtok
  5.     char* token;
  6.     int numOfTokens = 0;
  7.     int i = 0;
  8.     int length = strlen (ch);    // length of the string
  9.  
  10.         // copy to use it for counting tokens, since it gets modified (can't use ch)
  11.     temp = (char*)malloc (length + 1);
  12.     strcpy (temp, ch);        
  13.  
  14.     token = strtok(temp, " ");
  15.     while (token != NULL)
  16.     {    
  17.         numOfTokens++;    
  18.         token = strtok(NULL, " ");    
  19.     }
  20.  
  21.     //xx = (char**)malloc (1 * numOfTokens + 1);
  22.     xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
  23.     for (i = 0; i <= numOfTokens; i++)
  24.         xx[i] = (char*)malloc (50);
  25.  
  26.     strcpy (temp, ch);
  27.     //token = strtok(ch, " ");
  28.     token = strtok(temp, " ");
  29.     strcpy(xx[1],token);     // A living proof that show xx[0]is freeable
  30.     i = 2;
  31.     while ((token = strtok(NULL, " ")) != NULL)
  32.     {    
  33.         strcpy (xx[i], token);
  34.         i++;
  35.     }
  36.     xx[i] = NULL;     // Last arg is NULL
  37.     free (token);
  38.     free (xx[0]);      // my bane!!!!!
  39.     free (temp);
  40.     return xx;
  41. }
  42.  
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[].
Apr 5 '07 #8
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. xx = (char**)malloc (sizeof(char*) * (numOfTokens + 1));
  2.  
Apr 5 '07 #9
Sebouh
78
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

Expand|Select|Wrap|Line Numbers
  1. xx = (char**)malloc (sizeof(char*) * numOfTokens + 1);
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. xx = (char**)malloc (sizeof(char*) * (numOfTokens + 1));
  2.  
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!
Apr 8 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a...
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
3
by: JerryW | last post by:
I uninstalled/reinstalled .NET 2003 with no errors. When I try to create a Visual C# ASP.NET Web Application I get the error: "The Web server reported the following error when attempting to...
4
by: c language | last post by:
Hi All, I have written a C++ program, it can be compiled (in UNIX) but when I run it, I usually gives the following errors: free(): invalid pointer 0x51d4a0! free(): invalid pointer 0x51d460!...
6
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come...
1
by: mahiapkum | last post by:
hello all i have a code which looks fine when reviewed but when the application has a long run say for example of 2 days it gets exit, because of glibc error and the sample code is as follows: while...
11
by: Spiros Bousbouras | last post by:
#include <stdlib.h> int main(void) { char **p1 ; const char **p2 ; p1 = malloc(5 * sizeof(char *)) ; if (p1 == 0) return EXIT_FAILURE ; p2 = p1 + 1 ; p2 - p1 ;
3
by: Renzr | last post by:
I have a C++ package which works very well in the 32-bit Linux-like OS. However, it will lead to a "*** glibc detected *** ./ex2: munmap_chunk(): invalid pointer" in 64-bit (Fedora 7-64), when it...
68
by: DaveJ | last post by:
Recently I was working on a project where I came across an issue where the program cored and reported "free(): invalid pointer". I found a resolution for this, but don't fully understand why the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.