473,480 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regarding free

Hello Every one,

I am writing a program in gcc compiler which contains many
functions. In main I created memory for a variable and tried to free
in another function. But I am getting error as segmentation fault at
some times or glibc free Invalid Pointer at some times. Can any one of
you help me out. But I must free the data.

A(){
mem= (char *)malloc(sizeof(char));
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
....
free(K);
return j;
}
This one will give you an idea.
Thanking you all
Bye
Raghu

Mar 29 '07 #1
11 1662
raghu said:
Hello Every one,

I am writing a program in gcc compiler which contains many
functions. In main I created memory for a variable and tried to free
in another function. But I am getting error as segmentation fault at
some times or glibc free Invalid Pointer at some times. Can any one of
you help me out. But I must free the data.

A(){
mem= (char *)malloc(sizeof(char));
Drop the cast.

Note that sizeof(char) is defined to be 1. So you're allocating a single
byte of memory. You can't store much in one byte.
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
j's value is indeterminate, so anything you write through it is wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 29 '07 #2
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char)); // n bytes.

mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
Mar 29 '07 #3
raghu said:
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char));
I have already suggested that you drop the cast. If you ignore good
advice, why ask for it?

// n bytes.
>
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
I have already explained that j is indeterminate, so any writing through
it is invalid. If you ignore good advice, why ask for it?
.
.
free(K); // can i free the allocated memory for K in A().
Is it a valid one.
By this stage the program is so hopelessly riddled with bugs that it is
simply impossible to predict its behaviour with any assurance.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 29 '07 #4
"raghu" <ra*********@gmail.comwrites:
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char)); // n bytes.

mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
.
.
free(K); // can i free the allocated memory for K in A().
Is it a valid one.
return (j);
}
Try posting an actual complete program that exhibits the problem.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 29 '07 #5
On Mar 29, 12:20 pm, Keith Thompson <k...@mib.orgwrote:
"raghu" <ragavaku...@gmail.comwrites:
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char)); // n bytes.
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
.
.
free(K); // can i free the allocated memory for K in A().
Is it a valid one.
return (j);
}

Try posting an actual complete program that exhibits the problem.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

int main()
{
char *data;
data = (char *)malloc(sizeof(char) * 7);
strcpy(data, "hello");
data = process(data , strlen("hello"));
printf("%s", data);
}
char * process(char *data, int length)
{

char *temp;
temp = (char *)malloc(sizeof(char) * length);
//code for copying the data to temp which are only in odd places.
free(data);//this gives me the error
//even though data is freed memory for temp will be pointed to data
when temp returned
return temp;//return pointer to the temp
}

I think it will give you an idea.

Sorry for previous vague data which I posted.
Thanks in advance

-Raghu

Mar 29 '07 #6
raghu said:
On Mar 29, 12:20 pm, Keith Thompson <k...@mib.orgwrote:
>>
Try posting an actual complete program that exhibits the problem.

int main()
{
char *data;
data = (char *)malloc(sizeof(char) * 7);
strcpy(data, "hello");
data = process(data , strlen("hello"));
printf("%s", data);
}
char * process(char *data, int length)
{

char *temp;
temp = (char *)malloc(sizeof(char) * length);
//code for copying the data to temp which are only in odd places.
free(data);//this gives me the error
//even though data is freed memory for temp will be pointed to data
when temp returned
return temp;//return pointer to the temp
}

When I compile this (after hacking //X into /*X*/ for my compiler's
benefit), I get the following diagnostic messages:

foo.c:2: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:4: warning: implicit declaration of function `malloc'
foo.c:4: warning: cast does not match function type
foo.c:6: warning: implicit declaration of function `process'
foo.c:6: warning: assignment makes pointer from integer without a cast
foo.c:7: warning: implicit declaration of function `printf'
foo.c:8: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:10: warning: no previous prototype for `process'
foo.c:10: warning: type mismatch with previous implicit declaration
foo.c:6: warning: previous implicit declaration of `process'
foo.c:10: warning: `process' was previously implicitly declared to
return `int'
foo.c: In function `process':
foo.c:13: warning: cast does not match function type
foo.c:15: warning: implicit declaration of function `free'

Here's a version which fixes all of the above problems.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *process(char *data,
size_t length);

int main(void)
{
char *data;

data = malloc(7);
if(data != NULL)
{
strcpy(data, "hello");
data = process(data, strlen("hello") + 1); /* add 1 for terminator
*/
if(data != NULL)
{
printf("[%s]\n", data);
free(data);
}
}
return 0;
}
char *process(char *data,
size_t length)
{
char *temp;

temp = malloc(length);

/*code for copying the data to temp which are only in odd places.*/
if(temp != NULL)
{
memcpy(temp, data, length);
}
free(data);
return temp;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 29 '07 #7
raghu wrote:
>
int main()
{
char *data;
data = (char *)malloc(sizeof(char) * 7);
strcpy(data, "hello");
data = process(data , strlen("hello"));
printf("%s", data);
}
char * process(char *data, int length)
{

char *temp;
temp = (char *)malloc(sizeof(char) * length);
//code for copying the data to temp which are only in odd places.
Do you mean that you /left code out/ at this point?

Since you don't understand what's going wrong, why do you think
you know what's safe to leave out?

Keith asked for a /complete/ program.
free(data);//this gives me the error
I think it very likely that the code you left out updates `data` or
writes outside the allocated store.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
A rock is not a fact. A rock is a rock.

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Mar 29 '07 #8
On Mar 29, 1:03 pm, "raghu" <ragavaku...@gmail.comwrote:
On Mar 29, 12:20 pm, Keith Thompson <k...@mib.orgwrote:


"raghu" <ragavaku...@gmail.comwrites:
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char)); // n bytes.
mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
.
.
free(K); // can i free the allocated memory for K in A().
Is it a valid one.
return (j);
}
Try posting an actual complete program that exhibits the problem.
--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

int main()
{
char *data;
data = (char *)malloc(sizeof(char) * 7);
strcpy(data, "hello");
data = process(data , strlen("hello"));
printf("%s", data);}

char * process(char *data, int length)
{

char *temp;
temp = (char *)malloc(sizeof(char) * length);
length+1
//code for copying the data to temp which are only in odd places.
It should be correct,
free(data);//this gives me the error
are you sure this only gives error ?
what error, ? Segfault ? try to debug and you may find out actual
place.
//even though data is freed memory for temp will be pointed to data
when temp returned
return temp;//return pointer to the temp

}

I think it will give you an idea.

Sorry for previous vague data which I posted.
Thanks in advance

-Raghu- Hide quoted text -

- Show quoted text -

Mar 29 '07 #9
On 29 Mar 2007 01:03:47 -0700, in comp.lang.c , "raghu"
<ra*********@gmail.comwrote:
>data = (char *)malloc(sizeof(char) * 7);
You have been told many times already that you do not need the cast,
and that sizeof(char) is by definition 1. Please listen to that
advice.
data = malloc(7);

>data = process(data , strlen("hello"));
strlen("hello") will be five.....
>temp = (char *)malloc(sizeof(char) * length);
.....so this allocates space for five chars.

However you need space for six, if you plan to copy whats in 'data'
across - five for "hello" plus one for the terminating null to ensure
it is a string.
>//code for copying the data to temp which are only in odd places.
You left out the important bit. Probably at this point you write more
data to 'temp' than you can and corrupt your system memory. When you
try to free() variables, the system can't work out where they are any
more, and dies.

Top tip: if free() is crashing, it frequently means you didn't
allocate enough space somewhere, or walked off the end of an array.
Not necessarily in the object you're trying to free - the corruption
could be a totally different object, but it has damaged the object you
want to free.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 29 '07 #10
raghu wrote:
Sorry what i mean to say is:
A(){
mem= (char *)malloc(n * sizeof(char)); // n bytes.

mem = B(mem);
}
B(char *K)
{
char *j;
memcpy(j, K, length);
.
.
free(K); // can i free the allocated memory for K in A().
Is it a valid one.
return (j);
}

Your "code" has lots of problems. In fact, it is nowhere close to
compilable, and the best anyone can do is guess what you have in mind.
Always post real code, not some mockup that is supposedly like it. Tell
me if you had something like the following in mind, and if so we can
discuss what problems there might be. If this is not what you meant,
then show us some real comprehensible code.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LOTS 1024

char *B(char *, size_t);

void A(size_t n)
{
char *mem;
if (!(mem = malloc(n))) {
fprintf(stderr, "malloc failed.\n");
exit(EXIT_FAILURE);
}
mem = B(mem, n);
}

char *B(char *K, size_t length)
{
static char j[LOTS];
if (length LOTS)
fprintf(stderr, "Get real.\n");
else
memcpy(j, K, length);
free(K);
return j;
}

int main(void)
{
A(LOTS);
return 0;
}

Mar 29 '07 #11
raghu wrote:
[headerless code]

malloc is declared in <stdlib.h>. Include it.
strcpy is declared in <string.h>. Include it.
printf if declared in <stdio.h>. Include it.
>
int main()
{
char *data;
data = (char *)malloc(sizeof(char) * 7);
You have repeatedly been told about the useless cast '(char *)' and the
useless 'sizeof(char)'. Since you continue to ignore sound advice,
there is little reason to give it. In fact, such behavior suggests a
troll rather than an honest seeker of help.
Mar 29 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2180
by: threeseas | last post by:
Because the project is done in python and in support of FOSS http://msdn.microsoft.com/architecture/overview/softwarefactories/default.aspx?pull=/library/en-us/dnmaj/html/aj3softfac.asp ...
1
1052
by: praba kar | last post by:
Dear All, I have doubt regarding headers in cgi programming. If I gives "Content-Type:text/plain" then I try to print html contents. Is right or wrong after giving content-type: text/plain? ...
1
2765
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
2
1609
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
8
1257
by: pobnospam | last post by:
So far I need to store 2 components: Departments and Supplies. My problem: Multiple departments may own the same inventory. How do I join my supply table with the department table. Do I need an...
13
1837
by: Vijay | last post by:
Hi All, I am learning C++ and have one question. Using free or delete we can release the memory. After releasing memory where this released memory will go.. Does it go to back operating...
17
2492
by: farshid | last post by:
I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120...
42
6733
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
11
365
by: sam_cit | last post by:
Hi Everyone, int main() { char *p = malloc(100); p = "india"; //assuming p is not null p++; free(p); }
8
2014
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
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...
1
6743
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...
0
5344
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,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
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...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
185
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...

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.