473,796 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invalid pointer: 0x08ce6158 ***

78 New Member
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 2165
gpraghuram
1,275 Recognized Expert Top Contributor
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 MVP
Changed thread title.

P.S Is the gun loaded?
Apr 5 '07 #3
Sebouh
78 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
3863
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 pointer to a pointer in the .c: extern struct example_struct **pointer; and somewhere in the code i tried: pointer = &ex_ptr;
5
2246
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 an attempt to write to this pointer causes the program to exit with a core dump. Output after compiling with HP c-compiler: 1. ffffff78 1. 7eff3358
3
13373
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 create or open the Web project located at the following URL: 'http://localhost/MyWebTest'. 'HTTP/1.1 500 Internal Server Error'. The Event log has: "Failed to execute request because the App-Domain could not be
4
4342
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! free(): invalid pointer 0x51d760! free(): invalid pointer 0x51d7e0! ..
6
3023
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 across a problem I can't really explain: When deleting an object from an IStorage, the space it used up will not be freed, but rather marked as unused and overwritten the next time you add an object to the storage. This is obviously working as...
1
2948
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 (1) { if((kfp = kd_open(kd_filename, READ, NULL , INIT_YES))== NULL) { printf("%s: File not found ...\n", kd_filename); if (!forceflag) { sleep(5); ...
11
16944
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
18748
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 delete a object generated by new operator. I really do not known why? And I need your help. Please tell me why and when this error should happen? So that I can fix my problem. :)
68
2702
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 issue occurred in the first place. The project uses a simple template class that acts as a buffer. Initially it has a fixed length, but it's append methods will extend the size of the buffer if necessary. Another class defines a struct, that...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.