| | |
works for static need help to make it dynamic
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 1
Reputation:
Solved Threads: 0
I have used arrays in the program; a[3] - now i want to change the program to dynamic array value
If I enter more than allocated(3) characters in the input - it throws error and the program stops running
error "Segmentation fault" (i am running c on unix)
- i dont want that to happen - it should be able to handle any number of characters entered without limit.
What should i use? i tired using
char *c1="hi" but I was not able to do it. I am a beginner level programmer.
Warm Regards
Kumar.
If I enter more than allocated(3) characters in the input - it throws error and the program stops running
error "Segmentation fault" (i am running c on unix)
- i dont want that to happen - it should be able to handle any number of characters entered without limit.
What should i use? i tired using
char *c1="hi" but I was not able to do it. I am a beginner level programmer.
Warm Regards
Kumar.
Last edited by emailsrs; Dec 16th, 2005 at 10:53 am. Reason: little changes
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
dynamic memory in C uses malloc(), or a similar function, to allocate dynamic memory and free() to delete dynamic memory. In C++ it's new() and delete. In either case, if you want memory for an array of items then you add the [] operator, as appropriate.
//allocate an array of 10 char using dynamic memory
char * c = malloc char[10];
char * cpp = new char[10];
Neither C nor C++ have built in array size detection. The C++ STL class do include that "technology", and for that reason alone, it may be worth your while to consider dumping arrays/C style strings and learn about the STL vector and string classes. Having said that, there isn't anything wrong with learning how to adjust your arrays so they fit "just right". Most people find it boring to do this all the time, and it's not worthwhile from a memory standpoint or a speed standpoint on most computers, but it is a reasonable learning project.
To get array sizes just right you need to somehow keep track of the current size of the array and the capacity of the array. If the size of the array equals the capacity of the array and you want to add to the array, then you need to expand the array, and if the size is less than the capacity, then you need to decrease the size of the array. You start by declaring a temporary array of the size of the current array and copy the data from the current array into the temporary array. You then delete the memory from the current array and redeclare with just the right amount of memory you want/need. Then you copy the information in the temporary array back into the original array and delete the temporary array. Here's an example using char arrays and C++. It's a bit different using other types of arrays, but the basic theory is the same.
Goal: change word to fit lakes from lake
int orig = 5;
int final = 6;
char * word = new char[orig];
strcpy(word, "lake");
char * temp = new char[orig];
strcpy(temp, word);
delete word[];
word = new char[final];
strcpy(word, temp);
word[4] = 's';
word[5] = '\0';
cout << word << endl;
delete temp[];
//allocate an array of 10 char using dynamic memory
char * c = malloc char[10];
char * cpp = new char[10];
Neither C nor C++ have built in array size detection. The C++ STL class do include that "technology", and for that reason alone, it may be worth your while to consider dumping arrays/C style strings and learn about the STL vector and string classes. Having said that, there isn't anything wrong with learning how to adjust your arrays so they fit "just right". Most people find it boring to do this all the time, and it's not worthwhile from a memory standpoint or a speed standpoint on most computers, but it is a reasonable learning project.
To get array sizes just right you need to somehow keep track of the current size of the array and the capacity of the array. If the size of the array equals the capacity of the array and you want to add to the array, then you need to expand the array, and if the size is less than the capacity, then you need to decrease the size of the array. You start by declaring a temporary array of the size of the current array and copy the data from the current array into the temporary array. You then delete the memory from the current array and redeclare with just the right amount of memory you want/need. Then you copy the information in the temporary array back into the original array and delete the temporary array. Here's an example using char arrays and C++. It's a bit different using other types of arrays, but the basic theory is the same.
Goal: change word to fit lakes from lake
int orig = 5;
int final = 6;
char * word = new char[orig];
strcpy(word, "lake");
char * temp = new char[orig];
strcpy(temp, word);
delete word[];
word = new char[final];
strcpy(word, temp);
word[4] = 's';
word[5] = '\0';
cout << word << endl;
delete temp[];
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- how to make dynamic (JSP)
- Making T.Greer's JavaScript-synthesised combobox dynamic (JavaScript / DHTML / AJAX)
- Html basic question "creating a board thing" (HTML and CSS)
- dynamic IP's and Websites (Domains and DNS)
- Can php allow me to create whatever.homepage.com address (PHP)
Other Threads in the C++ Forum
- Previous Thread: compiling errors
- Next Thread: appending and subtracting a number in an array
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






