If you needed help understanding it I probably could have helped you
It's good that you figured it out though.
Accually i have read this tutorial over 30 times, And finally, now i get realloc![]()
If you needed help understanding it I probably could have helped you
It's good that you figured it out though.
Allocation and deallocation of memory to store the pointer value is automatic, because it's automatic memory. If a pointer is assigned to the return value of malloc(x), the pointer value is stored in automatic memory, but the memory that the pointer points to is dynamic. You've written a comment in your code later on that says something like "free the pointer"..........
If this example follows on to the next one, then I'd suggest that the identifier used should be "ptr", in order to avoid confusion.
There is no variable declaration for "array". I'm assuming you meant to carry on from the previous examples, which means "array" should in fact be "pointer". When using realloc, it's a common practise to set "pointer" to NULL after free()ing it, so that subsequent calls to realloc don't invoke undefined behaviour (by trying to use a pointer that has been free()d).
Aside from that, looks much better. I like how K&R's "The C Programming Language" worded it, however. That worked well for meIn regards to dcs' request, I believe it's necessary to explain pointers before dealing with malloc. Here's an extraction I've taken from K&R 2nd edition (page 93):
"Let us begin with a simplified picture of how memory is organized. A typical machine has an array of consecutively numbered or addressed memory cells that may be manipulated individually or in contiguous groups. One common situation is that any byte can be a char, ..." (in fact the C99 standard requires that the words "byte" and "char" be synonymous - 1 char is always 1 byte) "... a pair of one-byte cells can be treated as a short integer, and four adjacent bytes form a long. A pointer is a group of cells (often two or four) that can hold an address. ..." (it is worth noting the emphasis on the word common because a short integer isn't required to be 2 bytes, a long isn't required to be 4 bytes and a pointer ... well you get the picture) "... So if c is a char and p is a pointer that points into it, we could represent the situation in this way:"
(ok, so my diagram is pathetic... get the book!)Code:...+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+... | p | p | p | p | | | | | c | | | | | | | | ...+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+... |_________________________^
"The unary operator & gives the address of an object, so the statement p = &c; assigns the address of c to the variable p, and p is said to point to c. ..."
"The unary operator * ... accesses the object the pointer points to. ..." (Given the statement p = &c; the variable c could be assigned using the pointer p like: *p = x;. or retrieved using the pointer p like: d = *p;)
The book then goes into detail regarding pointer arithmetic (which is good fun), but it is necessary to know that a pointer to foo type is different to an array of foo type. In summary, the differences are:
- Pointer variables store pointer values. According to K&R: A pointer is a group of cells (often two or four) that can hold an address..
- The sizeof() operator used on an array will result in the number of bytes that the items within the array occupy. The sizeof() operator used on a pointer will result in the number of bytes that the pointer value occupies. On a side note, in many cases a pointer to foo is not required to be the same size as a pointer to bar. See the C99 standard draft for more information.
- Arrays are automatically managed. They are destroyed when their scope runs out. Pointer variables store pointer values. Pointer values point to objects. Arrays degrade to pointer values during runtime. A pointer variable is automatically managed, though the memory that a pointer value points to isn't (unless the pointer value points to automatic memory).
Back to K&R, page 252:
void *malloc(size_t size);
malloc returns a pointer to space for an object of size bytes, or NULL if the request cannot be satisfied. The space is uninitialized.
The K&R description of realloc is not so great, so I've decided to use the opengroup realloc page as a reference for realloc. At this moment in time (which is important because the proposed new C1X standard may change some things), the page states:
void *realloc(void *ptr, size_t size);
The realloc() function shall change the size of the memory object pointed to by ptr to the size specified by size. The contents of the object shall remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, the contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object shall remain unchanged. (and NULL will be returned)
If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size.
If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.
Last edited by n00py; 03-10-2010 at 09:19 PM.
@n00py: I changed the variable names to prevent confusion. As for the pointer stuff, I have a little note at the beginning that links to the pointer tutorial I did with ZekeDragon.
Just now I read your article on pointers. I didn't notice it at first, because the links aren't made obvious on this forum. Perhaps it might be a good idea to give it an underline, and highlight the link (as I should)... I believe your pointer tutorial needs slight modification. It's good, but arrays aren't pointers. There are differences, and the ones that come to mind are described by the points in my previous post. Perhaps it would be best if you worded it like: "Arrays decay to pointer values during runtime."
i just read the post, it's ok, but you have do give more detail, an i think you have an mistake "for (i--;i>=0;i--)".. i don't understand this statement, and furthermore "i" is a local variable in the for, an when the for exits the value of "i" is not holded... Good job anyway, + rep. P.S. sorry for my english but i am romanian
What are you talking about? Of course 'i' keeps it's value after the for loop. As long as it's in the same function, 'i' will not change it's value.
Just to prove my point, running this code will print out 10:
Code:#include <stdio.h> int main(void) { int i; for (i=0;i<10;i++); printf("%d\n", i); return 0; }
pff... sorry my bad...i thought i was wright...
There are currently 1 users browsing this thread. (0 members and 1 guests)