regarding free

R

raghu

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
 
R

Richard Heathfield

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.
 
R

raghu

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);
}
 
R

Richard Heathfield

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.
 
K

Keith Thompson

raghu said:
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.
 
R

raghu

Try posting an actual complete program that exhibits the problem.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <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
 
R

Richard Heathfield

raghu said:
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;
}
 
C

Chris Dollin

raghu said:
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
 
S

Sheth Raxit

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.
 
M

Mark McIntyre

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
 
M

Martin Ambuhl

raghu said:
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;
}
 
M

Martin Ambuhl

raghu wrote:
[headerless code]

malloc is declared in <stdlib.h>. Include it.
strcpy is declared in said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top