-1

I am new to C language, and the tutorial is here.

I follow the tutorial, but it will not give an error when i try to not realloc memory,

the results are same whether comments the realloc code or not.

I want to know why? Can someone explain this? Thanks

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

int main()
{
    char name[100];
    char *description;

    strcpy(name, "Zara Ali");

    description = (char *) malloc(30 * sizeof(char));
    if (description == NULL)
    {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    }
    else
    {
        strcpy(description, "Zara li a DPS student.");
    }
//    description = (char *) realloc(description, 100 * sizeof(char));
    if (description == NULL)
    {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    }
    else
    {
        strcat(description, "She is in class 10th.");
    }
    printf("Name = %s\n", name);
    printf("Description: %s\n", description);

    free(description);
}

result: picture

  • Accessing *inexisting* elements is **Undefined Behaviour**. Anything can happen. *Working as expected* is good. *Crash* is good. *Transfer the money in your bank account to mine* is good. *Printing primes to 1000 on Wednesday* is good ... So "Why?" **Because with UB everything is good.** You want to avoid UB at all costs in your programs. – pmg Sep 07 '21 at 08:15
  • By dumping > 30 characters (including the terminator) into a buffer only sized to 30 characters of occupancy, your program invokes undefined behavior. Thus, the simple answer is *what you see happening is perfectly reasonable in the realm of chaos*. Consider yourself *unlucky* you experienced no times, like seemingly "working" for you, but failing miserably on your instructor's test rig, or worse, a paying customer's critical operating environment. A crash now would have been far more fortunate. – WhozCraig Sep 07 '21 at 08:19
  • Perhaps more helpful: [What is undefined behavior and how does it work?](https://software.codidact.com/posts/277486) – Lundin Sep 07 '21 at 08:20
  • And on this site: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – WhozCraig Sep 07 '21 at 08:21
  • @WhozCraig Which is not particularly good IMO and also for C++. – Lundin Sep 07 '21 at 08:22
  • Thanks guys,first know the Terminology Undefined Behaviour, i think i get the reason now. – eyebrowkang Sep 07 '21 at 08:40
  • What else did you expect? What do you mean by "error trigger"? How should a "error trigger" look like? – 12431234123412341234123 Sep 07 '21 at 08:49
  • i thought it would not show the complete string after strcat, or just throw an error in terminal – eyebrowkang Sep 07 '21 at 09:22

2 Answers2

0

From a C reference on strcat:

The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character. The behavior is undefined if the strings overlap. The behavior is undefined if either dest or src is not a pointer to a null-terminated byte string.

Undefined behaviour does not mean there is an error, it means that the rules of C don't apply to your program. You observe all your text being written to the terminal in this case, because you are unlucky enough to have a subtle symptom here.

A conforming C implementation could emit an error message instead of a program, given your code. Or it could emit a program that formats you hard drive, or a program that crashes at runtime, or prints garbage. Any behaviour is permitted.

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

In your code you initialize description by

description = (char *) malloc(30 * sizeof(char)); this means that the value of description is not NULL.

Your printf statement relies upon the check if (description == NULL) returning true, which it won't as description is still not NULL

realloc will return NULL, thus setting description to NULL if it fails to allocate memory.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35