The "IT DOESN'T WORK!" thread
Moderators: phlip, Moderators General, Prelates
Re: The "IT DOESN'T WORK!" thread
As for what you could use instead, CMake comes to mind because it can also translate back to VS projects.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)
coding and xkcd combined
(Julian/Julian's)
coding and xkcd combined
(Julian/Julian's)
- Whizbang
- The Best Reporter
- Posts: 2238
- Joined: Fri Apr 06, 2012 7:50 pm UTC
- Location: New Hampshire, USA
Re: The "IT DOESN'T WORK!" thread
Sharepoint 2010 is needlessly difficult to work with, Sharepoint Designer 2010 is a joke, and why should I need to know C, javascript, HTML/CSS, and SQL just to make a simple site that displays various dashboards that point at only a few Lists?
I mean, really, is it too much to ask that clicking "New Item" on a "Related Items" web part creates a new item, where the parent ID is pre-filled? Is it too much to ask that when I create a View, and place it in a page, then modify the List's View, that it also modifies the View in the page? And why is it such a pain to create a joined list view? I understand I have to do it in Sharepoint Designer, but why then does Sharepoint Designer more or less break when trying to display a Dataview unles I view it in code only? And why is creating a tabbed navigation menu not a web part that I can just plug into my page, assign a few values, and I have a nice, elegant tabbed menu? I don't want my users to see the Lists in raw form. I want to hide my tables and have them use the views I have inserted into the various pages and dashboards I have created. Also, why does the New Item link on the bottom of a view only create a new item for the default Content Type? Why can't I force them to select a Content Type? Now I have to either hide the New Item link and create a custom link that allows them to select a Content Type, or I have to train them to only use the Ribbon.
I mean, really, is it too much to ask that clicking "New Item" on a "Related Items" web part creates a new item, where the parent ID is pre-filled? Is it too much to ask that when I create a View, and place it in a page, then modify the List's View, that it also modifies the View in the page? And why is it such a pain to create a joined list view? I understand I have to do it in Sharepoint Designer, but why then does Sharepoint Designer more or less break when trying to display a Dataview unles I view it in code only? And why is creating a tabbed navigation menu not a web part that I can just plug into my page, assign a few values, and I have a nice, elegant tabbed menu? I don't want my users to see the Lists in raw form. I want to hide my tables and have them use the views I have inserted into the various pages and dashboards I have created. Also, why does the New Item link on the bottom of a view only create a new item for the default Content Type? Why can't I force them to select a Content Type? Now I have to either hide the New Item link and create a custom link that allows them to select a Content Type, or I have to train them to only use the Ribbon.
Re: The "IT DOESN'T WORK!" thread
I'd also "recommend" SCons, and you could also look into WAF. I've used SCons quite a bit myself, and I've built a lot of CMake projects but never actually used it for my own. SCons is currently my build tool of choice, but its not without its problems.Jplus wrote:As for what you could use instead, CMake comes to mind because it can also translate back to VS projects.
- phlip
- Restorer of Worlds
- Posts: 7550
- Joined: Sat Sep 23, 2006 3:56 am UTC
- Location: Australia
- Contact:
Re: The "IT DOESN'T WORK!" thread
Some neat facts that became relevant for me today:
(1) If you switch branches in your repository, and there's some source file that isn't in the new branch, it will be deleted... but object files compiled from that source will still linger (as the repository doesn't track them, as well it shouldn't).
(2) If you have both "a/b/c.py" and "a/b/c/__init__.py" in your filesystem, and you say "import a.b.c" it will prefer to load the second option over the first.
(3) Python will happily load from a .pyc file if the .py file is missing.
Yeah. That whole mess took me several hours to figure out. A branch for an older version has "a/b/c.py", while the latest version has refactored that into "a/b/c/__init__.py" with a bunch of submodules. Switched branches to the old version to test a thing, but the "a/b/c/__init__.pyc" was left behind, and Python kept trying to load that instead, and got thoroughly confused by the version mismatch (lots of functions weren't where they were expected to be, because of the refactor). It wasn't until I started throwing random errors into a/b/c.py and found it wasn't actually being loaded at all that I realised it was picking it up from somewhere else...
(1) If you switch branches in your repository, and there's some source file that isn't in the new branch, it will be deleted... but object files compiled from that source will still linger (as the repository doesn't track them, as well it shouldn't).
(2) If you have both "a/b/c.py" and "a/b/c/__init__.py" in your filesystem, and you say "import a.b.c" it will prefer to load the second option over the first.
(3) Python will happily load from a .pyc file if the .py file is missing.
Yeah. That whole mess took me several hours to figure out. A branch for an older version has "a/b/c.py", while the latest version has refactored that into "a/b/c/__init__.py" with a bunch of submodules. Switched branches to the old version to test a thing, but the "a/b/c/__init__.pyc" was left behind, and Python kept trying to load that instead, and got thoroughly confused by the version mismatch (lots of functions weren't where they were expected to be, because of the refactor). It wasn't until I started throwing random errors into a/b/c.py and found it wasn't actually being loaded at all that I realised it was picking it up from somewhere else...
Code: Select all
enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵╰(ಠ_ಠ ⚠) {exit((int)⚠);}
Re: The "IT DOESN'T WORK!" thread
Python 3 fixes that, incidentally. It won't look for .pycs in __pycache__ if no .py exists.
Re: de "IT nuh wuk!" tread
Griping about someone else's verification code: If you say "This field should only contain letters", and don't accept that "é" and "ö" are letters, then I'm going to read that as "We don't want your foreign characters all over our 'MURICAN site!".
Not that "First names should only contain letters" is ever a good constraint.
Not that "First names should only contain letters" is ever a good constraint.
The preceding comment is an automated response.
Re: de "IT nuh wuk!" tread
yeah, and whats with the sites that don't accept a plus in an email address?
Re: The "IT DOESN'T WORK!" thread
Aghh every time I think I understand how C/C++ works, bam, it decides to prove me wrong.
common.h:
f1.c:f2.c:
I thought the #ifndef prevented this? I'm compiling everything with a single command so it should work. It's giving me multiple definition errors (for a function) and I don't want to split it in two files. Google has plenty of results but none seems to be the same case.
--
Blame the RFC. Instead of giving up and publishing an usable standard for email addresses (like "OK, we'll allow all unicode characters except these ones, and a maximum of 255 characters, oh and here's a regular expression that matches all valid addresses, feel free to use it") they cling to their horrible failed "standards", forcing developers to just shrug and implement their own (over-zealous) validation.
I dare you to find a single website that lets you sign up as !#$%&'*+-/=?^_`{}|~@(hi_everybody)[IPv6:2001:db8:1ff::a0b:dbd0] . Even better, I dare you to explain to me why we should have comments in email addresses. I don't see comments in file names or domain names.
common.h:
Code: Select all
#ifndef COMMON_H
#define COMMON_H
//should only be included once
#warning "FILE INCLUDED!"
#endif
Code: Select all
#include "common.h"
int main(){return 0;}
Code: Select all
#include "common.h"
Code: Select all
$gcc f1.c f2.c
In file included from f1.c:1:0:
common.h:4:2: warning: #warning "FILE INCLUDED!" [-Wcpp]
#warning "INCLUDED!!"
^
In file included from f2.c:1:0:
common.h:4:2: warning: #warning "FILE INCLUDED!" [-Wcpp]
#warning "INCLUDED!!"
^
I thought the #ifndef prevented this? I'm compiling everything with a single command so it should work. It's giving me multiple definition errors (for a function) and I don't want to split it in two files. Google has plenty of results but none seems to be the same case.
--
speising wrote:yeah, and whats with the sites that don't accept a plus in an email address?
Blame the RFC. Instead of giving up and publishing an usable standard for email addresses (like "OK, we'll allow all unicode characters except these ones, and a maximum of 255 characters, oh and here's a regular expression that matches all valid addresses, feel free to use it") they cling to their horrible failed "standards", forcing developers to just shrug and implement their own (over-zealous) validation.
I dare you to find a single website that lets you sign up as !#$%&'*+-/=?^_`{}|~@(hi_everybody)[IPv6:2001:db8:1ff::a0b:dbd0] . Even better, I dare you to explain to me why we should have comments in email addresses. I don't see comments in file names or domain names.
Re: The "IT DOESN'T WORK!" thread
if the ifdef worked like that, you couldn't use the same includes in multiple source files.
it is only defined within one compilation unit.
it is only defined within one compilation unit.
Re: The "IT DOESN'T WORK!" thread
3rdtry wrote:Aghh every time I think I understand how C/C++ works, bam, it decides to prove me wrong.
common.h:f1.c:Code: Select all
#ifndef COMMON_H
#define COMMON_H
//should only be included once
#warning "FILE INCLUDED!"
#endiff2.c:Code: Select all
#include "common.h"
int main(){return 0;}Code: Select all
#include "common.h"
Uh, you're getting the warnings from the *first* time that the file is included in each .c file. Are you expecting the file to only be included once across all of your .c files? That's not how the preprocessor works; each .c file is processed independently of the other.
Are you saying something like "int x;" in your header file? That can't work for exactly this reason; instead you need to put "extern int x;" in your header file and then choose one .c file and put "int x;" in it.
Re: The "IT DOESN'T WORK!" thread
In terms of what compiles or how it compiles, there's no difference between a single command or multiple commands to explicitly build object files and then link. The only difference is that if you say gcc a.c b.c then it will not output the object files (except perhaps as temporary files during the run) and will run the linker for you.3rdtry wrote:I'm compiling everything with a single command so it should work.
To be explicit, gcc a.c b.c is the same in its effect as
Code: Select all
gcc -c a.c
gcc -c b.c
gcc a.o b.o
rm a.o b.o
They're still separate compilation units, which is why what Rysto and speising said applies.
Re: The "IT DOESN'T WORK!" thread
Rysto wrote:Are you saying something like "int x;" in your header file? That can't work for exactly this reason; instead you need to put "extern int x;" in your header file and then choose one .c file and put "int x;" in it.
It's more like a ostream& operator<<(ostream& outStream, const Vector3D& v) { outStream << "{" << v.x << ", " << v.y << ", " << v.z << "}"; return outStream; }. I "solved" it by adding an "inline", which seems to work for now.
Why is it not complaining about the class I have defined right above that, though? With public methods and everything?
Re: The "IT DOESN'T WORK!" thread
Because classes are types, and types are supposed to be defined in every source file that uses them, unlike functions and variables.3rdtry wrote:Why is it not complaining about the class I have defined right above that, though? With public methods and everything?
Re: The "IT DOESN'T WORK!" thread
I don't like C++
- chridd
- Has a vermicelli title
- Posts: 806
- Joined: Tue Aug 19, 2008 10:07 am UTC
- Location: ...Earth, I guess?
- Contact:
Re: The "IT DOESN'T WORK!" thread
I think that functions with bodies defined inside the class definition are implicitly inline.3rdtry wrote:Why is it not complaining about the class I have defined right above that, though? With public methods and everything?
Usually you should just have the prototype of a function (the function name, return type, and argument types, without the body) in the header file, and the actual definition of the function in a .cpp file. (The exceptions to this rule are functions you specifically want to be inline, and template functions.) Something like this:
Code: Select all
// vector3d.h
class Vector3D {
public:
Vector3D(); // note the semicolon but no body
void do_something();
private:
void do_something_else(); // private functions, too
double x, y, z; // and member variables
};
ostream& operator<<(ostream& outStream, const Vector3D& v);
Code: Select all
// vector3d.cpp
#include "vector3d.h"
// the definitions of member functions refer to them with the syntax ClassName::method_name
Vector3D::Vector3D() {
// do stuff
}
void Vector3D::do_something() {
// ...
}
void Vector3D::do_something_else() {
// ...
}
ostream& operator<<(ostream& outStream, const Vector3D& v) {
outStream << "{" << v.x << ", " << v.y << ", " << v.z << "}";
return outStream;
}
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she(?)(?(?)(?))(?(?(?))(?))(?) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"
Re: The "IT DOESN'T WORK!" thread
3rdtry wrote:Rysto wrote:Are you saying something like "int x;" in your header file? That can't work for exactly this reason; instead you need to put "extern int x;" in your header file and then choose one .c file and put "int x;" in it.
It's more like a ostream& operator<<(ostream& outStream, const Vector3D& v) { outStream << "{" << v.x << ", " << v.y << ", " << v.z << "}"; return outStream; }. I "solved" it by adding an "inline", which seems to work for now.
Why is it not complaining about the class I have defined right above that, though? With public methods and everything?
Here's what you should be doing in general:
common.hpp:
Code: Select all
#ifndef COMMON_H
#define COMMON_H
ostream& operator<<(ostream& outStream, const Vector3D& v);
#endif
common.cpp:
Code: Select all
ostream& operator<<(ostream& outStream, const Vector3D& v) {
outStream << "{" << v.x << ", " << v.y << ", " << v.z << "}";
return outStream;
}
And then compile with
Code: Select all
g++ f1.c f2.c common.cpp
Rule of thumb: if the type of a function is entirely concrete (i.e. not a template), put it in a body file and share the prototype using a header file. In this particular case inlining is actually an acceptable alternative, because the function is very short, but in general I wouldn't recommend it because it will bloat your binary and slow down compilation.
(ninja'd by chridd)
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)
coding and xkcd combined
(Julian/Julian's)
coding and xkcd combined
(Julian/Julian's)
Re: The "IT DOESN'T WORK!" thread
Jplus wrote:Here's what you should be doing in general:
I know, but that's awkward and impractical. It's just a short class with a couple of overloaded operators. I do have headers and bodies for the other two larger classes.
I used to just name my files ".cpph" and #include all them from my main file anyway. I stopped because netbeans didn't seem to like it too much, and it made me feel dirty (but worked fine).
Re: The "IT DOESN'T WORK!" thread
EvanED wrote:In terms of what compiles or how it compiles, there's no difference between a single command or multiple commands to explicitly build object files and then link. The only difference is that if you say gcc a.c b.c then it will not output the object files (except perhaps as temporary files during the run) and will run the linker for you.3rdtry wrote:I'm compiling everything with a single command so it should work.
To be explicit, gcc a.c b.c is the same in its effect as.Code: Select all
gcc -c a.c
gcc -c b.c
gcc a.o b.o
rm a.o b.o
QFT.
@3rdtry
IMHO, it helps to remember that compilation and linking are quite separate operations. The compiler works on one .c or .cpp file at a time and is totally oblivious to what's defined in all the other translation units of your project.
EvanED wrote:They're still separate compilation units, which is why what Rysto and speising said applies.
Aka translation units.
3rdtry wrote:Jplus wrote:Here's what you should be doing in general:
I know, but that's awkward and impractical. It's just a short class with a couple of overloaded operators. I do have headers and bodies for the other two larger classes.
I used to just name my files ".cpph" and #include all them from my main file anyway. I stopped because netbeans didn't seem to like it too much, and it made me feel dirty (but worked fine).
Yeah, don't do that. It can work, but it's generally more efficient to organise your headers better and to #include them only in the files that need access to the macros and declarations they contain. The temptation is to think of the main file as some sort of master file for the program. Sure, it contains main(), your program's entry-point, but apart from that, there's nothing particularly special about it; as far as the compiler's concerned it's just another translation unit.
In traditional C, header files only contain macros and declarations; they never contain function or variable definitions. That rule gets bent a bit with some modern coding techniques and with C++ etc, but it's still a Good Idea to try to stick to that principle as much as is practical. So for each .c or .cpp file you should have a header that declares all the prototypes and other stuff that that translation unit needs to make public and only include that header file into the .c or .cpp files that need it.
I guess I ought to mention that I don't do C++, but I have been programming in C since the early 1980s, and so some of my ideas may be a tad antiquated.

-
- Name Checks Out On Time, Tips Chambermaid
- Posts: 1075
- Joined: Fri Jan 01, 2010 5:17 am UTC
- Location: couldn't even find coffee in copenhagen
Re: The "IT DOESN'T WORK!" thread
Embarassingly basic lua compilation error.
My code:
The error returned:
Google is not helping me. I had the same kind of problem last semester while experimenting with Lua, and google did not help then either. I copy and paste example code into empty .lua files and get the same error. Is there something wrong with my lua compiler (reinstalling did not help)? Am I forgetting something?
My code:
Code: Select all
Vector = {}
Vector.__index = Vector
function Vector:new ( )
local o = setmetatable({}, Vector)
self.__index = self
return o
end
function Vector:setComponents ( a, b, c )
self.i = a
self.j = b
self.k = c
end
function Vector:dotProduct ( u )
return self.i * u.i + self.j * u.j + self.k + u.k
end
function Vector:magnitude()
return math.sqrt(self:dotProduct(self))
end
The error returned:
Code: Select all
./vector.lua: line 1: Vector: command not found
./vector.lua: line 2: Vector.__index: command not found
./vector.lua: line 5: syntax error near unexpected token `local'
./vector.lua: line 5: ` local o = setmetatable({}, Vector)'
Google is not helping me. I had the same kind of problem last semester while experimenting with Lua, and google did not help then either. I copy and paste example code into empty .lua files and get the same error. Is there something wrong with my lua compiler (reinstalling did not help)? Am I forgetting something?
- chridd
- Has a vermicelli title
- Posts: 806
- Joined: Tue Aug 19, 2008 10:07 am UTC
- Location: ...Earth, I guess?
- Contact:
Re: The "IT DOESN'T WORK!" thread
Are you actually using the Lua compiler? That looks like the errors I get if I try to run your program as a shell script. (Typing ./vector.lua at the command prompt would cause this; it needs to know what to do with the file.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she(?)(?(?)(?))(?(?(?))(?))(?) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"
Re: The "IT DOESN'T WORK!" thread
Yeah, you're not invoking it properly. If you use "lua vector.lua" it should run, or you can put something like "#!/usr/bin/env lua" at the top of the file to run it the way you're trying to.
Re: The "IT DOESN'T WORK!" thread
You wouldn't be the first person to make a similar mistake. Many Python scripts start out with "import blah", and if you have ImageMagick installed, you'll actually have an "import" command. It waits for a mouse click and takes a screenshot with that name. Guess what prompted me to learn that one . 

Re: The "IT DOESN'T WORK!" thread
Nyktos wrote:[...] or you can put something like "#!/usr/bin/env lua" at the top of the file to run it the way you're trying to.
Note that you also need to chmod +x the file to make that work.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)
coding and xkcd combined
(Julian/Julian's)
coding and xkcd combined
(Julian/Julian's)
Re: The "IT DOESN'T WORK!" thread
I'm assuming he already did that, or else he'd be getting a permission denied error instead of what he is getting.Jplus wrote:Nyktos wrote:[...] or you can put something like "#!/usr/bin/env lua" at the top of the file to run it the way you're trying to.
Note that you also need to chmod +x the file to make that work.
-
- Name Checks Out On Time, Tips Chambermaid
- Posts: 1075
- Joined: Fri Jan 01, 2010 5:17 am UTC
- Location: couldn't even find coffee in copenhagen
Re: The "IT DOESN'T WORK!" thread
About two minutes after I posted that, I discovered what I did wrong and fixed it. And thought I deleted my post.
I needed to add #!/usr/bin/lua.
And that is not the only embarrassing error in the code I posted.
I needed to add #!/usr/bin/lua.
And that is not the only embarrassing error in the code I posted.
Re: The "IT DOESN'T WORK!" thread
Java
Please help, I have spent weeks just ignoring that this does not work. No errors/exceptions it just does not change the array's length.
Testing code:
Output:
I am starting to wonder if I have misunderstood something fundamental.
Please help, I have spent weeks just ignoring that this does not work. No errors/exceptions it just does not change the array's length.
Code: Select all
public static void doubleArraySize(Entry[] a) {
a = Arrays.copyOf(a, a.length * 2);
}
Testing code:
Code: Select all
Entry[] fish = new Entry[5];
System.out.println(fish.length);
MyClass.doubleArraySize(fish);
System.out.println(fish.length);
fish = Arrays.copyOf(fish, fish.length * 2);//Why does this work?
System.out.println(fish.length);
Output:
Code: Select all
5
5
10
I am starting to wonder if I have misunderstood something fundamental.
Re: The "IT DOESN'T WORK!" thread
Arrays are reference types -- that won't work for the same reason thatstickler wrote:I am starting to wonder if I have misunderstood something fundamental.
Code: Select all
static void swap(Object a, Object b) {
Object tmp = a;
a = b;
b = tmp;
}
won't help you. (That could probably be "better-written" with generics but that distracts from the point.)
Do you understand why your code doesn't work or should I write more?
Re: The "IT DOESN'T WORK!" thread
Thank-you!
I now have no idea how java methods work.
I thought that once you passed a variable into a method the method could change it without limit. (I suppose it can - it just can't change the reference).
Why doesn't the compiler warn me that line with the arrow is ignored?
I now have no idea how java methods work.
I thought that once you passed a variable into a method the method could change it without limit. (I suppose it can - it just can't change the reference).
Code: Select all
String a = new String("potato");
System.out.println(a);//potato
String b = new String("egg");
System.out.println(b);//egg
a = b;
System.out.println(a);//egg
makeCheese(a);
System.out.println(a);//egg
}
public static void makeCheese(String str) {
String c = new String("cheese");
str = c; //< ---
}
Why doesn't the compiler warn me that line with the arrow is ignored?
Re: The "IT DOESN'T WORK!" thread
Yep, exactly. And because you can't resize an existing array, the only thing you can do is change the reference to point to a different, bigger one. (Incidentally, this is presumably why the array doesn't have a resize() member.)stickler wrote:(I suppose it can - it just can't change the reference).
Dunno. Don't expect compilers to warn about everything they "could"; I'm not familiar enough with when Java compilers do and don't give you a warning.Code: Select all
public static void makeCheese(String str) {
String c = new String("cheese");
str = c; //< ---
}
Why doesn't the compiler warn me that line with the arrow is ignored?
- Xeio
- Friends, Faidites, Countrymen
- Posts: 5091
- Joined: Wed Jul 25, 2007 11:12 am UTC
- Location: C:\Users\Xeio\
- Contact:
Re: The "IT DOESN'T WORK!" thread
Well, it's not ignored per say. It does re-write str to be "cheese". Except that str is a local variable.stickler wrote:Why doesn't the compiler warn me that line with the arrow is ignored?
More accurately, it would probably be relevant to warn that you have an unused assignment (and, for that matter that the value passed into the method is un-used). I don't know if there is an IDE or toolset that offers those particular warnings for Java.
It seems Eclipse may warn you about unused variables, but I'm not sure if that applies to parameter values or if it supports unused assignments. Netbeans appears to offer at least the latter, and possibly both warnings.
- Yakk
- Poster with most posts but no title.
- Posts: 11073
- Joined: Sat Jan 27, 2007 7:27 pm UTC
- Location: E pur si muove
Re: The "IT DOESN'T WORK!" thread
Welcome to the world of pseudo-pointers!
Assignment changes what the reference refers to, while use implicitly changes what the reference refers to.
Thus demonstrating two things. First, English is a poor language to describe problems in CS. Second, so is Java.
Assignment changes what the reference refers to, while use implicitly changes what the reference refers to.
Thus demonstrating two things. First, English is a poor language to describe problems in CS. Second, so is Java.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR
Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
Re: The "IT DOESN'T WORK!" thread
Sorry, can't help it, but...

(yeah, I know that's not helpful...)

(yeah, I know that's not helpful...)
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)
coding and xkcd combined
(Julian/Julian's)
coding and xkcd combined
(Julian/Julian's)
Re: The "IT DOESN'T WORK!" thread
Java's reference behavior actually makes sense from the virtual machine's POV. You cannot have C++ like pointers and references in a managed language as they are inherently unsafe. If Java had C++ references each referenced local variable would have to be heap allocated.
- Yakk
- Poster with most posts but no title.
- Posts: 11073
- Joined: Sat Jan 27, 2007 7:27 pm UTC
- Location: E pur si muove
Re: The "IT DOESN'T WORK!" thread
You cannot have strong (owning) references to auromatic/stack/bounded lifetime objects.
There is nothing about 'safety' in the previous sentence, because that is an orthogonal issue. Java chose to have owning references and block literal bounded lifetime instances. You could easily track object lifetime separately and throw on illegal dereference for soft references (strong would behave like Java references). No problem.
Well, multi threading gets fun, I suppose.
There is nothing about 'safety' in the previous sentence, because that is an orthogonal issue. Java chose to have owning references and block literal bounded lifetime instances. You could easily track object lifetime separately and throw on illegal dereference for soft references (strong would behave like Java references). No problem.
Well, multi threading gets fun, I suppose.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR
Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
- Xeio
- Friends, Faidites, Countrymen
- Posts: 5091
- Joined: Wed Jul 25, 2007 11:12 am UTC
- Location: C:\Users\Xeio\
- Contact:
Re: The "IT DOESN'T WORK!" thread
Eh, doesn't Java store reference types on the heap anyway? The actual pointer/reference is the only thing that could be stack allocated.korona wrote:Java's reference behavior actually makes sense from the virtual machine's POV. You cannot have C++ like pointers and references in a managed language as they are inherently unsafe. If Java had C++ references each referenced local variable would have to be heap allocated.
I don't quite know if there is a technical reason stopping Java from having something similar to C#'s ref/out keywords though. That may be an existing bytecode limitation, or they may just not care to implement it.
- Xenomortis
- Not actually a special flower.
- Posts: 1410
- Joined: Thu Oct 11, 2012 8:47 am UTC
Re: The "IT DOESN'T WORK!" thread
I doubt the Java spec specifies where/how the object is actually allocated.
It may be a JVM decides that something can be safely stack allocated and has a mechanism to facilitate that.
It may be a JVM decides that something can be safely stack allocated and has a mechanism to facilitate that.

- Xeio
- Friends, Faidites, Countrymen
- Posts: 5091
- Joined: Wed Jul 25, 2007 11:12 am UTC
- Location: C:\Users\Xeio\
- Contact:
Re: The "IT DOESN'T WORK!" thread
Hrmmm, yea, I don't see any specific mention. Shows what I get for thinking people on stack overflow might know what they're saying.
Still, you can always wrap a class inside a dummy 'pointer' class. There's just no language feature to do that automatically. It doesn't matter if the data lives on the stack or heap (or a register) as far as passing it around.
Still, you can always wrap a class inside a dummy 'pointer' class. There's just no language feature to do that automatically. It doesn't matter if the data lives on the stack or heap (or a register) as far as passing it around.
Re: The "IT DOESN'T WORK!" thread
stickler wrote:Code: Select all
public static void makeCheese(String str) {
String c = new String("cheese");
str = c; //< ---
}
Incidentally, I'm having this same "problem" now. Is there any way to do that in java? Change an input parameter to "point" to a new object?
Basically we have:
Code: Select all
int[][] newComplicatedMatrix(){
int[][] M = new int[100][100];
[...]
return M;
}
int doStuff(int[][] inoutMatrix)
{
[...]
inoutMatrix = newComplicatedMatrix(); //<--- this is wrong
[more changes to inout matrix]
return 25;
}
And I need to get it working with the least amount of changes. I could copy the result of newComplicatedMatrix to inoutMatrix using a System.arrayCopy, or change newComplicatedMatrix to receive a parameter instead of creating a new one, is there any other way?
I know, I know, we should be using ArrayLists (probably inside a Matrix class) instead of raw arrays... but that's what we have now and we can't change it.
(Still, it's 2014, why can't I return two or three objects directly? Parameters should always be inputs)
Re: The "IT DOESN'T WORK!" thread
In Java, a function cannot change the object to which a caller's argument points. Period.
If you want to change the pointed-to object, you can either have the function return a reference to the new target and then assign it (e.g. o = foo(o)) or you can make a stupid wrapper class with a single field, use an instance of that object instead of a raw reference, and then change that. Example:
If you want to change the pointed-to object, you can either have the function return a reference to the new target and then assign it (e.g. o = foo(o)) or you can make a stupid wrapper class with a single field, use an instance of that object instead of a raw reference, and then change that. Example:
Code: Select all
class StringHolder {
public string v;
}
void foo(StringHolder myparam) {
System.out.println("Original: " + myparam.v);
myparam.v = "changed";
}
void bar() {
StringHolder myobj = new StringHolder();
myobj.v = "original";
foo(myobj);
System.out.println("Changed: " + myobj.v);
}
- Xeio
- Friends, Faidites, Countrymen
- Posts: 5091
- Joined: Wed Jul 25, 2007 11:12 am UTC
- Location: C:\Users\Xeio\
- Contact:
Re: The "IT DOESN'T WORK!" thread
3rdtry wrote:(Still, it's 2014, why can't I return two or three objects directly? Parameters should always be inputs)
You can, just not in Java. C#, Python, Go, etc. can all do it.
Though you return an array of object if you really want.
Who is online
Users browsing this forum: No registered users and 5 guests