simple adding of int problems - assignment makes integer from pointer without a cast - iphone

not sure why this is so difficult for me. I have an array and I store its count like so:
int index = myarray.count;
then I have a for loop like so and I simply want to add index and my i counter below:
for (int i=0; i < anotherarray.count ; i++) {
int newIndex = index + i;
}
this gives me the error:
assignment makes integer from pointer without a cast
and a crazy value

Note that index is a char * function, defined in string.h

Related

Boost numpy example does not work

I tried reproducing some of the examples described here, but I experience the following problem with the code below, which was written by just copy-pasting relevant parts of the linked page.
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <iostream>
using namespace std;
namespace p = boost::python;
namespace np = boost::python::numpy;
np::ndarray test()
{
int data[] = {1,2,3,4,5};
p::tuple shape = p::make_tuple(5);
p::tuple stride = p::make_tuple(sizeof(int));
p::object own;
np::dtype dt = np::dtype::get_builtin<int>();
np::ndarray array = np::from_data(data, dt, shape,stride,own);
std::cout << "Selective multidimensional array :: "<<std::endl
<< p::extract<char const *>(p::str(array)) << std::endl ;
return array;
}
BOOST_PYTHON_MODULE(test_module)
{
using namespace boost::python;
// Initialize numpy
Py_Initialize();
boost::python::numpy::initialize();
def("test", test);
}
When I compile as a shared library and load the module in python,
import test_module as test
print(test.test())
it seems that the ndarray gets created properly by the C++ code, but the version that python receives is rubbish; the arrays that get printed are:
[1 2 3 4 5]
[2121031184 32554 2130927769 32554 0]
What could be the cause of such a difference?
This week I had the same issue. To solve my problem I use dynamic memory:
np::ndarray test(){
int *data = malloc(sizeof(int) * 5);
for (int i=0; i < 5; ++i){
data[i] = i + 1;
}
p::tuple shape = p::make_tuple(5);
p::tuple stride = p::make_tuple(sizeof(int));
p::object own;
np::dtype dt = np::dtype::get_builtin<int>();
np::ndarray array = np::from_data(data, dt, shape, stride, own);
return array;
}
I think the difference according to this answer: https://stackoverflow.com/a/36322044/4637693 is:
The difference between declaring an array as
int array[n];
and
int* array = malloc(n * sizeof(int));
In the first version, you are declaring an object with automatic storage duration. This means that the array lives only as long as the function that calls it exists. In the second version, you are getting memory with dynamic storage duration, which means that it will exist until it is explicitly deallocated with free.
I will take more time in the next weeks to see if this works for a matrix too.
EDIT
Or you can use a dynamic structure from boost like list:
np::ndarray test(){
boost::python::list my_list;
for (int i=0; i < 5; ++i){
my_list.append(i + 1);
}
np::ndarray array = np::from_object(my_list);
return array;
}
This work also for a Matrix for example:
np::ndarray test(){
//This will use a list of tuples
boost::python::list my_list;
for (int i=0; i < 5; ++i){
my_list.append(boost::python::make_tuple(i + 1, i, i-1));
}
//Just convert the list to a NumPy array.
np::ndarray array = np::from_object(my_list);
return array;
}
I assume (for the moment) that by using the boost functions you will be able to avoid the memory conflicts.
Creating a new reference to the array before returning it solved the problem. Good news is that np::ndarray has a copy() method that achieves exactly the same thing. Thus, you should add
np::ndarray new_array = array.copy();
before the return statement

Assigning an array to a vector 32 bits at the time

I have an array 32 bit wide of n elements and I am trying to assign these elements to a vector, I have the following code:
function automatic logic [SIZE-1:0] my_function (my_array x_map);
logic SIZE-1:0] y_map = '0;
int fctr = (SIZE)/32;
int top_bnd = 31;
int lwr_bnd = 0;
for(int k0 = 0; k0 < fctr; k0++)
begin
y_map[top_bnd:lwr_bnd] = x_map[k0];
top_bnd = (top_bnd + 32'hFFFF);
lwr_bnd = (lwr_bnd + 32'hFFFF);
end
return y_map;
endfunction
However this is not working and I get two errors:
1) "the range of the part select is illegal"
2) "Cannot evaluate the expression in left slicing expression, the expression must be compile time constant"
Thanks
You might want to use the streaming operators for this
y_map = {<<32{x_map}};
BTW, you should show the declarations of all identifiers in your example, i.e. my_array.

Is it possible to append the value of macro argument instead of appending it literally?

I have a macro that I would like to use in the following fashion:
`define assign_m(CH, INT_NUM) \
assign dp_input``CH``_if.master_mp.sigA[INT_NUM] = some_signal[CH][INT_NUM];
generate
for(genvar i=0; i<2; i++) begin
for(genvar j=0; j<2; j++) begin
`assign_m(i,j)
end
end
endgenerate
I would like to have a construct like this to expand to:
assign dp_input0_if.master_mp.sigA[0] = some_signal[0][0];
assign dp_input0_if.master_mp.sigA[1] = some_signal[0][1];
assign dp_input1_if.master_mp.sigA[0] = some_signal[1][0];
assign dp_input1_if.master_mp.sigA[1] = some_signal[1][1];
But of course it doesn't happen that way as Verilog literally appends the variable j instead of its value (ยง 22.5 `define, `undef, and `undefineall of IEEE Std 1800-2012, page 644).
How can I have a macro where the argument's value is appended?
If dp_input_if can be created as an array and an indexed version used, then it could be written out as:
integer i;
integer j;
always #* begin
for(i=0; i<2; i++) begin
for(j=0; j<2; j++) begin
dp_input_if[i].master_mp.sigA[j] = some_signal[i][j];
end
end
end

COnverting Char Array to Long int

How to convert Char array to long in obj c
unsigned char *composite[4];
composite[0]=spIndex;
composite[1]= minor;
composite[2]=shortss[0];
composite[3]=shortss[1];
i need to convert this to Long int..Anyone please help
If you are looking at converting what is essentially already a binary number then a simple type cast would suffice but you would need to reverse the indexes to get the same result as you would in Java: long value = *((long*)composite);
You might also consider this if you have many such scenarios:
union {
unsigned char asChars[4];
long asLong;
} value;
value.asChar[3] = 1;
value.asChar[2] = 9;
value.asChar[1] = 0;
value.asChar[0] = 10;
// Outputs 17367050
NSLog(#"Value as long %ld",value.asLong);

Vector/Array to integer

-(void)userShow{
xVal = new vector<double>();
yVal = new vector<double>();
xyVal = new vector<double>();
xxVal = new vector<double>();
value = new vector<double>();
for(it = xp->begin(); it != xp->end(); ++it){
xVal->push_back(it->y);
xxVal->push_back(it->x);
}
for(it = yp->begin(); it != yp->end(); ++it){
xyVal->push_back(it->x);
yVal->push_back(it->y);
}
for (int i = 0; i < xVal->size(); i++){
int c = (*xVal)[i];
for(int i = 0; xyVal[i] < xxVal[i]; i++){
double value = yVal[c-1] + (yVal[c] - yVal[c-1])*(xxVal[i] - xyVal[c-1])/(xyVal[c] - xyVal[c-1]);
yVal->push_back(value);
}
}
}
I am having an issue with the double value = ... part of my code. I get three errors saying invalid operands to binary expression ('vector<double>' and 'vector<double>') pointing to the c.
should int c = (*xVal)[i]; be double c = (*xVal)[i]; when i try to use double i get 6 errors saying Array subscript is not an integer. Which means I need to convert the array into an integer. How am I getting an array if I am using vectors? Just a lot of confusion at the moment.
Not really sure if i really need to explain what the code is supposed to do, but if it helps. I am trying to get it so it take two vectors splits the vectors x and y's into x and y. then take the y of xp and the y of yp and put them together. but because xp and yp vectors do not match i need to use the for loop and the double value algorithm to get a decent set of numbers.
The c is fine. The problem really is in double value = .., as your compiler says. You have pointers, so you can't access the array's elements like this:
double value = yVal[c-1] + ...
It must be
double value = (*yVal)[c-1] +
The same for xyVal, xxVal, etc. You need to fix the whole inner for loop.
But why you allocate the vectors like this...? Is there any reason to use new? This is so error prone. I'd use just
vector<double> xVar;
instead of
xVal = new vector<double>();
And then use . instead of -> combined with *. It so much easier.
Ah, forgot about the question for c - no, it should not be double. You can't use floating point numbers for indices. Also, if xVal is supposed to contain integer numbers (so that they can be used for indices), why don't you just declare the vector as vector< int > instead of vector< double >? I don't what's the logic in your program, but it looks like it(the logic) should be improved, IMO.