From: Praetorian on
On Feb 4, 5:01 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
> On 4 Feb, 09:34, "bla&#382; " <blaz.kr...(a)siol.net> wrote:
>
> > Hello to you all,
>
> > i've been struggling for a while with my first mex file. Basically it is a simlpe program with 2 for loops, one along i and one along j. If some condition is satisfied i need to remember the specific i,j pair. So in the end i want to have a [? x 2] matrix. The complete code is written bellow. I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
>
> Is this C or C++? If C++, use the std::stack<> container to store
> intermediate results, and then allocate the necessary space for
> the return variable at the very end, when you know how many
> elements will be returned.
>
> Apart from that, *never* base variable names on the single letters
> 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> alone or in conjunction with only numbers. At some point you will
> inevitably confuse them with the digits 1 or 0. *Only* use those
> letters in textual labels, where it is obvious that they are letters
> and not  numbers.
>
> Depending on the font settings in your code editor, you might not
> be able to see the difference between the letters and numbers,
> making for bugs that are extremely hard to track down, unless
> it is obvious from the context what they are.
>
> Rune

Why std::stack? IMHO std::vector or std::deque would be a much better
choice in his case since they'll allow him to retrieve the matches in
the order they were found.

- Ashish
From: Rune Allnor on
On 4 Feb, 15:22, Praetorian <ashish.sadanan...(a)gmail.com> wrote:
> On Feb 4, 5:01 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
>
>
>
>
>
> > On 4 Feb, 09:34, "bla&#382; " <blaz.kr...(a)siol.net> wrote:
>
> > > Hello to you all,
>
> > > i've been struggling for a while with my first mex file. Basically it is a simlpe program with 2 for loops, one along i and one along j. If some condition is satisfied i need to remember the specific i,j pair. So in the end i want to have a [? x 2] matrix. The complete code is written bellow. I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
>
> > Is this C or C++? If C++, use the std::stack<> container to store
> > intermediate results, and then allocate the necessary space for
> > the return variable at the very end, when you know how many
> > elements will be returned.
>
> > Apart from that, *never* base variable names on the single letters
> > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > alone or in conjunction with only numbers. At some point you will
> > inevitably confuse them with the digits 1 or 0. *Only* use those
> > letters in textual labels, where it is obvious that they are letters
> > and not  numbers.
>
> > Depending on the font settings in your code editor, you might not
> > be able to see the difference between the letters and numbers,
> > making for bugs that are extremely hard to track down, unless
> > it is obvious from the context what they are.
>
> > Rune
>
> Why std::stack?

Because of the semantics. The OP clearly does not know a lot
of C(++), so any talk about vectors would likely confuse him
about pre-allocating space etc, the same problems he tries
to solve with the code he posted.

> IMHO std::vector or std::deque would be a much better
> choice in his case since they'll allow him to retrieve the matches in
> the order they were found.

The concept - and semantics - of a stack is that one does not
care about memory allocations. They are taken care of internally.
Once you know the number of elements present, you can allocate
the necessary space in the vector, and fill in the elements
in the order they are popped from the stack, whihf is the reverse
order of the one theyr were found. Start filling in the elements
at the end of the vector, and work your way towards the start.

The end result is a list of hits, in the order they were found.

Rune
From: Jan Simon on
Dear bla&#382;!

> Apart from that, *never* base variable names on the single letters
> 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> alone or in conjunction with only numbers.

Although this does not matter your question and sounds trivial - take Rune's advice seriously!

Jan
From: Praetorian on
On Feb 4, 7:30 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
> On 4 Feb, 15:22, Praetorian <ashish.sadanan...(a)gmail.com> wrote:
>
>
>
> > On Feb 4, 5:01 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
>
> > > On 4 Feb, 09:34, "bla&#382; " <blaz.kr...(a)siol.net> wrote:
>
> > > > Hello to you all,
>
> > > > i've been struggling for a while with my first mex file. Basically it is a simlpe program with 2 for loops, one along i and one along j. If some condition is satisfied i need to remember the specific i,j pair. So in the end i want to have a [? x 2] matrix. The complete code is written bellow.. I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
>
> > > Is this C or C++? If C++, use the std::stack<> container to store
> > > intermediate results, and then allocate the necessary space for
> > > the return variable at the very end, when you know how many
> > > elements will be returned.
>
> > > Apart from that, *never* base variable names on the single letters
> > > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > > alone or in conjunction with only numbers. At some point you will
> > > inevitably confuse them with the digits 1 or 0. *Only* use those
> > > letters in textual labels, where it is obvious that they are letters
> > > and not  numbers.
>
> > > Depending on the font settings in your code editor, you might not
> > > be able to see the difference between the letters and numbers,
> > > making for bugs that are extremely hard to track down, unless
> > > it is obvious from the context what they are.
>
> > > Rune
>
> > Why std::stack?
>
> Because of the semantics. The OP clearly does not know a lot
> of C(++), so any talk about vectors would likely confuse him
> about pre-allocating space etc, the same problems he tries
> to solve with the code he posted.
>
> > IMHO std::vector or std::deque would be a much better
> > choice in his case since they'll allow him to retrieve the matches in
> > the order they were found.
>
> The concept - and semantics - of a stack is that one does not
> care about memory allocations. They are taken care of internally.
> Once you know the number of elements present, you can allocate
> the necessary space in the vector, and fill in the elements
> in the order they are popped from the stack, whihf is the reverse
> order of the one theyr were found. Start filling in the elements
> at the end of the vector, and work your way towards the start.
>
> The end result is a list of hits, in the order they were found.
>
> Rune

I know we're arguing personal programming preferences here, but vector
doesn't require you to reserve memory either. In fact, in all
likelihood, your STL stack implementation is just an adapter
implemented around a vector object. I do agree with you that because
of the very limited interface that stack exposes it might be easier
for a beginner to work with, but vector isn't that hard to get a grasp
of either.

- Ashish
From: Rune Allnor on
On 4 Feb, 18:46, Praetorian <ashish.sadanan...(a)gmail.com> wrote:
> On Feb 4, 7:30 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
>
>
>
>
>
> > On 4 Feb, 15:22, Praetorian <ashish.sadanan...(a)gmail.com> wrote:
>
> > > On Feb 4, 5:01 am, Rune Allnor <all...(a)tele.ntnu.no> wrote:
>
> > > > On 4 Feb, 09:34, "bla&#382; " <blaz.kr...(a)siol.net> wrote:
>
> > > > > Hello to you all,
>
> > > > > i've been struggling for a while with my first mex file. Basically it is a simlpe program with 2 for loops, one along i and one along j. If some condition is satisfied i need to remember the specific i,j pair. So in the end i want to have a [? x 2] matrix. The complete code is written bellow. I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
>
> > > > Is this C or C++? If C++, use the std::stack<> container to store
> > > > intermediate results, and then allocate the necessary space for
> > > > the return variable at the very end, when you know how many
> > > > elements will be returned.
>
> > > > Apart from that, *never* base variable names on the single letters
> > > > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > > > alone or in conjunction with only numbers. At some point you will
> > > > inevitably confuse them with the digits 1 or 0. *Only* use those
> > > > letters in textual labels, where it is obvious that they are letters
> > > > and not  numbers.
>
> > > > Depending on the font settings in your code editor, you might not
> > > > be able to see the difference between the letters and numbers,
> > > > making for bugs that are extremely hard to track down, unless
> > > > it is obvious from the context what they are.
>
> > > > Rune
>
> > > Why std::stack?
>
> > Because of the semantics. The OP clearly does not know a lot
> > of C(++), so any talk about vectors would likely confuse him
> > about pre-allocating space etc, the same problems he tries
> > to solve with the code he posted.
>
> > > IMHO std::vector or std::deque would be a much better
> > > choice in his case since they'll allow him to retrieve the matches in
> > > the order they were found.
>
> > The concept - and semantics - of a stack is that one does not
> > care about memory allocations. They are taken care of internally.
> > Once you know the number of elements present, you can allocate
> > the necessary space in the vector, and fill in the elements
> > in the order they are popped from the stack, whihf is the reverse
> > order of the one theyr were found. Start filling in the elements
> > at the end of the vector, and work your way towards the start.
>
> > The end result is a list of hits, in the order they were found.
>
> > Rune
>
> I know we're arguing personal programming preferences here, but vector
> doesn't require you to reserve memory either. In fact, in all
> likelihood, your STL stack implementation is just an adapter
> implemented around a vector object. I do agree with you that because
> of the very limited interface that stack exposes it might be easier
> for a beginner to work with, but vector isn't that hard to get a grasp
> of either.

Again, the problem the OP presented indicates to me that
the different semantics concerning vectors and stacks midht
make a significant difference.

As for stacks being possible to implement in terms of vectors,
yes, you are right. It is possible. But even so, people prefer
to use stacs as opposed to vectors wherever the semantics of
the problem inodcates a stack might be appropriate.

I know this is a no-no what matlab is concerned, but the main
focus in C++ these days is to write code that makes sense to
the human being. The rationale is that a program is read more
often that it is written. So it pays in the long run to write
code that makes sense and is understandable to the human being.

Rune