# Recursive C fcn problems



## segfault (Oct 18, 2011)

Obviously my C skills have been on the shelf too long because i have been seriously looking at this code for about a week now (when I have the time) and STILL cannot find what the heck the problem is.
Can anyone see what I am doing wrong?
Hoping another set of eyes will see some simple mistake I've made.


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

int recursion(char *str, int ntoadd, int nreadsofar);

int main()
{
  char *str;
  int ret=0;
  
  ret = recursion(str, 2, 0);
  printf("final answer is (%d):%s\n", ret, str);
  
  free(str);
  
}


int recursion(char *str, int ntoadd, int nreadsofar)
{
 
  if(nreadsofar ==0)
  {
    str = (char *)malloc(ntoadd);
    memset(str, 'a', ntoadd);
  }
  else
  {
    char *tmp = (char *)malloc(ntoadd + nreadsofar);
    memset(tmp, 'b', ntoadd + nreadsofar);  
    strncpy(tmp,str,ntoadd + nreadsofar);
    free(str);
    str=tmp;
  }
  
  int x;
  for(x=0; x<ntoadd; x++)    
    str[nreadsofar +x] = 'c';
    
  if(nreadsofar +x < 4)
    x = recursion(str, 2,nreadsofar + x);

  
  printf("returning (%d):%s\n", x+nreadsofar, str);
  return x;
  
}
```


The output is this:

```
returning (4):cccc
returning (2):cc
zsh: segmentation fault  ./a.out
```


If I remove the "free(str);" line in main it gets rid of the segfault but still only displays:

```
returning (4):cccc
returning (2):cc
final answer is (2):%
```

Why isn't it returning the proper string of length 4? 
Thanks to anyone taking the time to read this.

.....It seems my account name is segfault for good reason


----------



## Alt (Oct 18, 2011)

free() in main fails because you do new alloc(), which change memory chunk, free()-ing the old chunk. While you doing this, main() function still remember last char *str, which already free'd.



			
				segfault said:
			
		

> ```
> returning (4):cccc
> returning (2):cc
> final answer is (2):%
> ...


While function recurses, it will print recurse-level-2 message before recurse-level-1 because you call sprintf() later than recursing.


----------



## Crivens (Oct 18, 2011)

C is call-by-value.

The pointer you free in main() is never initialised, the random value it contains is passed to the recursion and then the random value is free()'d. That has to crash.

Maybe you want to pass a "char**" to recursion so the value in main can actually be modified by the function? And only one instance of the string exists, even if the handle to it is passed around in the recursion?

And this reminds me to use lint once in a while. Anyone knows a good c++ capable lint in ports?


----------



## segfault (Oct 18, 2011)

Arrrg! of course!
I'm an idiot. Maybe I should stop working on this late at night before bed.
Thank you, I need to be passing a (char **) around through the functions. That should do it.
Thank you both very much for taking the time to read through my ugly code.

Code on!


----------



## Crivens (Oct 18, 2011)

segfault said:
			
		

> Thank you both very much for taking the time to read through my ugly code.


Don't worry mate, you are welcome.


----------

