Writing 2D array to binary file [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
The following code is supposed to write a 2D array (quad_array) to a binary file. The code creates the file (quads.dat) but does not write anything to it (0 bytes).
XMFLOAT3 * quad_array;
quad_array = new XMFLOAT3 * [quad_width];
for (unsigned int x = 0; x < quad_width; x++) {
quad_array[x] = new XMFLOAT3[quad_height];
}
// ... fills quad_array with data...
ofstream ofs("quads.dat", std::ofstream::binary);
ofs.open("quads.dat");
streamsize size = sizeof(XMFLOAT3)*quad_height*quad_width;
ofs.write((char*)&quad_array[0][0], size);
ofs.close();

I think that you should remove the
ofs.open("quads.dat");
statement, because you already gave the filename at the previous line (constructor).
But that is not enough. You don't have a 2D array, but an array of pointers to arrays, so they are not contiguous in memory.
If quad_height and  quad_width are compile-time constants, you could use nested std::array with C++11.
See also this.

Related

taking sorted array and value in a function and placing that value in correct location [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having problem in this particular question, Please guide me with simple c++. in a function we have to take 2 arguments one is sorted array and the other is value, function should place that value in correct location so that array remains in sorted order. There will be a use of pointers but i m confused in this question just guide me how to make use of pointers in this particular question. It would be better if you provide me with the sample good on how it works. Thanks in advance.
Seems trivial:
template <typename T>
int insert(T* array, int size, T const& value) {
T* position = std::upper_bound(array, array + size, value);
std::rotate(position, array + size, array + size + 1);
*position = value;
return size + 1;
}

C++ How to resize a dynamic array without losing data? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to resize a dynamic array without losing data.
Like this:
double * pVecDin;//POINTER
int num_values = 2;
pVecDin = new double[num_values];
pVecDin[0]=5;
pVecDin[1]=6;
int new_num_values=4;
pVecDin = new double[new_num_values];
//Next I lost value of pVecDin[0] and pVecDin[1]
pVecDin[2]=8;
pVecDin[3]=9;
Do I need make an Auxiliar Dynamic Array to copy the old values?
Like:
int new_num_values=4;
double * pVecDin_aux; //POINTER
pVecDin_aux = new double[new_num_values];
pVecDin_aux = pVecDin;
for(int i=0; i < n; i++)
{
pVecDin_aux[i] = pVecDin[i];
}
Make a new, empty array with the new desired size.
Copy everything in the old array to the new array.
Delete the old array.
Assign the pointer of the old array to the address of the new array.
Or use a vector.
As nhgrif has mentioned, you need to make copy of the array.
There is another way if you can use malloc for memory allocation, then you can use realloc for resizing the array.
There are better ways as suggested by other like to use std::vector or list.

can a c vector store more than one data type? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}

Convert string array to char ** [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
string str[3];
void foo(char** str)
{
//do something to str...
}
How to pass str[] to function foo in a convenient way?
The function expects an array of pointers, so you'll have to make one from your array of strings:
std::vector<char*> pointers;
for (auto & s : str) {
pointers.push_back(&s[0]);
}
foo(&pointers[0]);
Beware that this may not be valid if the function modifies the pointers, or the strings they point to. A better option would be to avoid mixing C and C++ style string handling, if possible.

C++ to matlab conversion [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to convert some C++ code into the matlab
in the C++ there is following statement:
static double *L[2];
for (int y=0;y<2;y++)
L[y] = new double[size];
in matlab I'll need to initialize the L parameter with some initial value like 0.
Can you please explain what does the C++ code means?
I need the same for the following:
static double **a[2];
for (int x=0;x<2;x++)
{
a[x] = new double*[size];
for (int y=0;y<size;y++)
{
a[x][y] = new double[numstates];
}
}
BR
L is an array of two arrays, each of them is an array of size doubles. It's the same as
L[0] = new double[size];
L[1] = new double[size];
You may interpret L as a 2xsize matrix.
Likewise, a is 2xsizexnumstates matrix of doubles.
Can you please explain what does the C++ code means?
The first snippet is just initializing a (2 * size) matrix of double-precision floating point numbers.
The second snippet is doing the same, but it's initializing a (2 * size * numstates) matrix.