|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||
|
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);
|
|
|||||
|
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); |
|
|||||
|
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. |
|
|||
|
Quote:
|
|
|||||
|
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 |
|
|||||
|
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... |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |