0

func1 is giving warning & junk value while func2 is giving the right output. What's the difference between the two?

//func 1
unsigned char *CreateData()
{
unsigned char data[]="Thanks";
return data;
}

/* func 2
unsigned char *CreateData()
{
unsigned char *data=malloc(6);
strncpy(data,"Thanks",strlen("Thanks"));
return data;
}


int main()
{
unsigned char *a;
a=CreateData();
printf("%s",a);
return 0;
}

Thanks :)

6
  • 1
    In func1 you are creating a variable called data o nthe stack and it is destroyed when the function returns. You may NOT return pointers or arrays from a function if you create them on the stack - you HAVE to use malloc. Commented Apr 16, 2014 at 4:32
  • @JerryJeremiah Thanks. And malloc takes memory from heap. Therefore that persists for the entire program. right? Commented Apr 16, 2014 at 4:36
  • Until the program ends or you call free for the pointer. Commented Apr 16, 2014 at 4:38
  • You're buffer overflowing here. "Thanks" is 6 characters long, but C-style strings are null-terminated meaning the null character is being written somewhere outside the memory you allocated for it. Commented Apr 16, 2014 at 4:48
  • 1
    @GriffinG: No, the null character isn't being written at all, because the code uses strncpy() rather than strcpy(). See my rant on strncpy(). Commented Apr 16, 2014 at 5:36

3 Answers 3

4

Using the first implementation of CreateData, you return a pointer to a variable with automatic storage duration and then use it past its lifetime, which is undefined behavior.

Less formally, what's actually happening is that data is allocated on the stack, and once you get around to using it as a, CreateData has ended and that stack space is now available for other functions to use, like main or printf, and those other functions are trampling over the space that was previously reserved for data.

When you use malloc, though, the memory is said to be allocated on the heap, not on the stack, and memory on the heap is only released when you tell it to be released (using free). Unlike with variables with automatic storage duration, the memory will not be released when CreateData has returned, so you can continue to use that memory in main.

Sign up to request clarification or add additional context in comments.

Comments

0

The static array ( ex. unsigned char data[]="Thanks"; ) will be destroyed as soon as you leave the current stack frame (basically, when the function you're in returns).

The dynamic array ( ex. unsigned char *data=malloc(6); ) sticks around forever until you free() it. It's lives on the heap.

Comments

0

Function 1 returns a pointer to the local variable (allocated on stack). This is an undefined behaviour.

In Function 2, the data block is allocated on the heap, hence its value persists out of the scope of the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.