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 


[C++]Help Resizing array

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sat Jun 28, 2008 1:12 pm    Post subject: [C++]Help Resizing array Reply with quote

I'm trying to add new entries into an array, but I have no idea how many entries I'm going to have when the program is run so I need to resize the array each time I want to add an entry.
Code:
   void addAddy(int aaAddy){
         int addysize = sizeof(count*sizeof(int));
         int * tmp = (int*) malloc(addysize+sizeof(int));//make a tmp of the size addys is + room for another entry
         int * tmp2 = addys;//make another tmp and set it to addys.
         addys = tmp;//set addys to the new array.
         memcpy(addys, tmp2, addysize);//Copy the old array into the new array.
         free(tmp2);//free the old array
         addys[count] = aaAddy;//set the last entry in the array to the value
         count++;}//increase the count


It goes through once fine, but the second time it hangs somewhere in this function. I haven't been able to pinpoint the problem yet, but I think it hangs around free and memcpy.

_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Jun 28, 2008 1:35 pm    Post subject: Reply with quote

May I suggest using linked lists. It is a much easier way of doing this then trying to dynamically size arrays at runtime. Here is a nice tutorial for it:

http://richardbowles.tripod.com/cpp/linklist/linklist.htm

And here is the full source if you would like to look through it and just pick up the gist of it instead of reading the tutorial:

http://richardbowles.tripod.com/cpp/linklist/linklist.cpp

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Jun 28, 2008 6:57 pm    Post subject: Reply with quote

You could use a vector as well.

http://www.cplusplus.com/reference/stl/vector/
http://www.cppreference.com/cppvector/all.html
http://www.mochima.com/tutorials/vectors.html

And a ton more links for examples and help via Google.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25836
Location: The netherlands

PostPosted: Sat Jun 28, 2008 8:32 pm    Post subject: Reply with quote

realloc
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sat Jun 28, 2008 10:58 pm    Post subject: Reply with quote

Thanks for the suggestions, guys. realloc seems like it's exactly what I'm looking for. I'd have to rewrite most of my class to switch to vectors or linked lists, so hopefully I can get realloc to work.
_________________
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Tue Jul 01, 2008 9:01 am    Post subject: Reply with quote

You should use calloc() for arrays.
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Tue Jul 01, 2008 12:22 pm    Post subject: Reply with quote

In sources, 9 times out of 10 I see malloc(count*size) instead of calloc(count, size) for arrays. Is there any real difference between them?
_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Jul 01, 2008 12:36 pm    Post subject: Reply with quote

http://msdn.microsoft.com/en-us/library/3f8w183e.aspx wrote:

Allocates an array in memory with elements initialized to 0.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
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