| View previous topic :: View next topic |
| Author |
Message |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Fri Aug 21, 2009 3:04 pm Post subject: |
|
|
| You should return a pointer.
|
|
| Back to top |
|
 |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
Posted: Fri Aug 21, 2009 3:30 pm Post subject: |
|
|
A pointer to what data type?
I've tried byte** and I couldn't get it to work.
Update: Fixed it by using a single dimension array and messing with the array index with mathematical stuff. Pleh.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time. |
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Fri Aug 21, 2009 5:03 pm Post subject: |
|
|
it depends on the array type ...
for int you should return int*
the pointer that will be returned is the start address of the array
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Aug 22, 2009 4:01 am Post subject: |
|
|
Just pass the array, it's pointing to the first element on its own, go wild.
| Code: | #include <Windows.h>
//////////////////////////////////////////////////////////////////////////
void cool_func(void* in_arr, void* out_arr)
{
memcpy(out_arr, in_arr, 4 * 64);
}
//////////////////////////////////////////////////////////////////////////
int __cdecl main(void)
{
int i;
int butt[64];
int dong[8][8];
for(i = 0; i < 64; i++)
butt[i] = i;
cool_func((int*)butt, (int*)dong);
return 0;
} |
You can also just return a pointer to the array, but you'll need to allocate some memory on the heap and point to that as since when the function returns, your array on the stack is toast. Just remember to free the memory after.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Aug 22, 2009 6:34 am Post subject: Re: Returning array in C++ |
|
|
| Burningmace wrote: | | and even vector<vector<byte>>. |
| Code: | #include <iostream>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <ctime>
typedef unsigned char byte;
std::vector< std::vector< byte > > f(byte x[])
{
std::vector< std::vector< byte > > v;
for(int i=0; i<8; ++i) {
std::vector<byte> b;
for(int j=0; j<8; ++j)
b.push_back( x[i*8+j] );
v.push_back( b );
}
return v;
}
int main(int argc, char*argv[])
{
byte a[64] = { 0 };
srand( static_cast< unsigned int >( time(NULL) ) );
for(int i=0; i<64; ++i)
a[i] = static_cast< byte >( (static_cast< double >(rand()) / RAND_MAX )*0xFF );
std::cout << "a[64]:" << std::endl;
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j)
std::cout << std::setw(3) << static_cast< int >( a[i*8+j] ) << " ";
std::cout << std::endl;
}
std::cout << std::endl;
std::vector< std::vector< byte > > v;
std::vector< std::vector< byte > >::iterator vitr;
std::vector< byte >::iterator bitr;
v = f(a);
std::cout << "v:" << std::endl;
for(vitr = v.begin(); vitr != v.end(); ++vitr) {
for(bitr = vitr->begin(); bitr != vitr->end(); ++bitr)
std::cout << std::setw(3) << static_cast< int >(*bitr) << " ";
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "a[i*8+j] == v.at(i).at(j):" << std::endl;
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j)
std::cout << ( a[i*8+j] == v.at(i).at(j) ) << " ";
std::cout << std::endl;
}
return EXIT_SUCCESS;
} | Note that sizeof(x) is 4 on 32-bit machines. So, you must make sure that it's really a 64 byte array before calling the function.
EDIT: oh right, forgot to do stuff with byte x[].
EDIT2: yeah, right, forgot to reply the original question: if you want to return an array, you can't. Try this: | Code: | typedef byte byteArray[2];
byteArray function()
{ byteArray a; return a; } | and you'll get an error like: | Code: | | error C2090: function returns array | Solution: use classes or pointers. Also, you could pass the output array as a param, but that's not really returning..: | Code: | #include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
typedef unsigned char byte;
void f(byte x[], byte y[8][8])
{
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j)
y[i][j] = x[i*8+j];
}
return;
}
int main(int argc, char *argv[])
{
byte a[64] = { 0 };
byte b[8][8] = { 0 };
srand( static_cast< unsigned int >( time(NULL) ) );
for(int i=0; i<64; ++i)
a[i] = static_cast< byte >( (static_cast< double >(rand()) / RAND_MAX )*0xFF );
std::cout << "b[8][8]:" << std::endl;
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j)
std::cout << std::setw(3) << static_cast< int >( b[i][j] ) << " ";
std::cout << std::endl;
}
std::cout << std::endl;
f(a, b);
std::cout << "b[8][8]:" << std::endl;
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j)
std::cout << std::setw(3) << static_cast< int >( b[i][j] ) << " ";
std::cout << std::endl;
}
std::cout << std::endl;
return EXIT_SUCCESS;
} |
|
|
| Back to top |
|
 |
|