Yes, C++ vectors do automatic resizing. From what I can tell, it isn't required that they double on each resize (It looks like it could just allocate 100 elements at a time, for example).
some lines in the code needs to be user friendly.................but still the explanation was superb............
Avoid this idiom. Use a temporary.
Can you explain why I should use a temporary?
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
If the realloc fails, you just lost your handle on the data.
Great tutorial.
But what's with the goto? You can simply use:
Right in the spot.Code:else { free(data); printf("Not enough memory!\n"); return 1; }
And maybe you could add calloc(), though it's the same as malloc().
For some reason, I was probably thinking you had to return values at the end of the program. It is changed now.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
No, that is an example of a declaration for a pointer variable. The definition of pointer is not "int *array;".You can define a pointer like so
The pointer points to a random location at firstAccording to the standard, a pointer variable contains an indeterminate value before it is initialised, and after free() is called with it's pointer value as an argument. Your debugger might set it to 0xbadc0de, for example. Using an indeterminate value such as this in any way (including a pointer comparison) is enough to invoke undefined behaviour according to the standard.A pointer does always points to something (or has the value NULL).
As previously stated, malloc accepts an argument that shall be the size of the allocation that the returned void pointer points to, providing the allocation is successful. malloc() does not "take an argument of how many bytes to give the pointer". The size in bytes of a void * pointer is constant when considering a specific implementation.malloc() returns a void pointer and takes an argument of how many bytes to give to the pointer.
I disagree. int array[2]; This is an example of using automemory. Automemory is valid only for it's scope. The memory allocated and pointed to by the return value of malloc() is valid up until it is deallocated using free().Using malloc like the above is very similar to doing this
realloc() may lead to code which leaves stale pointers. Using those stale pointers (even in a simple pointer comparison) invokes undefined behaviour according to the C standard.Now I will show you how to use realloc().
malloc() and realloc() are not operations that result in or operate on arrays. Arrays decay to pointers during runtime. Calling realloc() on an array would invoke undefined behaviour, and malloc()/realloc() return pointers that point to an allocated sequence of bytes. Here's an example that displays an obvious difference between a pointer and an array:You use realloc after you have used malloc to give the array more or less memory.
Code:int array[100]; int *pointer = malloc(100); printf("sizeof(array) = %zu\n", sizeof(array)); printf("sizeof(pointer) = %zu\n", sizeof(pointer));You can only free() pointers that were specifically returned by malloc() or realloc(). free()ing any pointer which was not returned by malloc() or realloc() invokes undefined behaviour. Furthermore, when you are done with a pointer that is returned by malloc() or realloc(), you must deallocate it by calling free() with the pointer as it's argument in order to avoid memory leaks.When you done using a pointer, you can free the memory
This is an example of invoking undefined behaviour.for (i--;i>=0;i--)
putchar(data[i]);
Last edited by n00py; 01-30-2010 at 05:02 PM.
That's what I meant, I'll change it.
That's why I said the location is random. You don't know where it points to unless you initialize it.According to the standard, a pointer variable contains an indeterminate value before it is initialised, and after free() is called with it's pointer value as an argument. Your debugger might set it to 0xbadc0de, for example. Using an indeterminate value such as this in any way (including a pointer comparison) is enough to invoke undefined behaviour according to the standard.
The wording is a little weird there. I'll make it more clear.As previously stated, malloc accepts an argument that shall be the size of the allocation that the returned void pointer points to, providing the allocation is successful. malloc() does not "take an argument of how many bytes to give the pointer". The size in bytes of a void * pointer is constant when considering a specific implementation.
I said it's similar, not exactly the same.I disagree. int array[2]; This is an example of using automemory. Automemory is valid only for it's scope. The memory allocated and pointed to by the return value of malloc() is valid up until it is deallocated using free().
I don't know what a stale pointer is.realloc() may lead to code which leaves stale pointers. Using those stale pointers (even in a simple pointer comparison) invokes undefined behaviour according to the C standard.
Just problems with the wording I can easily fix.malloc() and realloc() are not operations that result in or operate on arrays. Arrays decay to pointers during runtime. Calling realloc() on an array would invoke undefined behaviour, and malloc()/realloc() return pointers that point to an allocated sequence of bytes. Here's an example that displays an obvious difference between a pointer and an array:
Code:int array[100]; int *pointer = malloc(100); printf("sizeof(array) = %zu\n", sizeof(array)); printf("sizeof(pointer) = %zu\n", sizeof(pointer));
I'm aware of this, again it's something I should probably have clarified.You can only free() pointers that were specifically returned by malloc() or realloc(). free()ing any pointer which was not returned by malloc() or realloc() invokes undefined behaviour. Furthermore, when you are done with a pointer that is returned by malloc() or realloc(), you must deallocate it by calling free() with the pointer as it's argument in order to avoid memory leaks.
How? I already know variable i is the index at the end of the allocated memory, and I'm just going backwards until i is less than zero.This is an example of invoking undefined behaviour.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
There are currently 4 users browsing this thread. (0 members and 4 guests)
Bookmarks