Solved

Shortening a C++ array

Posted on 2007-03-24
12
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

695 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