Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Returning array in C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
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

PostPosted: Fri Aug 21, 2009 2:50 pm    Post subject: Returning array in C++ Reply with quote

Ok, this is driving me mad.

I'm trying to make a function that takes a one dimensional array of 64 of bytes, then returns a two dimensional array (each rank having a length of Cool of bytes.

I've tried returning byte** and vector<byte>*, and even vector<vector<byte>>.

I can't work out how to get this working.

_________________
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
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Fri Aug 21, 2009 3:04 pm    Post subject: Reply with quote

You should return a pointer.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Fri Aug 21, 2009 3:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Fri Aug 21, 2009 5:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Aug 22, 2009 4:01 am    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sat Aug 22, 2009 6:34 am    Post subject: Re: Returning array in C++ Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites