1

Below is the snippet of the code:

int main(void) {

    char sizing[] = "manshcanshcnams cndhan sndhcna snshans";
    char *xyz = malloc(sizeof(char));
    printf("%ld\n",sizeof(xyz));
    xyz = sizing;    // IT SHOULD FAIL HERE
    printf("Fail %s\n",xyz );
    return 0;

}

As you can see that I am trying to assign more memory to xyz then what it can hold. But the output doesn't fail. Is it an undefined behaviour?

SandBag_1996
  • 1,570
  • 3
  • 20
  • 50

3 Answers3

3

You can't copy strings with =. xyz = sizing just modifies the variable xyz so that it points to the array sizing, instead of pointing to the memory you malloced.

So, your memory is leaked but there is no undefined behavior (except that you forgot to include <stdio.h> and <stdlib.h>).

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • can we xyz hold memory then what it is allocated for? – SandBag_1996 Mar 08 '13 at 00:20
  • `xyz` doesn't "hold memory". It's a pointer. It points to memory. – Steve Jessop Mar 08 '13 at 00:22
  • @UnderDog try using your code replacing `char *xyz = malloc(sizeof(char));` with `char *xyz;`. –  Mar 08 '13 at 00:24
  • @UnderDog: Consider reading http://stackoverflow.com/a/5754/13005. It's long but it might correct your understanding. – Steve Jessop Mar 08 '13 at 00:24
  • so suppose when we create a link list, and assign some space to it. When will it fail, or will the link list continue to grow? I am just trying to understand the cases where malloc should fail – SandBag_1996 Mar 08 '13 at 00:25
  • @UnderDog The code in your question has nothing to do with `malloc()` failing. (`malloc()` could fail, I guess, when the address space or physical memory+swap area are exhausted.) It also seems like you have a fundamental misunderstanding of how C works, and I'm not sure SO is the right forum where to address that. – millimoose Mar 08 '13 at 00:49
  • Perhaps you could update your answer to show how to allocate the correct amount of memory and then copy a string with `strncpy`, and perhaps suggest using the `strdup` function instead. – paddy Mar 08 '13 at 00:50
  • @paddy: I don't think I could write any useful code along those lines without making assumptions that aren't supported by the code we can see. There's no evidence in the questioner's code of any need to take a copy, I don't want to introduce one just to show the technique. If I did then I would not use `strncpy`, though. – Steve Jessop Mar 08 '13 at 21:01
  • @SteveJessop So in that case there's no need to begin your answer with "you can't copy strings with `=`"... We both saw what the author intended. I just suggested you add something to show one or more correct ways to copy strings. – paddy Mar 09 '13 at 07:18
  • @paddy: I can see that the questioner is *trying* to take a copy, I don't see whether or not there is any *need* to do so. Pointer assignment might be the right thing to do, in which case the problem is just that the questioner thinks that it's "assigning memory". – Steve Jessop Mar 09 '13 at 13:42
1

All you're doing is telling the pointer xyz to point at the memory address associated with sizing. There is no reason that this would fail.

Daedalus
  • 1,667
  • 10
  • 12
-1

My understanding is that malloc() should not fail unless it cannot allocate the amount of memory it was asked for. However, when you use it, you are required to release that memory when you don't need it anymore or else your program will request more and more memory and eventually the request will fail because you haven't released anything.

Also, not all illegal memory operations cause an error, for example (code edited 2013-03-08 based on Kludas's observation that the previous version wouldn't compile):

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

int main(void)
{
    char myString[4];
    strcpy(myString, "abcdefghijklmnopqrstuvwxyz");  /* This overflows the buffer */
                                                     /* but MIGHT NOT cause an    */
                                                     /* immediate crash!          */
    printf("myString = [%s]\n", myString);
    return 0;
}

This may-or-may-not trigger an error, but it is certainly illegal because I am writing 26 characters into an array that should only hold 4 items (three characters plus a terminating NULL).

neilr8133
  • 142
  • 6
  • This won't even compile. You're trying to assign a pointer to a string literal to an array. – Kludas Mar 08 '13 at 05:12