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 


Little question in C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Wed Nov 18, 2009 4:05 pm    Post subject: Little question in C++ Reply with quote

I know 2 ways to pass arrays to a function.

1st way :

Code:

void example(int *array)
{
}

int array[2];
example(array);


2nd way :

Code:

void example2(int array[])
{
}

int array[2];
example2(array);


Which way is better? and why? I knows int *array will take 4 bytes in 32 bit system from the address of the array, but I don't know how int array[] works. Will it allocate more memory for a copy of the table to manipulate in function?? plz help Confused [/code]
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Nov 18, 2009 4:48 pm    Post subject: Reply with quote

When you pass an array as an argument, you aren't passing the array itself, you are passing the location of the first element of an array. Arrays and pointers are not the same; an array is a blob of memory the compiler knows about and can describe, whereas a pointer simply points to an element of the specified type. When you pass an array, you loose this information even if you try to describe it, thus either way is fine. Pick whatever suits your style, just remember to be consistent.

Here is a small example for you: http://codepad.org/Rbf6stCS

Personally, if I ever needed to pass an array, I would have the argument list describe the fact that it is a pointer and no longer an array (int* instead of int[10]). Then again, this is C++; you should pass by reference whenever you can. Smile
Back to top
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Wed Nov 18, 2009 5:12 pm    Post subject: Reply with quote

Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG Evil or Very Mad and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer??
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Nov 18, 2009 6:25 pm    Post subject: Reply with quote

kot1990 wrote:
Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG Evil or Very Mad and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer??
The pointer is 4 byte yes. I don't think you can pass an int[] as a parameter for a function...
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Wed Nov 18, 2009 8:42 pm    Post subject: Reply with quote

The most efficient method is to:

Code:

int MyArray[2];

void Example(int *PointerArray)
{

}


Faster, easy to understand, and quick to do with the same results.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Nov 19, 2009 1:36 am    Post subject: Reply with quote

kot1990 wrote:
Thx Athaem, I have no problem because I used to put pointers to manipulate the arrays inside functions, but this is the second year of my lessons in university and there's a dumb freak teacher that he says he doesn't like pointers OMG Evil or Very Mad and he wants that way void example(int array[]) in my exercises. I want to know if int array[] will allocate more memory or it is the same as the 4 byte pointer??


when using an array as a parameter, you must pass it by reference. so no, you're not copying the memory.


&Vage wrote:
The pointer is 4 byte yes. I don't think you can pass an int[] as a parameter for a function...


you can.
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