Solved

Shortening a C++ array

Posted on 2007-03-24
12
404 Views
Last Modified: 2010-05-18
Say an array has 5 elements: A B C D E. I want to delete the last element so the array become A B C D, but don't want to create a new array. What's the safe way to do it? Is it enough if E spot is assigned NULL?

Thanks
0
Comment
Question by:gromul
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18787343
If these elemts are strings or characters, yes, NULL is an option to do that. If you are using an arbitrary data type, you'd have to come up with one value that serves that purpose (if possible). What are you storing in that array?
0
 

Author Comment

by:gromul
ID: 18787401
Let's say it's an array of integers. They are also NULL terminated, right?
0
 

Author Comment

by:gromul
ID: 18787403
I'm concerned with a possibility of a memory leak.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 84

Expert Comment

by:ozo
ID: 18787415
What do you mean a memory leak?
Ae you trying to access beyond the number of elements in the array?
Can you set a variable to be the array size <= 5, and never access beyond that?
0
 

Author Comment

by:gromul
ID: 18787439
Would there be a memory leak in this case? array = A B C D E 0

*(array+2) == NULL; // B is now the last element in array: A B 0 D E

Are D and E now a memory leak?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18787471
It's not what I would call a memory leak.
0
 

Author Comment

by:gromul
ID: 18787485
So all the memory will be recovered upon exiting the program?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18787557
Yes, all the memory will be recovered upon exiting the program.
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18787760
>>Say an array has 5 elements: A B C D E. I want to delete the last element so the array become A B C D, but don't want to create a new array.

to use a vector data type is the safest and fastest way if you want to delete or insert after the last element.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18787976
Vector is a good idea.
What kind of array are you talking? Static one or a dynamic one.
int* iArray=new int[5];

iArray[0]=1;
.
.
iArray[4]=5;

if you delete the last element still the memory is remained allocated.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18788362
If you want to store integers, a 0 is probably not a good idea, since 0 is a valid integer value. However, since your concerns about memory leaks could already be ruled out (the full allocation size is always freed when an array goes out of scope), what about using another variable that describes the valid upper range for that array, i.e.

int array[10];
unsigned max_array_index = 9;

// fill it up to array[9]
//...

// reduce size

max_array_index = 5;

Alternatively, you might want to take a look at vectors: http://www.sgi.com/tech/stl/Vector.html

These can be used just like arrays and offer a lot of improvements, you could just

#include <vector>
using namespace std;

vector<int> array(10); // 10 elements

for (int i = 0; i < 10; ++i) {

  array[i] = i;
}

array.resize(5); // reduce to 5 elements;

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 18793114
>>>> array has 5 elements: A B C D E. I want to delete the last element
>>>> so the array become A B C D,
If you pass the array to a function the array turns to a pointer pointing to the first array element. There isn't an size information the called function can get from it's argument.

   void f(int arr[])
   {
         int siz = sizeof(arr);  // BAD: size is pointer size
                                           // cause arr has turned to a pointer

   }

So, in any case where you pass an array you either need to tell the size

    void f(int arr[], int siz);     // NOTE, you may tell a smaller size by the caller

*or* one of the array elements is a terminator.

#define MIN_INT -(1<<31)
   void f(int arr[])
   {
         // determine size
         int siz = 0;
         while(arr[siz] != MIN_INT) siz++;
   }

Note, MIN_INT is a better terminator than 0 but nevertheless it is a valid integer and you may get problems when using it as a terminator, e.  g. if you do

   arr[0] = MIN_INT;  

to make the 0th element invalid you have terminated the whole array by that ...

Also consider that your initial array A, B, C, D, E was *not* terminated. For a terminated array you always have to allocate one element more and always have to care for proper termination. So, normally a terminator is only used for char arrays and pointer arrays (terminated by a NULL pointer).

>>>> So all the memory will be recovered upon exiting the program?
All allocation/deallocation  happens by using a size parameter. The contents of an array never is used for freeing the memory but always the size that was passed when allocationg the memory.

Regards, Alex
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 66
convert char array to number in c 5 85
Issues with C++ Class 19 92
designing in object programming 12 75
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question