works for static need help to make it dynamic

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 1
Reputation: emailsrs is an unknown quantity at this point 
Solved Threads: 0
emailsrs emailsrs is offline Offline
Newbie Poster

works for static need help to make it dynamic array size

 
0
  #1
Dec 16th, 2005
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.
Last edited by emailsrs; Dec 16th, 2005 at 10:53 am. Reason: little changes
Attached Files
File Type: c hi.c (353 Bytes, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: works for static need help to make it dynamic array size

 
0
  #2
Dec 16th, 2005
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[];
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: works for static need help to make it dynamic

 
0
  #3
Dec 16th, 2005
lerner plz use code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC