Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shortening a C++ array

Posted on 2007-03-24
12
Medium Priority
?
446 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:Deepu Abraham
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 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

604 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