0

Here is my code, when i return "fo", the result is "0x7fffffffd870 "fo" ", my question is how to make it return "fo"?

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

char *substr(char *s, int from, int to) {
    int n = to - from + 1;
    char subs[n];
    strncpy(subs, s + from, n);
    return subs;
}

int main(int argc, char **argv) {
    char *s = substr("foo", 0, 1);
    puts(s);
    return (0);
};

update, here is correct code, but I don't know what's diff between char subs[n] and char* subs=malloc(n)

char *substr(char *s, int from, int to) {
    int n = to - from + 1;
    char *subs = malloc(n);
    strncpy(subs, s + from, n);
    return subs;
}

int main(int argc, char **argv) {
    char *s = substr("foo", 0, 1);
    puts(s);
    return (0);
};
nwaicaethi
  • 175
  • 3
  • 10
  • 1
    `subs` is an automatic variable. It goes out of scope when the function exits and returning such a variable is Undefined Behaviour. Your main options are either to pass in a buffer into the `substr` function or have the `substr` function allocate dynamic memory and return that to the caller. – kaylum Nov 15 '15 at 10:21

3 Answers3

1

update, here is correct code, but I don't know what's diff between char subs[n] and char* subs=malloc(n)

Difference is that char subs[n] is a local array , and is allocated on stack . Its lifetime is until the function substr terminates . Outside function block this array can't be accessed .

But when you allocate memory to char *subs , it is allocated on heap and it points to memory block allocated by malloc even after your function substr terminates. But you need to free this memory in calling function.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

char subs[n] is a static string (string intended as array of chars): the dimension is fixed do n and cannot change. char* subs = malloc(n) is a dynamic string: its dimension is dynamic and can change by reallocating the memory (with realloc for example) or can me totally deallocated with free(subs).

So the difference stands in how you store your string in memory:

  • You allocate a static amount of memory (in the STACK) -> char subs[n]
  • You allocate a dynamic amount of memory that can change (in the HEAP) -> char* subs =malloc(n)
magicleon94
  • 4,887
  • 2
  • 24
  • 53
0

when every program starts executing it allocates memory on stack.

In first program subs char array is automatic variable and memory is allocated on stack. once function call is completed it deletes its stack memory. At last of your function call you are returning address of array where that address had garbage value after end of function call which results in dangling pointer(i.e you are trying to access memory where it has nothing).

To over come this situation you need to allocate heap memory which can be done using malloc in c. Once it created memory on heap,program doesn't delete it automatically you need to delete it by using free keyword if not it results in overflow. Another way to overcome this is,create global variable where it's scope is through out the program and you can access it where ever you want.