-5

Does strncpy() leads to memory leak when we're copying less number of data to a larger (compile-time allocated) buffer? In other words, can the under-utilization of memory be termed as memory leak?

Below is my code

#define uk "ln"

int main()

{
  char buffer[32];
  strncpy(buffer,uk,sizeof(buffer));// IS it memory leak free?
                                    // uk macro has 3 byte of size to
                                    // hold the string but here the
                                    // Attemp of 32 byte is made to copy
                                    // from uk macro to buffer?
}  

Is there any memory leak or bug in above code?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
user4950013
  • 137
  • 5
  • 4
    This is more a code review request than a question. You should rephrase it to avoid downvotes. – ereOn May 28 '15 at 16:43
  • @ereOn I tried my best. Please verify the correctness. – Sourav Ghosh May 28 '15 at 16:53
  • The problem with `strncpy` is when the source is *not* shorter than the destination. In that case, the copied string will not be NUL-terminated, so using the copy could lead to a buffer overrun. Code which calls `strncpy` should explicitly null-terminate the copy. – rici May 28 '15 at 16:56

3 Answers3

7

memory leak?

No, there is no memory leak. Period.

If you don't allocate memory yourself (dynamically), you don't need to free it either. All the memory which is allocated by the compiler at the compilation time, compiler will take care for releasing those memories. We can have fun, now.

Bug?

Yes, you did not include string.h which contains the forward declaration for strncpy().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
5

In fact these two statements

char buffer[32];
strncpy(buffer,uk,sizeof(buffer));

are fully equivalent to the following declaration

char buffer[32] = "ln";

It is a valid declaration and there is no bug or memory leak.:)

In the both cases all elements of buffer that were not initialized by the string literal's characters (or by copying of its characters) are zero initialized.

From the C Standard (7.23.2.4 The strncpy function)

3 If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.

In general it would be more correctly to write

char buffer[32];

strncpy(buffer,uk,sizeof(buffer));
buffer[sizeof( buffer )-1] = '\0';

That is your original code is unsafe.

Of course header <string.h> in C or <cstring> in C++ has to be included but I think you simply forgot to do this for the demonstrative example.:).

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • is it good practice to write (strncpy(buffer,uk,sizeof(buffer)) ; i am confuse by thinking why not to save stack memory when one can .I do agree that compiler will release but why not to take care when one can save stack memory as stack memory is also limited ? – user4950013 May 28 '15 at 17:00
  • @user4950013 You already allocated memory when you declared buffer. Function strncpy has nothing common with memory allocation or reallocation – Vlad from Moscow May 28 '15 at 17:02
4

There is no memory leak because buffer is an automatic variable. It ceases to exist upon exiting main().

juanchopanza
  • 223,364
  • 34
  • 402
  • 480