how to free the allocated memory outside of the function

M

madhur

Hello
I have a function which is defined as follows:

char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}

In this function, I return a pointer to a char in the string newstr. Since
the newstr was
allocated dynamically, howcan I free this memory after I have used up the
pointer outside
of the function since "newstr" will get out of scope after the return.

Madhur Ahuja
India
 
A

Al Bowers

madhur said:
Hello
I have a function which is defined as follows:

char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}

In this function, I return a pointer to a char in the string newstr. Since
the newstr was
allocated dynamically, howcan I free this memory after I have used up the
pointer outside
of the function since "newstr" will get out of scope after the return.


The deallocation must refer to the value returned by the function
malloc. Since this value is lost after function execution, you have
no means to free the allocation. This results in a classic memory
leak. So, you will need to redesign the function some so that
the return value is newstr, not newstr+i+1.

IMO using dynamic allocations for this function is probably
not the best choice. However if you want to define the function
using dynamic allocations, I would at least make it more
versatile so that it can cut not just the front of the source
string, but, anywhere within the source string. You can use
function strstr to find the first occurrence of the cut string.

Example:

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

char *strcut(const char *source, const char *cut)
{
char *s1, *newstr = NULL;

s1 = strstr(source,cut);
if(s1)
{
newstr = malloc(strlen(source)-strlen(cut)+1);
if(newstr)
{
strncpy(newstr,source,s1-source);
strcpy(newstr+(s1-source),s1+strlen(cut));
}
}
return newstr;
}

int main(void)
{
char *s = strcut("This is not a good function", "not ");
if(s) printf("s = \"%s\"\n",s);
free(s);
return 0;
}
 
E

Emmanuel Delahaye

madhur said:
I have a function which is defined as follows:

char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

why 50 ?
strcpy(newstr,str);

what if i is >= than 50 ?
while(newstr==cut)
++i;
return(newstr+i+1);
}

In this function, I return a pointer to a char in the string newstr. Since
the newstr was
allocated dynamically, howcan I free this memory after I have used up the
pointer outside
of the function since "newstr" will get out of scope after the return.


With free(). But because the user knows nothing about your trick (+1+1), it
will not work if free() is used as-it.

Should be

char* s = strcut("abcdef", "abcxxx");
if (s != NULL)
{
...
free (s-1-1);
}

I suggest you supply a deletor function. BTW, identifiers beginning with str
followed by a lowercase are reserved for future extensions of the language.

void str_cut_delete(char* s)
{
if (s != NULL)
{
free (s - 1 - 1);
}
}

but the whole thing is clumsy to me. What's you spec ?

<<I have two non modifiable strings, I want to get a new string derived from
the first one, but with the beginning pattern common to both strings
removed.>>. BTW what if the pattern in not recognized at all? I suggest
return NULL.

A better approach should be

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

char *str_cut (char const *s_ori, char const *const s_pat)
{
char *s_cut = NULL;

if (s_ori != NULL
&& s_pat != NULL
&& *s_ori != 0
&& *s_pat != 0
&& *s_pat == *s_ori)
{
s_cut = malloc (strlen (s_ori) + 1);

if (s_cut != NULL)
{
char const *ro = s_ori;
char const *rp = s_pat;

while (*ro != 0 && *rp == *ro)
{
ro++;
rp++;
}

strcpy (s_cut, ro);
}
}
return s_cut;
}

#ifdef TEST
#include <stdio.h>
#include <assert.h>

int main (void)
{
/* unit test structure */
typedef struct
{
char const *s_ori;
char const *s_pat;
char const *s_cut;
}
ut_s;

static ut_s const a_ut[] =
{
{NULL, NULL, NULL},
{NULL, "", NULL},
{"", NULL, NULL},
{"", "", NULL}, /* debatable spec. Could be "" */
{"a", "b", NULL},
{"a", "a", ""},
{"abc", "abc", ""},
{"abc", "abd", "c"},
{"abc", "def", NULL},
};

int err = 0;
size_t i;

#define NELEM(a) (sizeof(a)/sizeof*(a))

for (i = 0; !err && i < NELEM (a_ut); i++)
{
ut_s const *const p_ut = a_ut + i;
char *s_cut = str_cut (p_ut->s_ori, p_ut->s_pat);

if (s_cut != NULL)
{
err = strcmp (s_cut, p_ut->s_cut) != 0;
free (s_cut), s_cut = NULL;
}
else
{
err = p_ut->s_cut != NULL;
}

if (err)
{
printf ("ERR at test %u\n", (unsigned) (i + 1));
}

assert (s_cut == NULL);
}

if (i == NELEM (a_ut))
{
puts ("P A S S E D");
}

return 0;
}
#endif
 
K

kal

madhur said:
char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}


It seems to me that this code is afflicted with bad design
and worse implementation.

As for your question, tricks akin to the following may work.
(Why anyone would do this is beyond me!)

char* strcut(const char *str, const char *cut)
{
char *newstr;
int i=0;
newstr=malloc(sizeof(void *) + 50);

strcpy(newstr+sizeof(void *),str);
while(newstr[sizeof(void *)+i]==cut)
++i;

(void *)(newstr+i+1-sizeof(void *)) = newstr;

return(newstr+i+1); /* why the +1 ? */
}

1. What if the first string is longer than 49 characters?
2. What if both the strings are of the same value?
etc. etc.
 
P

Prawit Chaivong

madhur said:
Hello
I have a function which is defined as follows:

char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}

In this function, I return a pointer to a char in the string newstr. Since
the newstr was
allocated dynamically, howcan I free this memory after I have used up the
pointer outside
of the function since "newstr" will get out of scope after the return.

Madhur Ahuja
India


Hi
If I were you , I wouldn't return that pointer from this function.
I would do this.

while ( *str++ == *cut++ ) { }
int len = strlen(str) + 1;
newstr = malloc(len);
memcpy(newstr,str,len);
return newstr;


Prawit Chaivong.
 
M

madhur

kal said:
"madhur" <[email protected]> wrote in message
char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}


It seems to me that this code is afflicted with bad design
and worse implementation.

As for your question, tricks akin to the following may work.
(Why anyone would do this is beyond me!)

char* strcut(const char *str, const char *cut)
{
char *newstr;
int i=0;
newstr=malloc(sizeof(void *) + 50);

strcpy(newstr+sizeof(void *),str);
while(newstr[sizeof(void *)+i]==cut)
++i;

(void *)(newstr+i+1-sizeof(void *)) = newstr;

return(newstr+i+1); /* why the +1 ? */
}

1. What if the first string is longer than 49 characters?
2. What if both the strings are of the same value?
etc. etc.


Hello
The code I presented, was just an example for the subject "how to free the
allocated memory outside of the function". The code is not being used as an
implementation for cutting strings etc or in any other form.
Hope it helps..

Madhur Ahuja
India
 
P

Prawit Chaivong

madhur said:
Hello
I have a function which is defined as follows:

char* strcut(char *str,char *cut)
{
char *newstr;
int i=0;
newstr=malloc(50);

strcpy(newstr,str);
while(newstr==cut)
++i;
return(newstr+i+1);
}

In this function, I return a pointer to a char in the string newstr. Since
the newstr was
allocated dynamically, howcan I free this memory after I have used up the
pointer outside
of the function since "newstr" will get out of scope after the return.

Madhur Ahuja
India


Hi
If I were you , I wouldn't return that pointer from this function.
I would do this.

while ( *str++ == *cut++ ) { }
int len = strlen(str) + 1;


Oop!! I made mistake this line should be
int len = strlen(--str) + 1;
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top