Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-2008, 11:55 AM
gamiR's Avatar   
gamiR gamiR is offline
Learning Programmer
 
Join Date: Aug 2008
Location: Nashik,Maharashtra,INDIA
Posts: 85
Rep Power: 2
gamiR is on a distinguished road
Exclamation Reallocate Memory

In my C++ programme I want to use dynamic array which will be modified easily.i.e. Consider the array of int datatype .Initially Its size is asked to user.If array becomes full and I want to add some more element how the size of that array will be increased.Please suggest some function in c++ which can do this.I know about New operator.Will it be used for reallocation of memmory i have doubt about it.
__________________
Busy Penguin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-14-2008, 12:06 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Reallocate Memory

No. Try using realloc(). You need to include malloc.h for it. I'm not sure if it works with the new operator. In that case, you'll need to allocate memory like this (someone else correct me if I'm wrong, please):

Code:
#include <malloc.h>
#include <iostream>

using namespace std;
.
.
.
int *myArray;
int arrSize = 10;

//use calloc to allocate arrays, and malloc to allocate single blocks,
//like for classes and structs. I'd use the new operator for classes,
//though.

myArray = (int *)calloc(arrSize,sizeof(int));
if(myArray == NULL)
    cout << "Allocation failed." << endl;
.
.
.
//reallocate:
arrSize = 15;
int *temp = (int *)realloc(myArray,arrSize);
if(temp == NULL)
    cout << "Reallocation failed." << endl;
else
    myArray = temp;
.
.
.
free(myArray);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-14-2008, 12:43 PM
gamiR's Avatar   
gamiR gamiR is offline
Learning Programmer
 
Join Date: Aug 2008
Location: Nashik,Maharashtra,INDIA
Posts: 85
Rep Power: 2
gamiR is on a distinguished road
Default Re: Reallocate Memory

But suppose i allocated memory to array using New operator.And now use realloc function will it work?
__________________
Busy Penguin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-14-2008, 12:52 PM
Aereshaa's Avatar   
Aereshaa Aereshaa is offline
Guru
 
Join Date: Apr 2008
Posts: 539
Rep Power: 9
Aereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the rough
Default Re: Reallocate Memory

No. The new operator may allocate memory a different way, so even if it works on your system, it might not work on other peoples'. Better to allocate using the function malloc() and deallocate using free():
Code:
int * array = malloc(10 * sizeof(int));
array = realloc(array, 15 * sizeof(int));
free(array);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-14-2008, 01:01 PM
gamiR's Avatar   
gamiR gamiR is offline
Learning Programmer
 
Join Date: Aug 2008
Location: Nashik,Maharashtra,INDIA
Posts: 85
Rep Power: 2
gamiR is on a distinguished road
Default Re: Reallocate Memory

Thank you for your guidance .So C++ has this drawback.We have to use malloc & realloc .
__________________
Busy Penguin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 09-14-2008, 01:50 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Reallocate Memory

A better option is to use a vector instead of an array. You can indicate the initial size, and it will automatically grow as needed if you exceed that size. That will remove the need to handle memory allocation yourself.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-14-2008, 01:55 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is offline
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 83
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: Reallocate Memory

Linked lists are good for this type of thing to.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-14-2008, 09:30 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: Reallocate Memory

Quote:
Originally Posted by WingedPanther View Post
A better option is to use a vector instead of an array. You can indicate the initial size, and it will automatically grow as needed if you exceed that size. That will remove the need to handle memory allocation yourself.
Aye.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-15-2008, 12:17 AM
Anirban Chakraborty's Avatar   
Anirban Chakraborty Anirban Chakraborty is offline
Learning Programmer
 
Join Date: Oct 2006
Location: India
Posts: 50
Rep Power: 8
Anirban Chakraborty will become famous soon enough
Default Re: Reallocate Memory

When it is specified that one has to use "dynamic array" you are not supposed to use Linked List. Guess that is an assignment.
So better option is that one uses vector. Moreover one won't have to get bothered about methods and pointers of Linked List.
__________________
Anirban Chakraborty
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 09-15-2008, 12:27 AM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is offline
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 83
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: Reallocate Memory

What are you talking about, of course you can use linked lists for dynamic memory assignment.

Are you serious?

After reading your post, I'm going to slightly retract that statement as I now think that maybe that was a comment on your assignment...

Last edited by MeTh0Dz|Reb0rn; 09-15-2008 at 12:28 AM. Reason: Edit...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared Memory implementation using thread ( C on AIX) kumars C and C++ 0 06-18-2008 06:19 AM
Hacking Applications With Memory Editors TcM Security Tutorials 8 06-07-2008 01:52 PM
Memory leak prevention methodogies c___newbie C and C++ 2 11-18-2007 11:29 AM
Virtual Memory ... Windows XP mindsurfer Computer Hardware 1 11-01-2007 07:38 PM


All times are GMT -5. The time now is 02:02 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads