Monday, April 1, 2019

c++ - iostream linker error



I have some old C code that I would like to combine with some C++ code.



The C code used to have has the following includes:




#include 
#include
#include
#include "mysql.h"


Now I'm trying to make it use C++ with iostream like this:



#include 

#include
#include
#include
#include "mysql.h"


But I keep getting the following linker errors when I compile:




[Linker error] undefined reference to `std::string::size() const'




[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::ios_base::Init::Init()'



[Linker error] undefined reference to `std::ios_base::Init::~Init()'




ld returned 1 exit status




How do I resolve this?



Edit: My compiler is Dev-C++ 4.9.9.2


Answer



The C string.h header and the C++ string header are not interchangeable.




Overall, though, your problem is that the file is getting properly compiled, but the wrong runtime library is getting linked in.



Dev-C++ uses GCC. GCC can correctly determine the language in a file based on file extension, but won't link the right runtime library in unless you specifically ask it to (-lstdc++ at the command line). Calling GCC as "g++" (or, in your case, "mingwin32-g++") will also get the right language and will link the needed library.


javascript - Why does Math.cos(90 * Math.PI/180) yield 6.123031769111... and not zero?

I convert degrees to radians (degrees * Math.PI/180) but why does the following:



Math.cos(90 * Math.PI/180)


yield 6.123031769111... and not zero?



I'm trying to perform 2D rotations uses matrixes and the results are completely out of whack.

php - SQL-safe method




Context: I'm trying to convince a friend to switch to using parameterized queries to prevent SQL injections and other malicious attempts as that is the standards these days but he has this mentality of "If it's not broken, don't fix it."




Here's the code he currently uses:



function sql_safe($text) {
return str_replace("'", "''", $text);
}


Is there a way for me to break this function to illustrate to him that this approach is not advisable anymore? I've been trying but I can't break it myself so now I'm turning to you guys for help.




Additional Info



It's being used as a general means to protect the system from SQL injections so that user inputs are escaped properly. But I feel like his approach could break at certain scenarios which I haven't figured out yet.


Answer



Here's your code:



function sql_safe($text) {
return str_replace("'", "''", $text);
}

echo "SELECT * FROM db WHERE field = '" . sql_safe($argv[1]) . "';\n";


And here's the most obvious way of breaking it:



$ php ./x.php "\' OR TRUE; -- MySQL"
SELECT * FROM db WHERE field = '\'' OR TRUE; -- MySQL';


has covered the topic of SQL injection extensively over the years. See for example Can I protect against SQL Injection by escaping single-quote and surrounding user input with single-quotes? . There's a neat trick in there that exploits "maximum length of string" to truncate just one of the replacement ''s.



c++ - Definition of variable inside loop





I was looking for an answer for my questions on many pages but I couldn't find it.



In this case we are defining variable and reinitialaze it in every loop:



while(1)
int k = 7;


In this case we are defining variable before the loop and reinitialaze it in every loop.




int k;
while(1)
k = 7;


There is any advantages or disadvantages of using both methods? Or maybe it don't make a difference?


Answer



The difference is in terms of scope of the variable.



In the first case, once the while loop ends, the variable k cannot be accessed.




In the second case, the variable k can be accessed out of the while loop.



In both cases, the variable is defined on the stack (or as TartanLlama points out, they could be allocated in registers) and so there is no difference in terms of performance.



However, the example you've used is wrong in the case that the while loop will never end. I'm guessing this is just a piece of dummy code to explain the situation.


plot explanation - At which dream level did Inception end?

In the ending of Inception (2010), there is some doubt that Cobb is in the real world. But if he is not in the real world then at which level of the dream he is in?


Starting at the presumed 'real world' as the first level of the story, I think the final scene it's the fifth level (limbo) because at the fourth level they save Fischer, but I need clarification.


Answer


None of the different means you can use to guess whether Cobb is in a dream or not at the end of the movie are completely reliable.


The spinning top has huge issues - the fact that it was Mal's totem, that it appears to act in a simplistic way that perhaps other people could dream, hence invalidating its usefulness - and of course the fact that we are never even shown what the top does at the end of the movie.


I am unconvinced by how completely watertight the wedding ring theory (referenced in @Eoin's answer), as it seems possible for Cobb to dream the wedding ring when he thinks he is in a dream and not otherwise. Yes it is an interesting theory, but whilst it is certainly a deliberate action from the writers/director - the meaning is not completely clear. Cobb clearly seems to treat the spinning top as his totem (flawed or not), as he uses it when flustered by his dream in Mombassa. So yes, it could be a clue, it could even be his true or second totem - but it is still not completely reliable in my opinion.


So is Cobb in a dream at the end? I don't think we can tell. Unlike the other answer, I like the ambiguity in the movie. If pushed to give an answer I would say that the fact that he is such an experienced shared dreamer, it seems likely that he would eventually work out if he is in a dream or not - so I guess he is likely to be in the real world, and the wedding ring therefore might be a true clue to that.


So to answer the original question, if this is not the real world, what level is this? Well if the top level of the story is not the real world, and is in fact another dream, then 'who knows' is the only real answer. The question has no real meaning as there are possibly untold levels of dreaming we have no knowledge of.


A quick and easy way to join array elements with a separator (the opposite of split) in Java




See Related .NET question



I'm looking for a quick and easy way to do exactly the opposite of split

so that it will cause ["a","b","c"] to become "a,b,c"



Iterating through an array requires either adding a condition (if this is not the last element, add the seperator) or using substring to remove the last separator.



I'm sure there is a certified, efficient way to do it (Apache Commons?)



How do you prefer doing it in your projects?


Answer



Using Java 8 you can do this in a very clean way:




String.join(delimiter, elements);


This works in three ways:



1) directly specifying the elements



String joined1 = String.join(",", "a", "b", "c");



2) using arrays



String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);


3) using iterables



List list = Arrays.asList(array);
String joined3 = String.join(",", list);


c++ - Is there a way to ensure that a code only uses names from std, and not the global namespace?

When using a header which has a format , an implementation will put names into std namespace. And it may put names into the global namespace as well, as it is described here:




[ Example: The header assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. [...] — end example ]





Is there a (maybe compiler dependent) way to circumvent/disable this behavior (I'm open to any tricky solution)? I'd like to use names from std only, and I'd like to have an error/warning when using names from the global namespace:



#include 

double a = cos(0.5); // I'd like to have an error here, because std:: is missing


The reasons:





  • It is hard to write a portable code, if it may use names from the global namespace, as these names may be not available in other compilers. It is much cleaner to use everything from std, and not use the global namespace at all

  • cos(0.5f) does a different thing whether std:: is prefixed or not (float vs double result).

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...