Don't know if anyone here cares to help, but i couldn't find any answers with searching google.
I want to take the size of two arrays, add them, then dynamically allocate a new one with the size of that sum.
i can't get the new array to work outside of this function.
is there a way to pass it outside of this function?
void sizing(double *f1, double *f2, double *out1, double *out2)
{
int x,y,z;
x = (sizeof(f1))/ (sizeof(f1[0]));
y = (sizeof(f2))/ (sizeof(f2[0]));
z = x+y;
*out1 = new double[z];
*out2 = new double[z];
}
Name:
Anonymous2011-11-07 23:54
>>1
Try to google: Leah Culver's five stars algorithm. It's an interesting little math problem.
Name:
Anonymous2011-11-08 0:19
You've got way more problems on your hands than merely figuring out how to pass pointers by reference so that you can return the results to the caller. The sizeof operator doesn't work on pointers for determining the array length, it instead returns the size of the pointer, which is 4 or 8 bytes depending on your target architecture. Plus you've got two outputs when it's obvious you only want one.
The following code is the proper C++11 way of doing it.
// result now holds the values { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }
// do something with result, like print it out
return 0;
}
Since std::vector, std::copy and std::back_inserter do all of the work for you, you don't really need to write a separate function to concatenate f1 and f2, you just need to compose the various primitives that the Standard C++ Library provides you together.
Note that it requires the new C++11 initializer list syntax for constructing f1 and f2, so with GCC 4.6+ compile with the -std=c++0x flag.
You need to pass the length in as well because arrays are just pointers.
void sizing(double *f1, int f1len, double *f2, int f2len, double **out1, double **out2)
{
int z = f1len + f2len;
*out1 = new double[z];
*out2 = new double[z];
}
>>4 because arrays are just pointers
no arrays are arrays, different than pointers. however they are converted to pointers when passed to a function.
you can use vector or you can simply pass size of arrays OP.
Name:
Anonymous2011-11-08 8:47
>>1
Arrays in C (and C++) decay to pointers when passed to a function, and pointers do not carry size information. You have to manually pass in the number of elements, as in >>4.
>>10
meh, I thought he was trying to free a statically allocated array since he finds its size using (sizeof(f1))/ (sizeof(f1[0]));. sizeof tricks won't work if he is using dynamic allocation.