« first day (374 days earlier)      last day (4576 days later) » 

7:00 PM
In case of a copy, the copy ctor argument will bind to it (as it must be a reference).
 
right and it will just copy the animal parts
 
But they only know how to operate on base subobject, hence slicing.
 
#include <iostream>

int main() {
    while (std::cin.good()) {
        std::cout.put(std::ostream::char_type(std::cin.get()));
    }
}
 $ cat main.cpp | test
(outputs nothing)
 
and if Animal animal is a member of a class?
 
@Srle Is this correct in Mathematics y = x + 1?
 
7:01 PM
0
Q: I am getting dereferencing to incomplete types every time i run this code. Thanks in advance

Ankita Balotia#include <pthread.h> #include <stdio.h> #include <stdlib.h> typedef void withdrawPtr(int); typedef void depositPtr(int); typedef void accountPtr(int); typedef void deltaccountPtr(); int balance; pthread_mutex_t mutex1; i have actually converted an equivalent c++ code to c cod...

Evil C strikes again.
 
@CatPlusPlus Where do such desires come from? :)
 
@BPDeveloper you mean like class Zoo { public: Animal animal; };?
 
yes
private in this case, but yes
 
Same principle, except you probably don't want plain reference data members.
 
@StackedCrooked works on my box
 
7:02 PM
A zoo with only one animal? I wouldn't pay for that. Unless it was a Dinosaur.
 
@LewsTherin Hello!
 
@FredOverflow A singleton zoo.
 
@BPDeveloper then the member will get the fields that belong to Animal copied, and the Dog-specific fields will be lost -- as well as its vtable, iirc
 
@codemaker Heh?
 
7:03 PM
@BPDeveloper hiya! :) Learning C++? :O
 
your animal would be near useless
 
@CatPlusPlus Not for long. "Nature finds a way!"
 
@LewsTherin unfortunatly :(
 
@codemaker Not on my Mac :(
 
@StackedCrooked Well, that's your own damn fault!
 
7:04 PM
@StackedCrooked "cat iocopy.cc | ./iocopy" prints iocopy.cc as expected. I'm on linux
 
@BPDeveloper Ugh, let me guess coursework. I have to go for an interview next week. Oh well
 
@BPDeveloper Weren't you planning on going back to Java? ;)
 
Useless use of cat.
./iocopy < iocopy.cc.
 
Dammit I forgot to prepend "./"
 
@FredOverflow I have a course in C++ so I need to do it:( Otherwise I would be in the Java room!
 
7:05 PM
It works now.
 
@LewsTherin Yes
@ch
 
cat iocopy.cc | cat | ./iocopy <-- that works too
 
@cHao So, would the dog still bark if I assign it to a Animal variable?
 
@StackedCrooked you can facepalm whenever you are ready
 
as a member
 
7:06 PM
Run cat few more times to be sure it does nothing.
:]
 
#include <iostream>

int main() {
    while (std::cin.good()) {
        int n = std::cin.get();
        if (n == -1) break;
        std::cout.put(std::ostream::char_type(n));
    }
}
^ Better.
 
uh
while(std::cin)?
 
@FredOverflow hi, can I pick your brain?
 
@LewsTherin Try
 
@LewsTherin Using an ice pick on a person's brain is known to be fatal.
 
7:07 PM
I like to do while(std::cin.get(n))
 
@StackedCrooked It'll probably be faster to do it with larger buffer.
 
@CatPlusPlus time for some profiling!
 
@BPDeveloper If you have Animal member, then assigning Dog will cause slicing. Being a member doesn't change that.
 
@BPDeveloper not unless you had a std::string sound in Animal, and Animal::makeSound just spit out this->sound (or something like that)
 
lol the few days I haven't been on here was like torture ha, I miss this room
 
7:07 PM
@DeadMG The challenge was to write a program that copies it "excactly" . Alf made it seem like it was something non-trivial on Windows. So I thought to be extra-careful and copy byte per byte avoiding implicit stuff.
 
@codemaker Byte-by-byte I/O is usually slower than properly buffered one. :P
 
Okokok, thank you
 
@CatPlusPlus right, but don't the iostreams do their own buffering?
 
@FredOverflow when you type cast an object, it doesn't change the object right, it just returns amount of bytes related to the type it is casted to...
 
@LewsTherin Where have you been?
 
7:08 PM
anything that's not defined in Animal can just get lost
 
@StackedCrooked the problem on windows is it doesn't work for binary files
 
@LewsTherin Do you cast object or pointer/reference to object?
 
@CatPlusPlus On byte at a time is what my mother taught me.
 
@FredOverflow Away from SO :(
 
@StackedCrooked go back and look at the transcript
 
7:09 PM
@FredOverflow I was going to ask the pointer, but I want to ask the object first
 
@codemaker What do I need to look at?
 
@LewsTherin Please show me code. Type casting never returns an amount of bytes. Are you sure you're not confusing casting with the sizeof operator?
 
@cHao So, I write Animal& animal; and then assign dog then it would work?
 
You can't assign to references in C++, you have to initialize them instead.
 
@FredOverflow What I mean is...am I returning a temporary object related to the type of the cast? For example int a =(int)floatObject;
 
7:10 PM
A: Move it!
B: std::move(it);
 
If you want to assign the dog later, then what you really want is a Animal *.
 
Animal& over Animal*
 
references have to be set when you declare them, iirc
 
Assignment? This kills the dog.
 
yeah, that's fine
 
7:11 PM
@LewsTherin Your code does a conversion from float to int. I don't see what the problem is.
It's a value conversion, there are no objects involved.
 
@AlfPSteinbach I posted a code sample (a few pages) above. Please tell me why it won't work on Windows.
 
Temporary objects of primitive type are quite rare in C++.
 
@cHao What is the difference?
 
@StackedCrooked It translates newlines.
 
@CatPlusPlus And this is a Windows-specific issue?
 
7:13 PM
@FredOverflow I am confused about the floats. I know if I cast an int into a char, it takes the first byte. But a float to an int, does it take the bytes before the decimal point?
 
@StackedCrooked we talked about how windows defaults cin to not be binary (maybe cout as well, but we didn't discuss that). As a result it will convert line endings and you can't match the input to the output.
 
How would it know where the decimal point is?
 
@StackedCrooked Dunno.
 
144
Q: What are the differences between pointer variable and reference variable in C++?

PrakashI know references are syntactic sugar, so easier code to read and write :) But what are the differences? Summary from answers and links below: A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. A pointer can point to NULL while refe...

 
~/Development/Playground $ cat ./test | test > test2
~/Development/Playground $ diff test test2
Binary files test and test2 differ
Dammit.
 
7:14 PM
./test < test > test2
 
@LewsTherin Ah, now I get it. No, the byte representation of an int has absolutely nothing to do with the byte representation of a float. The cast will actually emit an assembly instruction that does the conversion.
 
~/Development/Playground $ ./test < test > test2
~/Development/Playground $ diff test test2
~/Development/Playground $
 
@BPDeveloper Animal &animal; animal = dog; won't work, afaik. you need to set the reference as you define it. if you need to set it later, then Animal *animal; animal = &dog; will work.
 
Okey
 
7:15 PM
@FredOverflow Oh so how it is converted is done at compile time
 
@LewsTherin No, emitted assembly instructions are generally executed at runtime ;)
 
@FredOverflow um :S
how so?I suppose inbuilt to the cpu
 
The x87 instruction that does the conversion is called FIST (store integer).
 
x87, FIST? omg
 
7:17 PM
This just in: Intel engineers actually have a sense of humour.
 
@LewsTherin x87 is the part of your processor that does floating point math.
 
@FredOverflow Ah ok let's ignore that lmao. What if you cast an address?
 
Example code please.
 
What if you cast a spell?
 
I remember a colleage once bursting out in laughter. He a mistakenly written fistUser instead of firstUser.
 
7:18 PM
nasal demons, maybe?
 
:1746356 d:\dev\test> type foo.cpp
#include <iostream>

int main() {
    while (std::cin.good()) {
        int n = std::cin.get();
        if (n == -1) break;
        std::cout.put(std::ostream::char_type(n));
    }
}

d:\dev\test> cl foo.cpp
foo.cpp

d:\dev\test> foo <%windir%\system32\notepad.exe >bah.exe

d:\dev\test> fc %windir\system32\notepad.exe bah.exe
FC: cannot open %WINDIR\SYSTEM32\NOTEPAD.EXE - No such file or folder


d:\dev\test> fc %windir%\system32\notepad.exe bah.exe
Comparing files C:\WINDOWS\SYSTEM32\notepad.exe and BAH.EXE
 
@FredOverflow a sec please..
 
@AlfPSteinbach I failed :(
 
@StackedCrooked no, it's like an experiment. getting a negative result isn't to fail. it is to establish more truth
 
7:20 PM
@AlfPSteinbach Should I blame C++, Windows, or myself?
 
C++
it has the the order of abstraction layers, wrong
 
That's a once in a lifetime event.
 
all of the above!
 
@StackedCrooked blame as many of those as you can, except your self. That is the american way.
 
Is Dog(); the same as Dog dog; ?
 
7:20 PM
I blame Java.
 
with text conversion at the very bottom
 
I'll blame notepad.
 
@LewsTherin As a general answer, unless multiple inheritance is involved, casting pointers does not actually create any code.
 
@StackedCrooked if you are not american, blame the americans
 
It's obvious! Notepad doesn't support unicode!
 
7:21 PM
It does, actually.
 
ugh I wish I could remember exactly how it was written... http://ideone.com/HxUwN
I'm getting errors
 
@LewsTherin Is Dog(); the same as Dog dog; ?
 
@BPDeveloper Dog() is a temporary object. It won't live very long.
 
A temporary dog. A hot dog of sorts.
 
14
Q: Lifetime of temporaries

frunsiThe following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn't seem to be like this. So, now I assume that the temporary returned by foo(...

 
7:22 PM
Also Dog dog(); is most vexing parse
 
@BPDeveloper em nope... unless you store it in the dog object?
 
@LewsTherin you are trying to assign a pointer to a struct on line 14
 
So it is not the same?
 
@DeadMG Thank god he didn't bring it up.
 
@codemaker Ah yes that's why!
Thanks
 
7:23 PM
@LewsTherin C-style casts bad, struct before type name is silly Cism.
 
@BPDeveloper No, not at all. In C++, you have the choice between temporary dogs, automatic dogs, static dogs, and dynamic dogs. Whereas in Java, you only have dynamic dogs.
 
@AlfPSteinbach Why does it do it wrong? Why isn't it being fixed?
 
I want Java dogs
 
Then you "want" Dog* dog = new Dog();.
 
I should invent a programming language called Lassie.
 
7:23 PM
@CatPlusPlus I know But I have no choice :(
 
187
Q: In C++, why should `new` be used as little as possible?

BitgardenI stumbled upon the Stack Overflow question Memory leak with std::string when using std::list?. One of the first posters says: Stop using new so much. I can't see any reason you used new anywhere you did. You can create objects by value in C++ and it's one of the huge advantages to us...

 
@LewsTherin Er, what?
@FredOverflow All-star dog.
 
@BPDeveloper don't worry, there is a c++ programmer somewhere being forced to write java.
 
hah
 
somewhere?
 
7:24 PM
@CatPlusPlus Coursework, that's the way the code is written in the api and I have no choice but to understand it.
 
@LewsTherin If a code uses struct T, then it's probably in C, not C++.
 
@BPDeveloper Mindless zombie
 
@LewsTherin Casting between unrelated pointer types is UB.
 
@StackedCrooked it does it "wrong" because text mode has text conversion at bottom. i think main reason it isn't being fixed is that the restriction imposed by C and C++, in turn has influenced what one uses standard streams for. so no demand.
 
Teaching people to use new as little as possible is wrong. If you teach them the fundamentals right so that they will use new less as a side-effect. It's moronic to make it an goal in itself.
 
7:25 PM
@CatPlusPlus the api was written in C but I don't understand how it works. The casting does it make sense
For example how can you do (Dog*)NULL ? for example... doesn't make sense
 
@StackedCrooked People are lazy and silly, and if you won't tell them "no, stay the fuck away from this", they'll keep doing silly stuff.
 
Wait, we are mixing two examples here
 
@LewsTherin Why not?
 
@LewsTherin Why not? The null type is a subtype of every other pointer type.
 
@LewsTherin it's a null Dog* pointer. It makes sense.
 
7:27 PM
If you tell them to not use new they might zealously start to eliminate it in their code, causing slicing issues and/or stack overflow.
 
A nulldog.
 
What happens when I cast (Dog*)NULL
 
@StackedCrooked You shouldn't use new unless you need to. Many C++ newbies aren't even aware of the alternatives.
 
Nothing.
What did you expect to happen? A nulldog is a nulldog.
 
@FredOverflow See my previous comment.
 
7:28 PM
@LewsTherin if this is C, then struct typeOne typeA = *(struct typeOne*)&typeB; would work in most compilers. not sure if it technically causes UB, though
 
if you make Dog() and Dog dog; inside the same scope would they live for the same period of time
 
@cHao It definitely works. But I don't know how
 
@BPDeveloper Dog() lives until the next semicolon. Dog dog; lives until its scope ends.
 
If you mean as in Dog(); Dog dog;, then no. Dog() is a temporary and dies immediately.
 
Poor dog, never got a chance.
 
7:30 PM
It wouldn't buy you anything if Dog() lived longer, because you would have no way to access it a second time later, anyway. (That's the justification for move semantics, by the way.)
 
It might live for a while if you start your program like this: int main() { run(Dog()); }
 
Yes, until the semicolon :)
 
You cruel person, running over dogs like that.
 
If I make a dog inside a switch like this Dog dog;
and the switch is inside a function
would the pointer outside the switch refer to a living dog?
 
@CatPlusPlus Well, not immediately, that would be pretty pointless. (On a side note, this aspect of C++ was actually once so vaguely defined that immediate destruction was permissible.)
 
7:33 PM
In unrelated news, Overgrowth is going to be the best game ever.
 
@BPDeveloper I don't see any switch in "Dog dog;"
 
@BPDeveloper nope, dangling pointer
 
@LewsTherin if it works, it's because C's type system is weak enough that you can easily get around it. it's effectively like "yeah, i know &typeB looks like a struct typeTwo *, but i know better than you, and i say it's a struct typeOne *. now dereference it and gimme a struct typeOne."
 
So how would I do this @FredOverflow?
with new/delete?
 
@BPDeveloper Please show us actual code. It is needlessly hard to discuss without code.
If you're uncertain about lifetime, insert a std::cout << "goodbye!\n"; into your Dog destructor and step through the program with a debugger. Then you'll know for certain when your dogs go to heaven.
 
7:34 PM
Hai! I have a struct that uses pointers. Should I delete it?
 
@StackedCrooked Is that a joke?
 
@cHao I'm still confused, so what is returned from the cast is a pointer to a struct typeOne object?
 
@cHao @cHao C's type system doesn't care what you cast to what. You can cast any struct to any other struct. It trusts that you know what you are doing.
 
@FredOverflow You are free to answer it seriously if you feel like it.
 
@StackedCrooked yes, delete it. The delete key is just below your insert key.
 
7:36 PM
@StackedCrooked You should delete the struct if you acquired it with new, and you should delete the pointers inside the struct if you acquired them with new. But even better yet, don't use new/delete at all.
 
@codemaker It works! Tx
 
function test() {
Animal* animal = 0;

switch (...) {
case ...:
Dog dog;
animal = &dog;

...
}

animal->doSomething();
}
 
@LewsTherin there is no "return" from the cast. You are merely telling the compiler that this pointer points to a different type of object.
 
It looks like you want animal = new Dog(); but then don't forget to delete animal;
Also, Animal needs a virtual destructor, otherwise you will enter undefined behavior territory when you try to delete a Dog object through an Animal pointer.
 
Ok, so no new? How bout struct { char s[5000000]; } data; . Lol stupid noobs and their new.
 
7:37 PM
@StackedCrooked if you have a destructor that deletes the pointers inside, or have deleted them yourself, and the struct was dynamically allocated, then yeah -- you should probably delete it
 
@StackedCrooked How about std::vector<char> data(5000000); instead?
 
@StackedCrooked that's not a pointer. t hat's an array :P
 
@codemaker So basically it's the same address, just that it thinks it is pointing to a different object...
 
@FredOverflow What shall be inside that?
 
@LewsTherin basically, yeah
 
7:38 PM
@FredOverflow That uses the free store. Are use insane?
 
@BPDeveloper that might cause a segfault. Dog only lives to the end of the switch. Also, I don't think that code would compile like that as the switch will cause a jump past where Dog is created. (I don't know the technical term for it)
 
@BPDeveloper Probably nothing :) It just needs to be virtual.
 
what could have been inside? Just an example
 
@StackedCrooked What's wrong with the free store?
 
@LewsTherin right. Except C doesn't really think of it as an object :)
 
7:39 PM
@cHao That is weird, I don't understand. What if I do typeOne->a ? but the object doesn't have an a data member
 
@BPDeveloper Release of resources acquired in the constructor.
 
@codemaker lol yeah I'm used to that classes crap :(
 
I suck at trolling.
 
74
Q: What is The Rule of Three?

FredOverflowWhat does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?

@LewsTherin The type system will forbid it at compile time. Of wait, you casted your way around the type system. Then you're on your own. Good luck!
 
@LewsTherin then you access...something else. (could be some other member of whatever you casted.) or, depending on other stuff, you might segfault. or you might set your machine on fire.
better know what you're casting.
 
7:41 PM
So what is the point? Why would anyone want to do that? :S God I can't understand this api
 
@LewsTherin right. If the original object didn't have any data at that memory offset, then you have a problem.
 
@LewsTherin What API?
 
is the rule of three really gonna change with c++11?
(stated in the link)
 
1. New allocates from the free store and performs the constructor.
2. New is bad.
3. Performing the constructor is not bad.
4. Therefore: the free store is bad.
 
7:42 PM
@bamboon Sure, now we have move constructors and move assignment operators as well.
@LewsTherin Where are the funny casts?
 
@FredOverflow Does Dog need a destructor?
 
@LewsTherin well if you had a header struct on your data, struct data_header { int type; }; And struct data_type_one { int type; int look_ma_more_stuff; }; You could cast a data_header to a data_type_one if you knew the type was correct
 
sockaddr *s are casted to sockaddr_in *s and back all the time. it works because there's a field in there that says what kind of structure it is (and thus, what it's safe to cast to)
 
@StackedCrooked Using new in your own code is bad.
@BPDeveloper Dog inherits the virtualness of the destructor from Animal. So no.
 
by the way, I'd like to mention
I'm feeling rather kind of sick
and miserable
 
7:43 PM
look for this line if (bind(sockfd, (struct sockaddr *) &serv_addr,                   sizeof(serv_addr)) < 0)
  error("ERROR on binding");
@codemaker so if the offsets had matching data type it is fine?
 
@FredOverflow So can I assign it like this then Animal* animal = new Dog() ?
 
@BPDeveloper yeah
 
@BPDeveloper If Dog inherits from Animal, then yes.
 
@LewsTherin if they have data at the offset, then it's fine. the types don't really even have to match, but it'd be a really good idea if they did.
 
@BPDeveloper It would be really helpful if you posted the source to Animal and Dog here. Well, not here, but at ideone and then post a link.
 
7:45 PM
@LewsTherin right. You can access data_header->type in a casted data_type_one struct with no problem. You could even name it something different as long as the first item was an int.
 
@cHao what field?
 
@cHao Erm, strict aliasing?
 
I have seen more error messages in visual studio than I have seen in Java for 2,5 years
stupid language
:P
 
@codemaker But those structs have different fields :O
 
better language
 
7:46 PM
 
@LewsTherin which structs? data_header and data_type_one?
 
@FredOverflow hmm?
 
@LewsTherin Hah! what is that?
 
@cHao It is illegal to nilly-willy cast your way through memory. You cannot just access an int like it was a float or something.
 
http://www.retran.com/beej/sockaddr_inman.html
sockaddr and sockaddr_in are different. Yet they cast sockaddr_in to sockaddr :(
@BPDeveloper Some shit I am trying hard to understand, but failing miserably :(
 
7:48 PM
Ah, in other words: We are doing the same
 
@LewsTherin Probably C's idea of polymorphism without inheritance?
 
man
 
@FredOverflow it doesn't make sense sometimes, but you most certainly can do it. that "fast inverse square root" function in quake or doom or whatever, is proof that it's possible.
 
Starcraft 2's AMM put me in Platinum League 1v1
 
@LewsTherin here they have a cast from struct sockaddr_in and sockaddr_in6 to sockaddr. Both have a short in the first position. Sockaddr then has 14 bytes of "data". It's safe. Just go with it.
 
7:49 PM
:(
@cHao No, that's proof that it might work on one platform with one compiler.
 
@cHao It is proof of UB disguising as "it works!", like it does from time to time.
 
@FredOverflow Ha they should they have used a void*
 
it's Undefined Behaviour
 
@codemaker I have OCD or something close to it lol... So you are saying only the first field is used
 
@LewsTherin no, more than the first field is used :)
 
7:51 PM
@BPDeveloper Is this Animal/Dog crap an example from your C++ book?
 
Lol
 
@codemaker How do you know? It doesn't say that anywhere there, and how come it doesn't crash
@FredOverflow It is in every book lol. If not the lecturer uses it
 
It is from the Animal 1 on 1 book
 
1 on 1? Which one wins?
 
@LewsTherin Well you are passing the port and address to bind to. It uses the first field (the address family) along with the socket type probably, to determine what type of structure you passed in
 
7:53 PM
@FredOverflow If you made that dog object with new as I did in the function, but I want it to be around for some time. Where do you delete it? in the deconstructor?
 
@LewsTherin you also pass the size of the address you are passing in. The bind function just takes a generic "sockaddr" pointer and inside it does fancy stuff to match it to the right type.
@BPDeveloper delete it when you are done with it. You have to keep track of it and delete it manually. You can't delete it in the destructor because the pointer is local to that function. The destructor won't even have access to it.
 
ah, true
thank you
 
@codemaker can you please slow down a bit ha thanks ;) So the sockaddr_in holds a family field similar to sockaddr. It tells the socket which type of family it is. The sockaddr address (sa_data) fields holds the ip address?
 
I found it:
1. New is bad because if I use it then I allocate on the free store in *my own* code.
2. Placement new is not bad because it only performs the constructor. Constructors are good.
3. I'll use a std::vector to allocate memory and placement new to construct it. This way I'm safe.
4. Sarcasm.
4
 
@LewsTherin yes basically
 
7:57 PM
@codemaker But which sa_data field in sockaddr matches sockaddr_in ?
 
class Zoo {private: Animal animal; };

Zoo::OpenZoo() {
// Make a dog
animal = dog;
}
So there is no way of doing this? I can't understand it
I want the variable animal to be available to all functions
and I want a dog attached to it
 
@LewsTherin you can think of each one of those structs as being mapped out in memory just as they are defined (with padding in some cases). So struct {char a; char b; char c; char d; }; Is 32 bytes. So is struct {int i; }; You can exploit this fact and cast between them to achieve different goals.
 

« first day (374 days earlier)      last day (4576 days later) »