Solved

Shortening a C++ array

Posted on 2007-03-24
12
377 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
Comment Utility
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
Comment Utility
Let's say it's an array of integers. They are also NULL terminated, right?
0
 

Author Comment

by:gromul
Comment Utility
I'm concerned with a possibility of a memory leak.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
It's not what I would call a memory leak.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Expert Comment

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

Expert Comment

by:MacroLand
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now