Rashakil Fol 978 Super Senior Demiposter Team Colleague

One reason you can't just "catch and handle" pointer errors is that if you overwrite something in the wrong place of memory, it can totally screw with the behavior of your program. You could have overwritten other variables, especially hidden values like the addresses the processor would jump to after it returns from a function. In particular, the mechanism C++ uses for propagating exceptions might have been totally corrupted. That's why you can't rely on the exception system of C++ to handle errors where pointers point to unmodified locations.

The only reasonable way to protect yourself from these kinds of errors is to watch things from a separate process. This is one reason why the web browser Google Chrome has all its tabs in separate processes.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Using pointers directly is undesirable because pointers are dangerous. If you have a pointer, the thing it's pointing to could become destroyed, so you have to make sure that hasn't been done. Also, if you're pointing to things manually on the heap, you have to make sure to destroy those things -- so you have have to keep track of who owns what. Keeping track of things like this is a pain. There are lots of ways to avoid using pointers. For starters, use vectors instead of raw C-style arrays. Pass arguments to functions by reference instead of passing pointers. Use the various kinds of smart pointers instead of raw C-style pointers.

Since you are new to C++, you don't need to worry about any of the things I've said! Continue to use pointers so that you can learn the low-level details of the language, and later, you will appreciate the other tools that are provided.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well, error catching is what try-catch blocks are for...

But if you're talking about some pointer-related bug, the right answer is to avoid using pointers directly in the first place. I don't know what your program is trying to do -- why are you deliberately indexing past the end of an array?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

C++ has a more flexible template system that can do things (like take template classes as template parameters) that C#'s generics system just can't do.

Its use of RAII is nice, too.

It has a bunch of good libraries, like boost, and you can more easily write cross-platform applications with it.

Also, you can use multiple inheritance, which helps get around a few holes in the type system.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

There are none.

Well, there are a few. If you don't have a C++ compiler that targets the platform you're looking for, or if you're working on a project (that somebody else started) that uses C liberally.

Also, if you're making a few low-level functions that will get called via a foreign function interface by another language, C is reasonable. And it's fine for making an interpreted programming language -- you never have to fret about the C++'s runtime stuff.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Garbage collection is the most important, and outweighs the combination of all other advantages.

C# has anonymous functions, i.e. closures, which outweighs the combination of all the advantages below.

C# has an actual module system.

C# has generics, which are more powerful in some ways than C++'s compile-time template system, and less powerful in others -- I would describe them as a little bit less verbose.

C# has extension methods which are cute but only a minor advantage.

You get a little bit more certainty about the various primitive data types -- you know that an int is an Int32.

Did I mention garbage collection?

Unless you're doing low-level numerical computation where processor time is the limiting factor, or unless you're using a lot of C++ libraries, C# is preferable.

Another advantage of C# is that you can use better languages that run on .NET too, like F#, and other languages, like Boo.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Hm yes, VernonDozier identified what was actually causing your problem. Fix your random function, too, though.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Your function random is broken! This is an awesome bug. It's broken in two ways.

First, it's broken because rand() may return RAND_MAX. In such a case, your random function is clearly broken, because it will return the value ceiling.

So the quick fix is to divide by RAND_MAX + 1.0 .

Now, you have to be careful. Even then your code is broken! Try running this code.

#include <cmath>
#include <cstdlib>

#include <iostream>

int main()
{
  double f = static_cast<float>(RAND_MAX - 64) / (RAND_MAX + 1.0);
  double g = static_cast<float>(RAND_MAX - 63) / (RAND_MAX + 1.0);

  std::cout << "f < 1.0: " << (f < 1.0)
	    << "; g < 1.0: " << (g < 1.0)
	    << std::endl;
}

You'll notice that it outputs false for one of the values, and true for the other!

Why? Why, the datatype float doesn't have enough precision to represent the value RAND_MAX on most computers (on some computers, RAND_MAX is 32768, which is small enough so that it can). So when you convert RAND_MAX to a float, it rounds to the nearest acceptable float. You can see that if rand() returns the value RAND_MAX - 63 or greater, your function random() will treat it the same as if it had returned RAND_MAX. float can only exactly represent integers up to 16777216, which is 2 to the 24th power.

So your problem's probably that rand() is returning a value too large for a float to distinguish between …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Is there a reason you posted this message?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

I would like to take a poll of what version of C# and .NET you are used to working with. I'll start.

C# 3, .NET 3.5.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Also, it's important to delete them because they might have other stuff to do (like flushing data to filehandles, and such).

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Why would you want code for a chatbot? Go die, cheater.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

It doesn't remain "in memory" because the process has completed and all of its memory is reclaimed by the operating system.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Thread safety? I thought you were using a select loop! I guess you would need to worry about that a bit, yes.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Yes, it does remain. If new operator is used u need to delete explicitly.

What the fuck are you talking about? When the program ends, nothing remains.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This is a C question and belongs in the C forum.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

How about that!

Rashakil Fol 978 Super Senior Demiposter Team Colleague

What I'm more worried about is that I need a new map for every identifier to find the correct client object.
So atm there are 3 maps that all have 'CClient*' as a value but different keys(string, int etc.). There is no way to have this in 1 container?

Well, yeah, there is:

class Foo {
    map<string, CClient*> byThing_;
    map<int, CClient*> bySocketId_;
    map<blah, CClient*> byBlah_;

    void addCClient(CClient* c) {
        // this is me completely forgetting my STL, .add is probably wrong
        byString_.add(c->getThing(), c);
        bySocketId_.add(c->getSocketId(), c);
        byBlah_.add(c->getBlah(), c);
    }
    CClient* getBySocketId(int socketId) {
        return bySocketId_.find(int);  // or whatever it is; i forget STL
    }
};

Now you have all your maps safely synchronized; adding methods for looking up values is left as an exercise to the reader.

Basically it woul be nice to have a structure as a key and that I can use 'map.find' and it will search the structure members for a match but I guess that is not possible or am I wrong?

You would then either need three versions of 'find' (for each type of key) or some type that can represent any given type of key. I recommend going with what I described above.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Probably because you're assigning an element past the end of the array, and there's a variable there that throws off your code unpredictably.

Also, you've already showed a way to change that range; I don't see what the problem is.

But generally speaking the code can be slightly improved. Some random number generators in bad versions of standard libraries will be not very random when you take the result modulo some small number. So using division is.. recommended. ((int) (100 * (rand() / (1.0 + RAND_MAX)))) seems to be a recipe. Of course, this is a lot uglier -- we use floating point arithmetic to avoid risking overflowing int.

With decent random number implementations, you could just use modulo.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well ya... i asked how you would do something like this because I don't know how and I'm writing code in c++ right now... and your kind've being a dick, even in my other thread you insulted me.

I didn't insult you in the thread, but if you're going to cry like a babby about insults that you've imagined, I think you deserve one now.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Header files typically contain declarations of functions, not their definitions. Typically, the implementations are placed in a similarly named cpp file. Your project will then have multiple cpp files.

Also, #define is not how you declare or define functions; I don't know what you're thinking.

For example, I might want to have a function fooz available by a header file "foo.h". So in foo.h I'll have

#ifndef guard_foo_h_
#define guard_foo_h_

int fooz();

#endif /* guard_foo_h_ */

And in foo.cpp

int fooz() {
    return 4;
}

Then, some other file, like bar.cpp, might #include "foo.h" so that the compiler sees the declaration of fooz. Or the writer of bar.cpp could just manually write

int foo();

to inform the compiler about the declaration.

Then, after you compile foo.cpp and bar.cpp and link them together, you'll get a working program where a function written in bar.cpp calls a function written in foo.cpp.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

If you're worried about maps, don't worry at all. They will scale just fine. If you ever finding yourself wanting to eke out that last bit of performance, it should be easy to plug in various other implementations like hash tables or such. Consider using typedefs so that you would only have to make that change in one place.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

You call this a C++ question?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This is absolutely awful. Do not do what you are talking about doing.

Tell us what your problem is, what you really are trying to achieve, instead of asking about one particular mechanical option, so that a better solution can be recommended.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Maybe this is a stupid question, but why you writing myvector->push_back(...) instead of myvector.push_back(...) ? myvector is a cliext::vector<T>, not a cliext::vector<T>^.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This isn't a C++ question, and it's an easy question; figure it out yourself.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

An image is just a two dimensional array of colors. A color could be represented a number of ways, one of which is to give brightness levels for red, blue, and green that range from 0 to 255.

An example image file could be (in hexadecimal)

00 00 00 03     ; the width (3 pixels)
00 00 00 02     ; the height (2 pixels)
; the first three bytes in each pixel are R, G, and B components
; the fourth is unused
FF 00 00 00     ; bottom left
FF FF 00 00     ; bottom middle
00 FF 00 00     ; bottom right
00 FF FF 00    ; top left
00 00 FF 00     ; top middle
FF 00 FF 00     ; top right

This example (which isn't in any well-known file format) is very close to a version of the BMP file format -- the BMP format has more options, though.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

C# is easy to learn, and unless you have a very specific, articulable reason to be using C++/CLI, you would be better off going with C#.

Edit: Also, don't expect an answer here; your question is offtopic in a C++ forum.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Mathematically, space analysis works the same way as time analysis, except that you measure a function by the maximum amount of memory it uses, instead of the total amount of time.

So it just involves asking the question: how much space does a function use?

In the vector example, the answer is Theta(n*n), because space for n*n integers gets allocated.

In your second example, it's recursive, and there's no extraneous space usage outside of that needed for your stack frames. So the question is: how many stack frames are there? Hint: the answer is not n.

----

To approach the vector problem more precisely, we can note that the "memory usage" of the vector v is more like sizeof(vector<vector<int>>) + n * elementUsage. I'm pretending that the vector has a few fields to contain a size and allocation limit value, along with a pointer to its dynamically allocated array. Then I'm accounting for the space taken by the array with n elements by multiplying each element's usage by n. This expression produces an exact number of bytes of memory usage, but the truth is that if you consider that the array of elements is allocated on the heap, there's a few bytes of overhead, not controlled or known by us, used to keep track of memory allocation -- a constant amount, A, presumably. So we could say more precisely that the memory usage is sizeof(vector<vector<int>>) + A + n * elementUsage.

And the value …

Rashakil Fol 978 Super Senior Demiposter Team Colleague

is there a way to contain the text in a normal format when writing out to a text file?

Yes, you can make your own class that inherits from ostream.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Um, the way solutions work is that you devise them yourself. Do you think education is about having somebody else tell you exactly what to do?

In this case, you have the most trivial differential equation, with a stable asymptote. It doesn't get easier than this. If you're referring to this as "advanced" numerical computation, I take it this is the first assignment. If you can't solve this problem with the class materials at hand, you need to switch out of the class.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

This isn't a C# question, and you should be figuring this out yourself using lecture notes and other examples seen in class, in textbooks, and on this and other websites.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

C++ is pretty much just "C with Classes", as defined by it's creator.

Understatement of the century.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Good for you. Why are you telling us this?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

When you see it say that there's "no matching function", that means there's no function with the given name that expects the types you've provided. If you double-check the documentation for ifstream's open function, you'll see that it takes a char*, not a string.

So use the .c_str() member function to get a char*.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

For the sake of information, are you saying I am wrong about headers, or just spouting your opinion on C++?

Just for the sake of information, are you just refusing to recognize my authority on the coolness of things, or just feeling butthurt about some nonsensical post on the internet?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Why don't you just learn both?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Cool, right?

Wrong.

Muhahahahahahah! C++ is never cool.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Yes, guard all your headers with #ifndef directives.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

You can derive the answer yourself from the fact that #include effectively copies the text of the header file into the cpp file before the compiler starts compiling.

So, my question is: why would you want to require people to follow extra instructions just to use your header file?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Thank you so much for fast and detailed responce., so after statement p=a+i, does it mean that p equals 2 in the first pass?

No, the statement p=a+i changes the variable p so that it contains a memory address i units after a. There is no way p will equal 2, because 2 is an int, and p is an int*.

and does it mean that with statement x=*p means that x=2? i'm abit confused with these 3 lines...

If *p evaluates to 2, then that means x will contain 2. Here is a simple example:

int array[] = {2, 3, 5, 7, 11};
int* p = array;  // p contains the memory address of array[0]
int x = *p;  // *p returns whatever's at the memory address stored in p.  So it returns the value that's in array[0].  So it return 2.  Thus, the value 2 gets assigned to the variable x.
int* q = p + 2;  // q contains the memory address 2 units after p.
int y = *q;  // *q is 5, so y gets assigned 5.
Comatose commented: Nicely Done Man. +9
Rashakil Fol 978 Super Senior Demiposter Team Colleague

when in the function p=a+i; does it mean p=0+0?

Only if a and i are both zero. But a is not zero.

since a points to the 1st element which is a 0 and i is also 0 in a first pass of the loop???

No, the first element is a 2. (The array whose memory location is passed as the parameter 'a' has the elements 2, 4, 1, 3.) But you're not adding the first element -- you're adding the memory address of the first element to an integer i. The sum (a+i) will be the memory address of the element i units over from the element pointed at by 'a'. For example, (a+2) will be the memory address of the element 1, and *(a+2) will be the value 1.

or it would be a=0 + 2(since its the first element's value)?
thank you

What the fuck? How could the line "p=a+i" end up assigning a value to a?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Object oriented programming is the practice of passing dynamic libraries as parameters. Most object oriented programs are not robust in any sense.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

The code doesn't store the value of x in a[2] before that line. What makes you think it does?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

No, that's wrong too because your code will append to whatever was previously stored in Label5.Text :P

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Why, take a second look at that code! You're overwriting the value each time through the loop?

You want Label5.Text += element + " "; inside the loop, but don't forget to overwrite it with an empty string before. Of course, that's a mere incremental improvement. You probably really want Label5.Text = string.Join(" ", stringArray);

Rashakil Fol 978 Super Senior Demiposter Team Colleague

What is sas?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Use a for loop or a while loop and with it, add up the marks of the elements of the array.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Well, you would have an easier time using C#.

Rashakil Fol 978 Super Senior Demiposter Team Colleague

Okay, but why would a deck of cards be a two dimensional array?