While writing a rather simple code for an exercise i was assigned, I run into this problem.
The exercise asks me to create a function (what it does it's not important, but it loops through each element of the matrix to do something) getting as parameters a 2D array (NxM) and a double (which is used in the function).
The function so looks something like this:
int function(int (*myarray)[M], double y){}
Now if I'm not missing anything, this is not enough because I can't loop through the elements without using the value N as range. Am I right? If there is no way to use the number of rows inside this function without passing it as a parameter, it means that the exercise took for granted that passing the matrix meant also passing N (rows) as a parameter.
(I thought about using sizeof to get the number of rows somehow, but sizeof(myarray) inside function(){} doesn't work as expected. What is the difference between myarray inside this function and the original myarray I declared in main()?)
myarray is declared as int myarray[N][M]; and then initialized with a loop which contains myarray[i][j] = rand()%(max-min+1)+min;. I don't think this is too relevant to the problem but I include it to explain it is automatically allocated.
I already checked some similar discussions like thisand this but since everyone gives the dimension as parameter I didn't find an answer yet.
Can I use a matrix in a function without specifying ROWS?”
The answer is largely no. The function must have some way of knowing how much data to use from the array. That is not necessarily the original size of the array; it could be the amount of the array, or portions of the array, the caller wants the function to operate on. The C standard does not provide any way to know the original size of an array from a pointer.
That information can be made available to the function in multiple ways: By a parameter, by an external variable, including by a sentinel value.
Now if I'm not missing anything, this is not enough because I can't loop through the elements without using the value N as range. Am I right?
Passing the number of rows in the array is one way of being able to iterate through them. The information could be made available in other ways. In your circumstance, passing the number of rows is likely the intended method.
What is the difference between myarray inside this function and the original myarray I declared in main()?)
When a function parameter is declared as an array, it is automatically adjusted to be a pointer. So, if a parameter were declared as int myarray[N][M], it would be automatically adjusted to int (*myarray)[M]. Then myarray inside the function is not an array; it is a pointer.
When an array is passed to a function as an argument, it is automatically converted to a pointer to its first element. This automatic conversion occurs whenever an array is used in an expression except as the operand of sizeof or the operand of unary & or when it is a string literal that is used to initialize an array.
Related
I was coding in MPI using C. I don't understand how the MPI_Send() works or if maybe &array[element] works.
MPI_Send(&array[element],element_left,MPI_INT,i,0,MPI_COMM_WORLD);
here array[]={1,2,3,4,5,6,7,8,9,10} and element = 6 and element_left = 4. I understand array[element]=array[6]=7 but why this function picks 7,8,9,10? I know it will pick 4 elements from the array but why do we need & here and by only giving starting entry array[6] how is this function able to pick the next 3 as well?
I thought I have to add one after another using a for loop or something, but when I searched something on Google I got this code and after going through so much I still didn't understand. Please help me understand the backwardness of this code.
&array[element] is the same expression as array + element and means the address of the elementth element of the array array.
The function you call wants this address as the first argument, and takes the number of elements to process as the second argument.
Most MPI routines take a trio of arguments:
address of buffer
count of elements
datatype of elements
So by &array[element],element_left,MPI_INT you specify the elements element as the start of the buffer, and then you take element_left many integers to send. Kinda strange that you name the count element_left which is more like a name for an index, but that's what happens.
I'm doing a micro-optimisation of my LRU cache solution in Golang where I'm using https://golang.org/pkg/container/list/. My solution works by having a map[int]*list.Element, where each list.List list.Element is []int, with [0] being key, and [1] being value.
I'm trying to move from []int to [2]int for my optimisation, but I'm then running into the issue that modifying the fixed-size array, after ee := e.Value.([2]int) (note the [2]int type for fixed-size array), is no longer modifying the underlying values in the list.Element, unlike was the case w/ ee := e.Value.([]int) (note the []int type), which I guess makes perfect sense, since slices are based on references, whereas fixed-size arrays are based on copied values.
I've tried stuff like e.Value.([2]int)[1] = …, as well as various combinations with := &e.Value…, and casting from [2]int to []int, but it all results in complier errors.
Q: Is there no way to use the container/list with an embedded array, and perform modifications of said fixed-size array in-place?
As you already noted:
I guess makes perfect sense, since slices are based on references, whereas fixed-size arrays are based on copied values
So if you want to make this work, you'll need to use references to your fixed-size arrays by storing pointers to the arrays instead of array values.
That way, you'll be able to modify the underlying array through the list element.
See here for a simple example:
package main
import (
"container/list"
"fmt"
)
func main() {
l := list.New()
// create a fixed size array and initialize it
var arr [2]int
arr[0] = 1
arr[1] = 2
// push a pointer to the array into the list
elem := l.PushFront(&arr)
// modify the stored array
elem.Value.(*[2]int)[0] = 3
// print the element from iterating the list
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
// print the underlying array, both are modified
fmt.Println(arr)
}
EDIT
Note that this behaviour is not something specific to this list implementation, but rather related how type assertions work in the language itself.
See here:
https://golang.org/doc/effective_go.html#interface_conversions
Quoting from that section (and adding emphasis of my own):
The syntax borrows from the clause opening a type switch, but with an explicit type rather than the type keyword: value.(typeName) and the result is a new value with the static type typeName.
When using reference types, copying the value does not affect you cause copying a pointer value ends up allowing you to change the same underlying reference.
But when the array is a value itself, it does not make sense to assign to the copy. When you try to modify the array directly (without assigning the type assertion to a variable) Go would even catch this at compile time so that you don't assign to this "temporary" copy which would obviously be a mistake. That's how you got all your syntax errors when trying to do this.
To overcome this (if you don't want to use pointers) one possibility might be for you to implement your own list, borrowing from the implementation you are using but making your Element's value an explicit [2]int instead of an interface.
That would remove the need to make the type assertion and you'll be able to modify the underlying array.
I am being passed an array from a C program that does not include the size of the array; that is, it just passes a pointer to the array. The array is a generic type <Item>. How can I determine the end of the array in order to detect a buffer overflow?
I tried iterating through the array until I received something that wasn't an <Item>. That worked most of the time but sometimes the nonsense at the end Would be of type <Item>. I am using C and calling a function from an external class I had no deal in developing. <Item> is a struct with multiple references to other arrays (sort of like a linked list).
EDIT:
The api stated that the array was intended to be a read-only version. The problem is I cannot read it if I do not know the size. It doesn't appear there is a sentinel value. There is a random comment stating that if the size is needed use sizeOf (array)/sizeOf (Item) which doesn't work. It was developed by a team that no longer works here. The problem is other code already relies on this C code and I cannot change it without fear of ruining other code.
It is not possible to determine the end of an array based on just a pointer to an element of that array.
I tried iterating through the array until I received something that wasn't an <Item>
It's also not possible to determine whether particular memory location contains an object of particular type - or whether it contains any object. Even if you could, how would you determine if the object that you find is really part of the array and not just a separate <Item> object that happens to be there?
A possible solution is to use a sentinel value to represent the end of an array. For example, you could define the interface such that <Item>.member == 0 if and only if that is the last element of the array. This is similar to how null-terminated strings work.
If all you have is a pointer and no size or known "end-of-array" marker (sentinel) in the data, then you have an impossible situation. There is no way in that case to determine the size/end of the passed array.
I think inout makes you passes in a reference (is that accurate?), then if the reference gets changed many times, as you might do with an array, the array then does not have to copied many times because its now a reference type?
The semantics for in-out parameters in swift is different from passing value by reference. Here's exactly what happens when you're passing an in-out parameter:
In-out parameters are passed as follows:
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value is assigned to the original argument.
This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.
See https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545
Array is value type in swift so it's fully copied in this scenario. Of course the swift compiler may optimize that but anyway you're guaranteed to see exact same behavior as it'd be with full copies performed.
If you want to pass an array by reference and allow the called function to modify elements quickly, you have the choice of either explictly creating an NSMutableArray, or creating a class where instances have an array as their single member.
I'm changing some codes in a database library. The way it works I send a void pointer, to get the size of it I call a query and using the query I calculate the size of the structure. Now the problem is I receive the struct as params but the function fails before/in the middle of the first fetch. After that I need to clear the structure, but I dont even have the size.
I know the best way is send the size of the structure as a param, but I have thousands and thousands programs already compiled, the library is from 1996, so I need to find a way to calculate the structure size even if the type is void.
One idea I had was to calculate the position of the next element that is not in the structure
0x000010 0x000042
[int|char[30]|int|int][int]
So the size is 32, because the 0x00042-0x000010 is 32.
Is there a way to know when I got out of the structure.
the prototype of the function is
int getData(char* fields, void* myStruct)
I need to find out the structure size.
Sorry if I missed some information, the code is HUGE and unfortunately I cannot post it here.
No, in general there's no way, given a void *, to figure out what you're after. The only thing you can do is compare it against NULL, which of course doesn't help here.
Note that there's nothing in the void * that even says it points at a struct, it could just as well be pointing into the middle of an array.
If you have some global means of recording the pointers before they're passed to getData(), you might be able to implement a look-up function that simply compares the pointer value against those previously recorded, but that's just using the pointer value as a key.