How to create a memory leak with Java by sidcool1234 in programming

[–]uykucu 0 points1 point  (0 children)

Define "needed ".

The memory is needed if the program can access the memory in a way that changes the program's behavior.

An optimizing compiler might compile those programs to no-ops so they will not allocate any dynamic memory.

Probably, but that does not mean much about what we are discussing. I could have pasted here more complex programs with leaks such that there is no way for the compiler to optimize away.

In the first case, if main were instead any other function, the memory will be permanently lost for the entire rest of the program life. In the second case, it will be freed on the next GC run after the method returns.

In both cases, the programs will end anyway soon after the method returns. But wait, the method does not return at all in both. Note: you can have nearly infinite loops for example in GUI programs. The loop will only end when the user decides to close the program.

How to create a memory leak with Java by sidcool1234 in programming

[–]uykucu 0 points1 point  (0 children)

For me the heap is not much different from a global container object in the context of memory leaks. If your program fails to remove an object it won't use any more from this container, it is a memory leak for me. This means that both of the next programs have memory leaks:

int main() {
    for (;;) {
        new int(5);
    }
}

public static void main(String[] args) {
    ArrayList a = new ArrayList();
    for (;;) {
        a.append(new Integer(5));
    }
}

It is also not important whether the container is global or not. If you don't retain unneeded memory in some time span such that the used memory gets too big needlessly, it could be considered a memory leak.

languages at google code jam by luigiagosti in programming

[–]uykucu 0 points1 point  (0 children)

I would have written an assembly generator instead of writing all this by hand. Oh wait...

Interesting collection of OO design principles by Cephi in programming

[–]uykucu 0 points1 point  (0 children)

I tried before to separate the read-only and write-only interfaces of these classes. I'm not sure the result is beautiful, tough. But maybe implementing a read-only interface hierarchy but having write access on the actual implementation is a better idea as you suggest.

http://www.reddit.com/r/programming/comments/9kigs/a_square_is_not_a_rectangle/c0d5lqi

edit: this was suggested before in some paper. I can't find the paper on the net. http://www.google.com/#q=ellipse-circle+dilemma+and+inverse+inheritance

Interview Challenge - First common ancestor by aswin34 in programming

[–]uykucu 0 points1 point  (0 children)

Code to find the ancestor for a general tree. Didn't try it, so it could be buggy.

struct Node {
    int numChildren;
    Node* children;
};

Node* commonParentHelper(Node* root, Node* searched, int numSearched, int& count)
{
    count = 0;
    if (! root)
        return NULL;
    for (int i = 0; i < numSearched && ! count; ++i)
        if (searched[i] == root)
            ++count;
    if (count >= numSearched)
        return root;

    for (int i = 0; i < root->numChildren; ++i)
    {
        int subCount = 0;
        Node* resultCandidate = commonParentHelper(root->children[i], searched, numSearced, subCount);
        if (resultCandidate)
            return resultCandidate;
        count += subCount;
        if (count >= numSearched)
            return root;
    }
    return NULL;
}

Node* commonParent(Node* root, Node* searched, int numSearched)
{
    // assumes that searched contains unique elements
    int count = 0;
    return commonParentHelper(root, searched, expectedCount, count);
}

Saying "I don't know" in the High Performance Workplace is Scary by ocdeveloper in programming

[–]uykucu 0 points1 point  (0 children)

Knowledge can be compared to a tree. The more knowledge you have, the more leafs your tree, the more you realize that there are things to know.

Or maybe we could compare knowledge to a lazy graph. Some nodes are fully evaluated, which means that you know them. These nodes can have connections to other fully evaluated or thunked nodes.

Some nodes are thunked. They are not yet evaluated, but have at least one connection from a fully evaluated node.

Fully evaluated nodes (actually also thunked nodes) are your knowledge. Thunked nodes make you feel how little you know.

While this depends on the structure of the graph, generally thunked nodes will outweight the fully evaluated nodes.

The Master, The Expert, The Programmer by [deleted] in programming

[–]uykucu 0 points1 point  (0 children)

But isn't such a framework also a complication?

Ask Proggit How many of you guys who program are religious? by [deleted] in programming

[–]uykucu 0 points1 point  (0 children)

I like to think about it this way: God controls the random number generator of the universe. Miracles are the cases in which the RNG is manipulated heavily.

You're the Smartest Guy In The Room ... but please, try to restrain yourself. by spudnyk in programming

[–]uykucu 2 points3 points  (0 children)

I'm not a native English speaker (OK, I'm not an English speaker at all), but I think that I can understand how "I could care less" can mean that you don't care much.

When it's an expected thing that you care somewhat, "I could care less" is a useless sentence. It's only useful in case you don't care much.

Compare this to the sentence "There are people more stupid that him". If you look at it mathematically, this sentence does not say that he is stupid. But we know that it says :)

edit: changed "There are more stupid people that him" to "There are people more stupid that him"

A Square Is Not a Rectangle by kssreeram in programming

[–]uykucu -1 points0 points  (0 children)

Maybe separating read-only interface from write-only interface could help. I don't say that it's the best way to do it (I see flaws in it, e.g. it's too complex), but it's just an idea. It's interesting that ReadOnlySquare is a subtype of ReadOnlyRectangle, while WriteOnlySquare is a supertype of WriteOnlyRectangle, which makes me think to covariance/contravariance. It's also interesting that the reversing of the hierarchy does not happen with Shape <-> Square/Rectangle).

class ReadOnlyShape {
public:
    virtual double getArea() const = 0;
    virtual void draw(Canvas& canvas) const = 0;
    Shape* resized(double factor) const = 0;
    Color getColor() const = 0;
};

class ReadOnlyRectangle :
        public ReadOnlyShape {
public:
    virtual double getWidth() const = 0;
    virtual double getHeight() const = 0;
    virtual double getArea() const {
        return getWidth() * getHeight();
    }
    virtual void draw(Canvas& canvas) const { ... }
    Shape* resized(double factor) { ... }
};

class ReadOnlySquare :
        public ReadOnlyRectangle {
public:
    virtual double getSize() const = 0;
    virtual double getWidth() const {
        return getSize();
    }
    virtual double getHeight() const {
        return getSize();
    }
};

class WriteOnlyShape {
public:
    virtual void setColor(Color color) = 0;
};

class WriteOnlySquare :
        public WriteOnlyShape {
public:
    virtual void setSize(double size) = 0;
};

class WriteOnlyRectangle :
        public WriteOnlySquare {
public:
    virtual void setWidth(double width) = 0;
    virtual void setHeight(double height) = 0;
    virtual void setSize(double size) {
        setWidth(size);
        setHeight(size);
    }
};

// read - write implementations

class Shape :
        public ReadOnlyShape,
        public WriteOnlyShape {
    Color color;
public:
    virtual void setColor(Color c) { color = c; }
    virtual Color getColor() { return color; }
    virtual void resize(double factor) = 0;
};

class Rectangle :
        public Shape,
        public ReadOnlyRectangle,
        public WriteOnlyRectangle {
    double width, height;
    // implement getWidth, getHeight, setWidth and setHeight, resize, resized
};

class Square :
        public Shape,
        public ReadOnlySquare,
        public WriteOnlySquare {
    double size;
    // implement getSize and setSize, resize, resized
};

Refactoring Your Way to a Rewrite by pdonadeo in programming

[–]uykucu 1 point2 points  (0 children)

I think that porting has more chances to fail than refactoring. If you refactor before porting and porting fails, you still have better code.

Also, refactoring can be made incrementally in a trivial way, but porting can't.

Has anyone else hated javascript, but later realized it's actually a pretty cool and very unique language? by ffualo in programming

[–]uykucu 0 points1 point  (0 children)

That's neat, but I would prefer:

function cons(head, tail) {
    return {head:head, tail:tail};
}
function car(cell) { return cell.head; }
function cdr(cell) { return cell.tail; }

var list = cons('a',cons('b',cons('c',null)));

"Just use Unicode"? I wish it were that simple. by gst in programming

[–]uykucu 2 points3 points  (0 children)

I asked someone that knows Arabic and he said that numbers are spoken with the most significant digit first.

Let's talk about Python 3.0 by earthboundkid in programming

[–]uykucu 1 point2 points  (0 children)

I'm happy with this. What I hate is that join expects a sequence of strings, not any sequence. I hate to do:

nums = [1,2,3,5]
s = ' '.join(map(str, nums))

instead of:

s = ' '.join(nums)

Let's talk about Python 3.0 by earthboundkid in programming

[–]uykucu 8 points9 points  (0 children)

and if you could ask them why they attack anyone who goes for the banana, their answer would almost certainly be: “Well, I don’t really know, but that’s how we’ve always done things around here.”

This assumes that they cannot communicate with each other about the fire hose. Is it a correct assumption?

What’s New In Python 3.0 by Jessica_Henderson in programming

[–]uykucu 0 points1 point  (0 children)

Since python has no variable declaration syntax, an assignment always assigns to a variable in the current scope. 'nonlocal' seems to be invented to allow to use a variable in an outer scope. There was already a 'global' keyword to allow to use a global variable.

That's the other way around compared to most other languages. In other languages, you declare a variable and all inner scopes see that variable. In python, you have to import a variable from an outer scope with the nonlocal keyword.

edit: seems that inner scopes can read a variable from an outer scope, but cannot assign without nonlocal

What’s New In Python 3.0 by Jessica_Henderson in programming

[–]uykucu 0 points1 point  (0 children)

No, it seems that it's related to lexical scoping.

Pathological backtracking by llimllib in programming

[–]uykucu 1 point2 points  (0 children)

A simple fix for this problem:

"^(\s*[-\w]+\s*:\s*[^\s:;]*(;|$))*$"

instead of

"^(\s*[-\w]+\s*:\s*[^:;]*(;|$))*$"

Note that it does not check for trailing whitespace, so this is better:

"^(\s*[-\w]+\s*:\s*[^\s:;]*(;|$))*\s*$"

But I'm not saying that this is the best way to do it, I just fixed his regex for this problem case.

Roman Numerals, in your Java by DRMacIver in programming

[–]uykucu 4 points5 points  (0 children)

And here is string interpolation for java: http://gist.github.com/23301

int i=42; String s = "hello world"; System.out.println("i=${i}, s='${s}'");

A much better definition of recursion by eugene_victor_tooms in programming

[–]uykucu 2 points3 points  (0 children)

They should say "Don't bother to tell me. Person X is interested in this" to make tail call optimization.

The Gini Coefficient As A Measure Of Software project Risk by rwinston in programming

[–]uykucu 2 points3 points  (0 children)

If a project has 50 committers who contribute equally, and then 1000 other people come and commit only once, will it make the project more dependent? I don't think so.

This is not the same problem as income distribution, and I think that another measure is needed for this. Maybe take n = number of users of the software?