1

I am reading about the realloc function, i.e.

void *realloc(void *ptr, size_t size);

and the author of the textbook I'm reading (K.N. King "C Programming a Modern Approach", 2nd edition, p.421, last paragraph), at some point writes

Although realloc doesn't require that ptr point to memory that's being used as an array, in practice it usually does.

My apologies if this is trivial, but I'm confused. My obvious question is: how? Unfortunately, the author doesn't go any further on this.

Can you provide an expanded explanation, perhaps using a simple example?

utobi
  • 279
  • 8
  • 16
  • 2
    ```realloc(NULL, 20)``` is the same as ```malloc(20)``` – Daniel Botnik Dec 31 '22 at 15:00
  • 4
    It simply means that it can also be used as a single element of some type. But normally the memory is used as an array and `realloc` is called to increase or decrease the number of elements in that array. – Gerhardh Dec 31 '22 at 15:02
  • 2
    Ask the author as it hard to understand what he has in mind. – 0___________ Dec 31 '22 at 15:05
  • 2
    Whenever you quote something, and especially when you are asking about it, give a proper [bibliographic citation](https://en.wikipedia.org/wiki/Citation). Title, author, date of publication, identification of where the quote is in the document (page, chapter, clause, section, whatever), and, if available, a URL. – Eric Postpischil Dec 31 '22 at 15:07
  • thank you all for your comments! @0___________ is the first thing I thought, but I wanted to make sure it was not a trivial point. – utobi Dec 31 '22 at 15:08
  • 1
    The author would have done better to just say nothing. It does not need saying, you will use realloc for whatever your application demands, there is nothing useful in pointing out that it will typically be an array. That is just is in the nature of _"things you might want to resize"_. – Clifford Dec 31 '22 at 15:48
  • 1
    Some caution with your sources. That book was published in 2008 and covers C89 and C99, so not quite that "modern". Programming books that describe their content as "modern" clearly have no sense of computing history and have a self imposed "use-by" date. I have been doing this long enough to remember when the "modern approach" to C programming was called C++ ;-) – Clifford Dec 31 '22 at 16:29
  • @Clifford thank you so much for the feedback! Although this is quite old now, I find it enjoyable to read; an oldie but goodie. I do have some updated C books on my bookshelf waiting to be processed :-). – utobi Dec 31 '22 at 16:41

2 Answers2

4

The “it” in “in practice it usually does” means ptr. The clause is saying that we usually use realloc with memory that is being used as an array.

The most common use of realloc is to reallocate space for an array: We initially allocate some memory, process input, find that processing the input requires a larger array, and so use realloc to increase the space. This is not a requirement of realloc; it is the nature of how it is used.

It is not the only possible use. Another is we might have some set of related structures, such as struct Square, struct Circle, struct Polygon, and so on, all with some common initial element, struct Shape. We might initially allocate space for struct Shape so that we can do some initial processing of the shape before we know what shape it will be. Later, when the specific shape is done, we might use realloc to grow the space from that needed for struct Shape to that needed for struct Square.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • @Thomas: I interpreted “it” to mean `realloc`. Given that, the latter part of the sentence is false; `realloc` does not in practice require the pointer to point to memory that is being used as an array. However, I now see the author intended “it” to mean “the pointer.” I will update the answer. – Eric Postpischil Dec 31 '22 at 15:40
2

He is merely (and probably unnecessarily) pointing out that typically if you are resizing an allocation, it will be for storing a different number of objects of the same type (i.e. an array). It really does not need saying.

Situations where that might not be the case may occur, but are "unusual". For example you might implement a string "class" using a structure such as:

typedef struct
{
    size_t length ;
    size_t capacity ;
    char strdata[1] ; // gcc allows [0] here by extension
} string_t ;

Now you can dynamically allocate or reallocate a single object of type string_t to accommodate a string of variable length using a size sizeof(string_t) + string_capacity. By virtue of being at the end of the structure, you can "overrun" the strdata array into the dynamically allocated space.

It is perhaps all a bit "tricksy" and anyone doing that has probably long passed needing realloc() explaining, so it might have been better for the author to have said nothing, since the typical usage is also the obvious usage, and it would probably never occur to a novice to use it any other way.

Clifford
  • 88,407
  • 13
  • 85
  • 165