Solved

Shortening a C++ array

Posted on 2007-03-24
12
391 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 62
Problem to scan all sheets 3 93
GUI: DIalog Stacking and Popping in MS C++ 4 65
Dynamically allocate memory 9 56
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

932 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

13 Experts available now in Live!

Get 1:1 Help Now