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 *** -
// 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!
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
Changed thread title.
P.S Is the gun loaded?
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.
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
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().
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.. -
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
Hi,
I have made some minor modifications in the code and it is working fine for me.
Initially i also got the sebmentaion fault.. -
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[].
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 -
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 -
xx = (char**)malloc (sizeof(char*) * (numOfTokens + 1));
-
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 -
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 -
xx = (char**)malloc (sizeof(char*) * (numOfTokens + 1));
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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
|
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
|
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!
..
|
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...
| |
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);
...
|
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 ;
|
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. :)
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |