| 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
|
Posted: Sat Jun 28, 2008 1:12 pm Post subject: [C++]Help Resizing array |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25836 Location: The netherlands
|
Posted: Sat Jun 28, 2008 8:32 pm Post subject: |
|
|
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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sat Jun 28, 2008 10:58 pm Post subject: |
|
|
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 |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jul 01, 2008 9:01 am Post subject: |
|
|
| You should use calloc() for arrays.
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Tue Jul 01, 2008 12:22 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Jul 01, 2008 12:36 pm Post subject: |
|
|
| 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 |
|
 |
|