473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Values disappearing from array on cleanup

179 100+
I've got a section of a program that I can't quite get to work. I'm fairly sure its something very simple/trivial but it looks correct to me, so if someone could help me fix the problem, and explain what it is that is wrong, that would be great... I've posted a sample of code, which is the bit of interest:

Expand|Select|Wrap|Line Numbers
  1. class Solution
  2. {
  3. private:
  4.    int *HeuristicTours;
  5.  
  6. public:
  7.     int* GetTours()
  8.     {
  9.         return HeuristicTours;
  10.     }
  11. };
Expand|Select|Wrap|Line Numbers
  1. int run(Problem &p)
  2. {
  3.    int **m_ppiTourMatrix_bestsofar; // declared globally so cleanup() can access
  4.    int *piNext, *piTours;
  5. ...
  6.    piNext = p.solution.GetTours();
  7.    for (i=0; i<p.solution.HeuristicVehicleCount; i++)
  8.    {
  9.       iCount = m_ppiTourMatrix_bestsofar[i][0];
  10.  
  11.       for (j=1; j<=iCount; j++)
  12.       {
  13.          *piNext = m_ppiTourMatrix_bestsofar[i][j];
  14.          piNext++;
  15.       }
  16.  
  17.       *piNext = -1;
  18.       piNext++;
  19.    }
  20.    cleanup();
  21.    return 0;
  22. }
  23.  
  24. void cleanup()
  25. if (m_ppiTourMatrix_bestsofar != NULL)
  26. {
  27.    free(m_ppiTourMatrix_bestsofar);
  28.    m_ppiTourMatrix_bestsofar = NULL;
  29. }
  30.  
m_ppiTourMatrix_bestsofar is populated with values to start with, so the aim is to copy these values to the p.solution tours. This then can iterate through the tours later, using -1 as a termination of a tour.

The problem I have is that although the values are set fine before calling cleanup(), as soon as cleanup() has been called my values within my solution tours are now uninitalised.

I thought that
Expand|Select|Wrap|Line Numbers
  1. *piNext = m_ppiTourMatrix_bestsofar[i][j];
would copy the value within the matrix, into the value stored at location piNext, but if that were the case then freeing up m_ppiTourMatrix_bestsofar shouldn't make any difference should it?

Thanks for your help.
Apr 24 '08 #1
4 2634
weaknessforcats
9,208 Expert Mod 8TB
Several things aren't correct here.

1) Solution::GetTours() returns a pointer to a Solution private data member. That breaks encapsulation and makes the private data member dangerous to use since it can be altered(even deleted) by nefarious agents who do not belong to the class.

I wouldn't do that. Unstead, write methods to return and update a specific element in this array.

2) This -1 business is really another private Solution data member foe the number of elements. The Solution member funcitons use this to control themseleves.

3) This code:
[code=cpp]
for (i=0; i<p.solution.HeuristicVehicleCount; i++)
[/quote]

infers that there is a public data member. You don't need that. What you need is a method to position to the start of your HeuristicTours array and another to tell you when you have reached the end. That is, this is C++ so why are you not using a vector<int>?

4) Global variables are a big no-no. Read this article http://bytes.com/forum/thread737451.html. In this case m_ppiTourMatrix_bestsofar shoulkd be a function argument.

5) m_ppiTourMatrix_bestsofar is an int** and this should be
used as the address of a 2D array. Read this article: http://bytes.com/forum/thread772412.html.

6) m_ppiTourMatrix_bestsofar is an int**. This is the address of an int pointer.
Freeing this does not free the stuff the int pointer points at. It just frees the pointer to the pointer. If the address in the pointer m_ppiTourMatrix_bestsofar points at is on the heap, you leak. Again why are you not using a vector here?

7) After you call cleanup(), the global variable is zero. Next time you use it, you crash.

8) What dos this mean:
run(Problem &p)
{
int **m_ppiTourMatrix_bestsofar; // declared globally so cleanup() can access
etc...
How can you say this? m_ppiTourMatrix_bestsofar is a local int** inside the
run() funciton. If there is a global variable, this local one hides it.


Post again when these concerns are addressed.
Apr 24 '08 #2
IanWright
179 100+
weaknessforcats,

Thanks for the response. I understand that there are a number of problems, I'm trying to integrate some existing code with my own to test as a prototype, once I can prove that it works then the next step is to tidy it all up...

At present I am trying to figure something out, I can't really post the code because its quite complex and scattered all over. As an example though, I'm trying to change a double **m_ppdPheromoneMatrix_vei to a vector (similar to the suggestion you made about the int** variable).

Hopefully once I've managed to change a few of these simpler ones, I can start changing the others which are a bit more widely used within the program. I'm having a problem however in doing this and would appreciate any suggestions:

Currently:

Expand|Select|Wrap|Line Numbers
  1. double **m_ppdPheromoneMatrix_vei;
  2.  
  3. // elsewhere
  4.  
  5. void function()
  6. {
  7. double **ppdPheromoneMatrix;
  8. ppdPheromoneMatrix = m_ppdPheromoneMatrix_vei;
  9.  
  10. // and is used like so:
  11.  
  12. ppdPheromoneMatrix[iLastNode][iNextNode] += dTmp2;
  13. }
I am trying to change, as per your suggestion to pass around a vector:

Expand|Select|Wrap|Line Numbers
  1. // declared like below in a function
  2.  
  3. std::vector<std::vector<double>> PheromoneMatrix(iSize, std::vector<double>(iSize, 0));
  4.  
  5. // then passed to another function
  6.  
  7. void function(std::vector<std::vector<double>> &PheromoneMatrix)
  8. {
  9.    // Then replace all ppdPheromoneMatrix references with PheromoneMatrix
  10.   PheromoneMatrix[iLastNode][iNextNode] += dTmp2;
  11. }
This however causes my code to no longer work, and I am trying to figure out whether there is a fundamental difference in the way these are treated/used as I would have expected the program to behave exactly the same... instead of accessing an array to instead access the vector items.
Apr 25 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
double **m_ppdPheromoneMatrix_vei;

// elsewhere

void function()
{
double **ppdPheromoneMatrix;
ppdPheromoneMatrix = m_ppdPheromoneMatrix_vei;

// and is used like so:

ppdPheromoneMatrix[iLastNode][iNextNode] += dTmp2;
}
OK, let's assume m_ppdPheromoneMatrix_vei is the address of a 2D matrix of double. Inside the function() you wish to change the array so you can just use m_ppdPheromoneMatrix_vei to identify it. You don't need ppdPheromoneMatrix becuse you never change the address inside ppdPheromoneMatrix. Your code should work without it.

[code=cpp]
double **m_ppdPheromoneMatrix_vei;

// elsewhere

void function()
{

m_ppdPheromoneMatrix_vei[iLastNode][iNextNode] += dTmp2;
}
[/quote]

When you replace thjis with a vector<vector<double> > you have a vector of vectors and not a 2D array that you can fiddle with indiscriminately. That is, the equivalent of
Expand|Select|Wrap|Line Numbers
  1. m_ppdPheromoneMatrix_vei[iLastNode][iNextNode] += dTmp2;
  2.  
won't work unless the element is already in the vector. You can't add an element with a value of dTmp2 to a vector this way.

Your vector sample is not clear that the vectors already constain the elements and you are just changing the values of already existing elements.
Apr 25 '08 #4
IanWright
179 100+
Thanks weaknessforcats, I believe that I've understood it correctly, so I'm just going to take a deeper look to try and find out what's going wrong. The vectors are populated upon their creation so that shouldn't be a problem.
Apr 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: madsgormlarsen | last post by:
Hi I am making a html table builder, and would like every second row to be blue. So for that purpose I need a number that changes on every iteration of a while loop, for exampel so that a value...
9
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background...
3
by: Csaba2000 | last post by:
I apologize for the length of the page below, but I've tried to whittle it down as much as I can. The problem is that Netscape 6.1 on my Win 2K Pro machine is disappearing the final row of the...
2
by: Rachel Suddeth | last post by:
Here is my scenario: I have a few custom controls that I set up on a form and tested setting properties and appearances. Then I added a couple references to the project which add classes I need to...
12
by: Ant | last post by:
Hi, I need to return a value to a methods call from a switch statement nested in the method. How can this be done? int myMethod(int someValue) { switch (someValue) {
9
by: Steve Poe | last post by:
Configuration: PHP 4.3.9 Apache 2.0.52 Hello PHP group, I am a novice to PHP. I have searched through the newsgroup on my issue but I am not sure I know what I am missing.
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.