Coming from Python, if C does not have array bounds, how does it know where a[1] starts?
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
b = a[1][1];
arrayc
Coming from Python, if C does not have array bounds, how does it know where a[1] starts?
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
b = a[1][1];
The Java Collections framework is something that you should browse. There are also some types that aren't array-like (Set and Map) but you will find that you will use them often. There are many different types of collections, and some have advantages over others for certain types of operations.
First, the thing that isn't in the collections framework. The Array. Not ArrayList, but rather the classic int[]
. The advantage of the array is that it closely maps to what memory is. foo[42]
is to lookup. Many system level calls that give you a fixed number of items are arrays and they are good to know. "1,2,3".split(",")
returns back an array for example.
The problem with arrays is that they aren't that flexible. Adding an element to the end of an array that already is at is maximum capacity means allocating a new, larger array and then doing an System.arraycopy()
(javadoc) to copy all the elements from one array to another array in a timely manner.
To remove an element from the list you need to remove it and then walk the remainder of the list to have everything be contagious again. If you want to remove foo[4]
from the list, you then assign foo[4] = foo[5]
and foo[5] = foo[6]
and so on down to the end of the list. Thats if you have to do it by hand. You can also use that System.arraycopy
mentioned to do it for you given the right set of arguments. Something like:
System.arraycopy(foo,5,foo,4,foo.length - 4);
I might have that wrong, but thats the general idea. Copying the array back on itself. It should work.
If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.
As you see, arrays are a bit awkward to work with. You've got do it all by hand. Its good to know, but a pain to practice. There are two lists that are in common use that make this easier for you. For the most part, Lists look like arrays.
List<Integer> foo = ...;
foo.get(4);
foo.remove(5);
You call methods on them, but you are fetching the 4th element via method call instead of via direct access through the array. See that foo.remove(5)
. Thats it. You're done. You have removed an element from the list and deleted it. Much easier to remember.
The ArrayList is backed by an Array. This means that doing foo.get(4)
runs fast, but foo.add(42, foo.size()+1)
is likely to be slow because it has to do all of that moving of things around to allocate a new list (yes, I know that the list can be longer than the size, but lets assume that its the same size as its actual list).
Instead of having to remember how to do all the shuffling, you've got nice methods to do your work for you. It doesn't mean that its not done (the work is still done behind the scenes of the interface), but its something that you don't have to worry about.
So, the ArrayList is for:
For some reason, everyone's goto list implementation is the ArrayList. I don't know why. Many times when I'm working with Lists, the LinkedList is the one that is more applicable. You keep adding things to the end. Rarely do you want the nth element. You just want to iterate over them.
This is what the LinkedList is good for. It does fast remove and append and its iterator is just as fast as the ArrayList. Its just if you want foo.get(400)
its going to be a bit slower than the ArrayList implement.
The LinkedList is also a Deque too, but people forget about that. That lets you access the LinkedList as a double ended queue (nice for implementing a Stack or Queue).
So, if you are just appending and iterating, the LinkedList is for you.
Note that the O(index) there is a bit of an award bit. It will index depending on which way is faster to get there (from the end or start). So if the LinkedList is 100 long, and you get(10), it starts from the start. If you ask for get(90), it starts from the end. So get(index) is never more than get(length/2), but thats all just factors that don't matter when looking at Big O.
"Optimized out" means the compiler realises the variable isn't necessary, and deletes the variable. For example, this code:
void func(void)
{
int x = 0;
int y = 3724832+x;
printf("%d\n", y);
}
might be compiled the same way as:
void func(void)
{
printf("%d\n", 3724832);
}
or even (if your compiler is smart enough)
void func(void)
{
puts("3724832");
}
and we would say that the variables x
and y
have been optimized out, because the optimizer has removed them from the program.
"Optimized out" does not mean that the variable gets destroyed when the function returns. All non-static
local variables get destroyed when the function returns. No exceptions. This includes arrays.
In your function:
void func(void) { uint8_t tempBuffer[2] = {0x00, 0x01}; sendBufferOverUSB(tempBuffer, 2); }
the variable tempBuffer
will be destroyed when the function returns. If you try to trick the compiler so you can use tempBuffer
after func
returns - for example, using a pointer - you might find that the memory that used to contain this variable has been overwritten, and it no longer contains {0x00, 0x01}
. You might even get a crash when you try to use it. This isn't because of optimization.
Best Answer
C knows the bound of an array at compile time, as opposed to Python which knows the bound of an array at runtime.
The compiler basically rewrite array access into pointer operations. It knows at compile time that the size of a the first dimension of the array is
int[3]
, i.e.sizeof(int)*3
. So:Is basically rewritten to
So the knowledge about the dimension (the number 3) is encoded into the code, even if the dimensions are not explicitly available at runtime. Arguably, arrays are purely a compile-time construct in C.