If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

First, let's look at this above. This code causes a memory leak as you never clean up the memory. If you repeatedly call linprog1() in your main application, you will exhaust the heap of free memory. Nowhere is a delete[] issued for any of those calls to new[]. Then you have this in your solverr() function:

Again, no call to delete[] to clean up the memory. If you were to call this function repeatedly throughout a program, the memory/resource leak adds up here.

In modern C++ programming, the use of proper container classes instead of the "new[]/delete[]" combination is what is done to implement dynamic arrays. Containers such as std::vector, or if using MFC, CArray, supersedes the usage of using new[]/delete[] in this way. Unless what is written is some sort of memory allocator or you're writing your own container class, new[]/delete[] should not be used if your goal is a dynamic array, unless you can justify its usage.

The reason why container classes are better than new[]/delete[]:

1) The memory allocation and deallocation is automatically done without you having to do anything yourself. This allows you to create the object that wraps a dynamic array, use it, and when the function or functional block exits, the object is destroyed via destructor. No need for you to call delete[] anywhere.

2) If your function returns for any reason, whether it is through a return statement, the end of the function is reached, or an exception is thrown causing the code to leave the function, the container class's destructor cleans up the memory. Using new[]/delete[], you need to stick a delete[] for every single return point in your program, and you also have to write a catch() block for any every exception that may be thrown, since you have to delete[] the memory inside the catch blocks (the latter case makes it almost a requirement to use a container class as opposed to new[]/delete[]).

So which sounds easier and safer? The new[]/delete[] or the container classes?

Another problem -- you're calling functions without caring if what is returned is valid. For example:

Code:

lp = glp_create_prob();

You then use "lp" assuming that is valid. How do you know that lp is not NULL or something that can't really be used?

Also, you should name your variables with descriptive names, not "x" and "p" as you've done in that function.

Last, are you debugging your code using the debugger? Did you unit test the function you wrote, or did you write the whole thing and expected it to work right out of the box?

Eliminating the comments and code that just does printing, the above does not cause a memory leak. Now it still doesn't address the issues I mentioned concerning not checking your return values from various functions.

You need to start using proper container classes such as vector instead of the new[] / delete[], then all or most of that pointer usage is not necessary at all and the program becomes safer and less prone to memory related bugs. As a matter of fact, I bet that even the linprog function parameters itself need not be pointers, but references to vectors (as my code shows). The only thing that needs a pointer are the third-party library functions, and I've demonstrated above how to use vector to "fool" those functions into using a vector as a parameter instead of a pointer (pass a pointer to the first element of the vector).

Re: "The system cannot find the path specified"

This is all very useful, and the std::Vector (or rather arma::Col) that I am now using thanks to you works great!

Not surprising. A lot of errors become magically eliminated once a vector is used -- the memory leaks clear up, and the hard-to-maintain pointers also become a moot point.

The only thing that can't be corrected by using vector is if you access an element out-of-bounds using the [] operator. For that, vector has the at() function, but that requires you to change your code to call at() instead of using [].

I actually tried to do it this way earlier, but did not add the [0] where I called the variables. Why is this zero necessary? What is it?

The vector has an internal array. To access the internal array, you need to get a pointer to the first element of the array. That's why you need to get the address of the first element. (The vector's [] is actually an overloaded operator that gets to the array element).

The CArray is the same thing as vector in terms of what they accomplish. All of that code you had now with new[]/delete[] is already done by CArray and vector internally (but with smarts added to it that you may not have even considered). So basically all you were doing is copying what vector/CArray was doing, but you were doing things the naive way while those container classes are much smarter and use new[]/delete[] and free-store properly, safely, and optimally.

Re: "The system cannot find the path specified"

You're passing these parameters by value. This is not optimal, or even correct.

You should be passing these by reference or const reference. There are only a few situations where an object must be passed by value, otherwise you should always pass by reference or const reference. Take a look more closely at my edited post above.

Passing by value is not optimal, as a copy of the data has to be made to the function. Also, passing these objects by value may be an error, especially if your class doesn't have a proper coded copy/assignment operator, and if you're expecting the function to retain the results in the passed-in objects.

Re: "The system cannot find the path specified"

Alright, that is very good to know, I thought the variables were only to be called in that way when you desired to modify their value within the function.

The function now runs, and the Linear Program that it is supposed to create runs fine. The only problem is that the output of the function doesn't make sense, which makes me think that I am making an error in my code somewhere, the code is

The variables sent to the function are read from row/column 1 rather than 0 which is why I have added ones in certain places, however I am wondering whether I am sending through a variable too many or too few in one place or another... The reason I think so is because of this line:

Code:

glp_load_matrix(lp, A_sp.n_rows - 1, &ia(1), &ja(1), &ar(1));

this line should have a second value of A_sp.n_rows, but doesn't execute properly unless I subtract one from it.

Re: "The system cannot find the path specified"

I have no idea what a "Col" or "Mat" is. These are proprietary types as they are not standard C++ types or MFC types.

however I am wondering whether I am sending through a variable too many or too few in one place or another....
...

Instead of wondering what may be happening, you need to debug your code using the debugger to find out where things go wrong. There is no need to be using cout statements to debug or random guesses and trial/error -- the Visual C++ compiler comes with one of the most powerful debuggers created for PC based C++ systems.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.