# C question



## Business_Woman (Jun 15, 2010)

Hello,

I tried to make a C program to concatenate strings.
This is the result:http://pastebin.com/i18pKRd1

However, how are you supposed to free the allocated space given by malloc since the pointer is local to the function only?


----------



## graudeejs (Jun 15, 2010)

you have memory leek

```
int main(){
    char *c = stringcat("coffee","cup",3);
    printf("%s", c);

    free(c);
    return 0;
}
```

should fix this


----------



## graudeejs (Jun 15, 2010)

also you don't need to 

```
char *f = (char *)malloc((str1_length + n)[B]* sizeof(char)[/B]);
```
since char is 1 byte:

```
char *f = (char *)malloc(str1_length + n);
```

also take a look at calloc(3), malloc(3), free(3) (same man actually  )


----------



## Business_Woman (Jun 15, 2010)

Thank you


----------



## Alt (Jun 15, 2010)

Better version is

```
char *f = (char *)malloc(str1_length + n);        
strncpy(f, str1, str1_length);
strncpy(&f[str1_length], str2, n);
```
1st strncpy prevents your `f` from overflowing. afaik using strncpy is always more secure than strcpy.. Plus, this code easier and should be faster


----------



## darkshadow (Jun 15, 2010)

*why not to use this*

why not to use this http://www.cplusplus.com/reference/clibrary/cstring/strcat/


----------



## expl (Jun 15, 2010)

For concatenation of several strings/integers/what-ever I use libc's asprintf():


```
char *ptr = NULL, s1[] = "cows", s2[] = "milk";
int liters = 5;

asprintf(&ptr, "%s gave %d liters of %s.", s1, liters, s2);
assert(ptr != NULL);

printf("STRING: '%s'\n", ptr);
free(ptr);
```


----------



## fairy (Jun 15, 2010)

killasmurf86 said:
			
		

> you have memory leek
> 
> ```
> int main(){
> ...


Doesn't FreeBSD free allocated memory at the completion of program? Smth like exit(3) does to open streams.


----------



## graudeejs (Jun 15, 2010)

fairy said:
			
		

> Doesn't FreeBSD free allocated memory at the completion of program? Smth like exit(3) does to open streams.



But, what if your program runs for years without exiting, and keeps concatenating strings? [you get my point]


----------



## Alt (Jun 15, 2010)

killasmurf86 said:
			
		

> But, what if your program runs for years without exiting, and keeps concatenating strings? [you get my point]


I belive such programs cant run for years :e


----------



## fairy (Jun 16, 2010)

killasmurf86 said:
			
		

> But, what if your program runs for years without exiting, and keeps concatenating strings? [you get my point]


Then it will *never* reach free() statement either. Stretching my point ad absurdum isn't substitute for an answer.


----------



## kpedersen (Jun 16, 2010)

Hmm, to make a small concatinating program run for years, I would put it in a loop.

To make sure it does not memory leak for all those years, I would put the free() in the loop also.

Wouldn't do much useful, but at least it wouldn't leak


----------



## expl (Jun 16, 2010)

Its not about just how practical are the deallocations, its about coding style (correctness of it). It does not matter if your code will just be ran once in entire program's life, it is good coding practice to always deallocate memory after you are done using it. After you get used to deallocate your mallocs/news every time, there will be small chance of big memory leaks appearing in your more vital code segments when you forget to do so.


----------



## Alt (Jun 16, 2010)

Agree with expl. Clean and correct code is important step on road to serious projects.
Its not essential which size program, correct memory handling must be a standard for any programmer.


----------



## varnie (Jun 18, 2010)

memory leak is a serious *BUG*. and that's all. please take it into consideration. thanks


----------



## psycho (Jun 18, 2010)

i'm just curious about this code.
Line 5.

```
char *stringcat
```
   Is that function? Why declaring this pointer?


----------



## expl (Jun 18, 2010)

line 5 is declaration of the function. It might be confusing because Business_Woman's separation of codeblocks is terrible.


----------



## SirDice (Jun 18, 2010)

psycho said:
			
		

> i'm just curious about this code.
> Line 5.
> char *stringcat
> Is that function? Why declaring this pointer?



It declares the return of that function. In this case the function returns a pointer to a character array.


----------



## Business_Woman (Jun 18, 2010)

expl said:
			
		

> line 5 is declaration of the function. It might be confusing because Business_Woman's separation of codeblocks is terrible.


Really?

How would you have written it?


----------



## expl (Jun 18, 2010)

Business_Woman said:
			
		

> Really?
> 
> How would you have written it?



http://pastebin.com/mxthsWZG

same code just with correct separation


----------

