davidktw
Arch-Supremacy Member
- Joined
- Apr 15, 2010
- Messages
- 13,547
- Reaction score
- 1,300
because my assignment got extra weight,i need to calculate the normal of the face.
vertex* vertex_array;
Face * face_array;
these are array i thought of
I am looking back at my java assignment 3 years ago:
somewhere along the line:
static int custCount = 0;
static Customer customers[] = new Customer[custCount];
it has code that can copy an array to another array
this was done before I did the actually proceed to do the linkedlist example
if this can be done I will be glad.
currently i am looking at this:
realloc - C++ Reference
update but strangely,when I do a vertex * array i check the size it is 4,
all i did is simply : vertex_array = (struct vertex*)malloc(0);
Like I have mentioned, discard away your Java semantics in C. Most of them will not work because C doesn't have all these features in the language. If you keep on locking yourself in what Java has offered and you are looking for equivalence in C, you will never be able to do C.
Realloc is to either expand the size of the allocated memory if it can be done, or copy the contents of existing memory block into a larger and return you a new pointer to new memory block and dealloc the previous one.
You are not encourage to use realloc as the means of expanding memory size since this is a very costly process and there are better data structures out there to serve your needs than to keep on resize your memory block.
Code:
vertex *array;
"array" is a pointer to a "vertex" type. As such, it is a variable that will contain memory addresses. 4 bytes = 32bits is correct on a 32bits arch system.
Unless you use the name of the array, then the compiler will resolve the 'sizeof' the name of the array as the size of the number of elements * sizeof(element type)
'sizeof' is not a runtime function, it is a compile type operator upon the type.
As such, if you are using dynamic memory allocation, 'sizeof' operator cannot resolve the size of the dynamic array, because it doesn't have any information of the size of the array before it's executed. At the same time, there is no compile time information about a dynamic memory allocation for 'sizeof' operator to work properly.
Code:
int *j;
j = malloc(10 * sizeof(int));
printf("%lu\n", sizeof(j));
Take note that while it is possible to address each element of the j pointer as such
Code:
printf("nth element value = %d\n", j[n]);
printf("nth element value = %d\n", *(j + n));
It actual fact, that doesn't make 'j' an array. It is just what you think. The square bracket notations are just sugar syntax for pointer arithmetic. They give programmer the idea of an array addressing technique, but in actual fact, it's nothing more than pointer addressing technique.
The 'sizeof' j is the size of the pointer, which is 4 bytes in 32bits system and and 8 bytes in 64bits system.
However the sizeof(*j) is most likely always 4bytes unless the C compiler choose otherwise for 'int' datatype to be of some other size.
sizeof(*j) in english is "size of where j is pointing to". Since 'j' is pointing to a 32bits integer, therefore the size is known at compile time to be 4bytes.