« first day (381 days earlier)      last day (4793 days later) » 

sbi
sbi
17:01
@RMartinhoFernandes You mean, ask the question again, and wait for good answers to trickle in, while promising a bounty to raise the quality? That's a good idea – except that the question will be closed immediately as a dupe.
@sbi Well, I meant a bounty on that question. I think the question is good, but I'm not entirely satisfied by the existing answer.
Als
Als
@RMartinhoFernandes: Which one?
sbi
sbi
@RMartinhoFernandes Oh, I see. Mhmm.
27
Q: In what cases do I use malloc vs new?

I am new to C++ programming but have a solid background in C#, Java and PHP. I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the...

sbi
sbi
@RMartinhoFernandes I always prefered FAQ entries to have an accepted answer, but maybe that's not as good and important as I thought it is?
@RMartinhoFernandes What is this???
Als
Als
17:03
The new keyword is the C++ way of doing it, and it will ensure that your type will have their constructor called. The new keyword is also more type safe whereas malloc is not typesafe at all.
@sbi Well, that question can never get an accepted answer now. The asker was nuked for his weird editing behaviour.
Als
Als
Only these two lines in the answer talk about new over malloc
@sbi It's one of the reasons available for bounties.
sbi
sbi
@RMartinhoFernandes I know. Hence my statement.
@RMartinhoFernandes You can give a reason for a bounty?
Yes.
It's also possible to write your own.
sbi
sbi
17:06
@RMartinhoFernandes When I click on "start a bounty", I don't see any way to give a reason... Ah, I didn't dare to click on "Next" before. That seems good.
sbi
sbi
Well, I'd be willing to spend 500rep on a bounty for this. What do you guys think?
Als
Als
What are we expecting here as answer?
Well, I was willing to do the same, but I won't stop you :)
sbi
sbi
@Als "Canonical answer required"
17:08
can you not double bounty a question?
sbi
sbi
@RMartinhoFernandes I have more rep to spare than you.
@awoodland Good idea. Lemme check...
A question can have three simultaneous bounties.
sbi
sbi
@awoodland No, it seems you can't. At least I cannot start a bounty on this question.
@sbi I'm way past 20k now, there are no more thresholds.
sbi
sbi
@RMartinhoFernandes I'm past 20k 2.5 times. :)
17:09
Oh, then I read that wrong.
Probably means a user can have bounties on three questions.
sbi
sbi
@RMartinhoFernandes Yes, that's how I understood this.
You can start a total of 3 simultaneous bounties.
Right, that's actually pretty clear.
Silly me.
sbi
sbi
@RMartinhoFernandes A silly robot? Now that deserves a star!
I think we should explain in the custom bounty message that this is for the C++ FAQ, and what we are expecting from it.
sbi
sbi
17:13
@RMartinhoFernandes Absolutely.
And I suggest we pin a message here directing the regulars towards it.
Mhmm. I just had another idea...
sbi
sbi
We could also add a CW'd answer and all collaborate on it.
But this would need >54 upvotes to pass the so-far best answer, plus any upvotes that one will catch once we start drawing attention to the question.
Does an answer that has gained a bounty rise above the others?
Not unless it gets more votes.
sbi
sbi
@RMartinhoFernandes So we'd need more upvotes anyway, even with the bounty.
I'm sure an answer worthy of the bounty would rise up.
A big fat bounty draws a lot of attention.
17:16
vs install requires SECOND reboot. how silly. for an application
stackoverflow.com/questions/184537/… - that was on the right question in the end?
sbi
sbi
@RMartinhoFernandes Yeah, and I don't really worry about losing 500rep. What I am worried about, though, is that we might attract many, many answers with a bounty, many of which might be pretty good, but none definitive.
@sbi That's why we need to make that clear in the bounty description.
sbi
sbi
@RMartinhoFernandes Shrug. So? People won't magically provide a definitive answer just because you request one.
They will!
:(
Damn pessimists.
Als
Als
17:19
I doubt there is going to be an definitive answer.
You too, Brutus Als?
Als
Als
I like @sbi suggestion of making it a CW and collaborating on it to answer it definitively.
sbi
sbi
@RMartinhoFernandes I'm not a pessimist, but I'm too old to not to be a realist!
@Als That has drawbacks, too, though. For example, while many of us here are good enough to provide a good answer, that does by no means guarantee that we can collaboratively come up with a good answer. The result might be of frustratingly low quality, or this room might actually die in a horrible flame war, because we cannot agree on something.
dons asbestos longjohns
I'm ready.
Yay! Sci-Fi rep is trickling in :)
Als
Als
@sbi: We Come up with one,We can then review if it is good enough(Let it stay, and take comments),and if we feel it is not definitive we let it stay and then start a bounty, that way the bounty hopefuls will be willing to meet the deficiencies of that answer.
sbi
sbi
17:26
@awoodland: Too bad the guy deleted stackoverflow.com/questions/7967594/different-between-c-syntax/…, I was just about to provide a link to stackoverflow.com/questions/184537/… in a comment.
@Als You want to eat the cake, and have it, too?
What do the others think about this idea?
Als
Als
@sbi: No actually I am tied up with a client visit in couple of weeks,So I don't think I will be eating the cake.Just an suggestion.
A great answer needs to mention: 1) how malloc only creates objects if they're of POD types (is POD still the requirement with the new C++11 categories?); 2) how new calls constructors; 3) how neither of these should be used "in the open", but instead contained in resource managing classes. 4) ?
... how new throws on failure, avoiding UB by dereferencing null pointers ...
Als
Als
It should also explain when malloc is better to use than new.
fancy a flame war :P
Right. (When is that btw? C interoperability. Anything else?)
Als
Als
For POD types malloc is faster?
17:36
Only if you want them not-initialised.
Right, should mention that malloc doesn't initialise a damn thing.
Als
Als
@RMartinhoFernandes: Why don't you come with an answer?
I may do so.
But I'm planning it :)
sbi
sbi
@Als That's easy: In C++, never use malloc(), period.
@RMartinhoFernandes ...how a new expression does two things, while malloc() only serves one of those.
Als
Als
@sbi: Debatable.
sbi
sbi
@Als Watch what you refer to!
Als
Als
17:40
huh
Well, there's only one thing malloc is good for: getting you uninitialised memory.
There's very little use for that.
And ::operator new can do that trick too.
Als
Als
Put that all in an answer...dazzle me.
sbi
sbi
@Als If you refer to a user, rather than to a message, then your message seems to refer to the last message of that user. And I do not see at all how it is debatable that a new expression does two things.
Als
Als
Okay
Let me be explicit
5 mins ago, by sbi
@Als That's easy: In C++, never use malloc(), period.
Debatable.
sbi
sbi
@Als Don't be explicit here, Jeff will ban you.
@Als Why do you make this so hard to yourself? Just click on that damn arrow on the message you want to refer to and be done!
Als
Als
17:44
Did you intend to mean expletive?
lol, you sound like Google.
sbi
sbi
@Als :)
@RMartinhoFernandes You?
Als
Als
@RMartinhoFernandes: Trying to be correct :P for a change
sbi
sbi
1 min ago, by sbi
@Als Why do you make this so hard to yourself? Just click on that damn arrow on the message you want to refer to and be done!
17:45
Wait, what's going on here?
I meant that @Als sounded like Google with "Did you intend to mean expletive?"
Google keeps trying to correct you.
Als
Als
Geez.
Can I bother you folks with a C++ problem, I am trying to think of a solution to?
sbi
sbi
Ok, I have done some research on meta, in order to evaluate @Als' idea. Obviously, you can award a bounty to a CW answer, in which case the one who started the CW answer gets it. That would be bad, if the CW idea seems to fail, we setup a bounty, and the CW answer still ends up as the best answer. (No, I'm not a pessimist. But I do like planning ahead.)
OTOH, awarding your bounty to your own question is futile. I conclude that, should we try the collaborative route, the one who starts the CW question should be prepared to pay for the bounty, if the collaboration fails. :)
Als
Als
Do you mean an separate/new CW question?
sbi
sbi
@Als Dang, I meant answer, not question.
You cannot CW questions anymore (well, at least not without asking mods for it).
sbi
sbi
17:54
Lemme rephrase: The one who starts the CW answer should be prepared to pay for the bounty, if the collaboration fails.
Als
Als
@sbi: sounds good.
Silence.
Life is gooooood.
how's: stackoverflow.com/questions/184537/… - I talked through fixing the deficiencies of malloc with C++ and concluded that it's practically just reinvented new
ok this is supposed to be hard, not sure how taking input from STDIN and outputting to STDOUT is supposed to be hard?? Maybe I'm wrong though
18:06
@awoodland The question itself is weird. What's wrong with new?
Als
Als
@awoodland: I dont see you explaining that malloc does not iniitalize memory while new ensures that
@TonyTheLion: Huh..good old cin cout ehh
@Als - good point
Als
Als
@awoodland: Could have been better to not write template based code example, but a normal C++ code example?
Someone not well versed with templates will find it less useful.
@TonyTheLion What's that?
Als
Als
And in the context templates is not the crux of the matter.
18:09
@EtiennedeMartel well, if you can complete a test within the allotted time, you can get yourself an interview with Facebook
it's a sample though
Als
Als
@TonyTheLion: Did you get one?
just realized
no
@Als - That makes sense. I'll strip it back after I've had dinner then
Als
Als
@awoodland: No worries. :)
sbi
sbi
Ok, I think @Als' idea is a good one. Shall I start a CW answer?
Als
Als
18:22
@sbi: @awoodland spoilt the party :P He came up with an answer on that.
pun intended on that one, no offence @awoodland :)
sbi
sbi
@Als I've skimmed the answer and I'm not convinced. I still like the idea of a collaborative answer. What do the others say?
Als
Als
13 mins ago, by awoodland
@Als - That makes sense. I'll strip it back after I've had dinner then
0
A: In what cases do I use malloc vs new?

R. Martinho FernandesThere is one big difference between malloc and new. malloc allocates memory. This is fine for C, because in C, a lump of memory is an object. In C++, if you're not dealing with POD types (which are similar to C types) you must call a constructor on a memory location to actually have an object th...

Als
Als
@sbi: At present as the answer stands it is not succinct or definitive to me
Now get cracking.
sbi
sbi
18:27
@RMartinhoFernandes Well, too bad that I asked first, then, because now my effort to come up with an initial version of an answer is wasted. :(
Oh, I see @awoodland substantially edited his answer.
@RMartinhoFernandes Shouldn't new POD be just as fast as malloc?
@FredOverflow Yes. I mentioned that. (I didn't put it in terms of speed, but in terms of having the same behaviour)
Als
Als
@FredOverflow: That is under debate.
Flame wars..anyone?
@Als Why would it be slower?
18:28
52 mins ago, by Als
For POD types malloc is faster?
52 mins ago, by R. Martinho Fernandes
Only if you want them not-initialised.
@RMartinhoFernandes This sounds like "For POD types malloc is faster if you want them non-initialized"...
new POD does not initialise it. It's basically the same as (POD*) malloc(sizeof(POD)), but throwing.
Als
Als
@RMartinhoFernandes: I am not sure on either I don't have the proofs
@RMartinhoFernandes new POD() zero-inits it, IIRC
@EtiennedeMartel Yes, but we are discussing new POD without the parenthesis.
@FredOverflow Indeed
18:31
Does new POD() value-initialize or zero-initialize? Damn, I can never remember :)
Als
Als
@FredOverflow: uhm without paranthesis i notice now
@FredOverflow Right, I forgot about new POD at the time :)
Is the keyword restrict in the C++ standard?
216
A: Do the parentheses after the type name make a difference with new?

Michael BurrLet's get pedantic, because there are differences that can actually affect your code's behavior. Much of the following is taken from comments made to an "Old New Thing" article. Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the...

@ManofOneWay No, it's C99.
Als
Als
@FredOverflow: Zero Initializes
18:32
Yeah, that I know. Just wondered if it was also added to the C++ standard
@ManofOneWay No, it wasn't.
Als
Als
@ManofOneWay: Not that we know of
@ManofOneWay restrict is not listed under 2.12 §1, so no.
Okey thanks
The export keyword is unused but is reserved for future use.
Als
Als
18:33
I have an Question actually, Can i seek some advice here?
lol
@Als not again, please :) just ask
I hate people that have questions and don't share.
Als
Als
@FredOverflow: Cool I shoot, A bit long but please bear it
I'll get tea!
Als
Als
I have a procedural C++ code(Yikes I know).This code uses a global fixed sized array to keep track of some say X type of items.This array is being used in a clumsy way in the code, an structure is passed as function parameter across control paths and one of the structure member holds a pointer to the first element of the said global array.
Across the functions the array is being accessed using index,Now I need to make the array dynamic not fixed sized
Ideally,I would want this to be achieved without modifying the code to a huge extent.
One way is to make the global array an std::vector and the structure being pased around can hold an pointer to it, but this has a serious problem that if array relocates the memory then the pointer points to invalid memory.Also, holding an iterator in the structure will have same problems.
18:34
There's nothing wrong with procedural C++ code. There is something wrong with a global array, though.
Als
Als
what is a good way of achieving this?
Allocate the vector in the structure, and provide a member to get &v[0] (or v.data() if your compiler has it already)?
Als
Als
I could alternately place the vector inside the structure being passed but then I am not sure about the memory requirements and how it would affect.
Numerical indices stay valid after reallocation. So numerical index + reference/pointer to the vector.
ok to think this way ? -> int (*a)(f*) == int (f::*a)() = &f::func; // struct f { int func(){} };
18:36
So, wait, you're asking us how to patch the code so that it works?
The good way would be to axe the whole thing
Als
Als
@EtiennedeMartel: I don't want to get my ass whopped son. Its a job. Pays for meh bread.
@Als How about a std::deque? Or do you do pointer arithmetic?
Als
Als
@FredOverflow: Since the code uses it as array only indexing, which deque will provide too
@MrAnubis Does that compile?
18:38
@RMartinhoFernandes i am trying to figure out hidden parameter theory
@Als Well, std::deque it is then :)
@RMartinhoFernandes no , it doesn't compile , but was thinking that way
Als
Als
@FredOverflow: use std::deque as member of the structure being passed?
That won't break the function interfaces for sure.
@MrAnubis Oh, you're asking if it's wrong to think of a pointer to a member function as a pointer to a function with this as the first parameter?
Als
Als
@FredOverflow: But there was an catch condition in that which I seem to miss out now
18:40
That's fine, as long as you don't think that a pointer to a member is a pointer. It isn't.
@RMartinhoFernandes oh yes :)
one of the structure member holds a pointer to the first element of the said global array
@Als What do you do with that pointer? Do you index into the whole array? Then std::deque won't work, because the memory isn't contiguous.
@RMartinhoFernandes grins , wasn't thinking that , thanks
Als
Als
@FredOverflow: Yes, that is how the code accesses the array.
@FredOverflow: Adds data at indexes, which is another global count of items..(crazy i know)
@MrAnubis If you want to know why they are not pointers, Raymond Chen wrote a good post about it: blogs.msdn.com/themes/blogs/generic/…
18:42
@Als How can you add data to an array? An array always has the same size.
Sounds you should simply flush the mess down the toilet and write something beautiful from scratch.
Als
Als
@FredOverflow: Err...fills the elements in the array
And you can't replace those pointers with iterators?
Als
Als
They would invalidate once vector relocates, right?
And I wouldn't know when that would happen
Yes, but not with std::deque. Those iterators remain valid, because std::deque never reallocates.
std::deque is often a better choice than std::vector. I need to get this habit ingrained, but it's being hard.
18:45
I have never used std::deque even once in my life so far :)
Herb Sutter has a nice article about it, IIRC. Or was it Scott Meyers?
Als
Als
right, So replace global array by std::deque and maintain an iterator as structure member of the structure that is being passed around?
18:46
@FredOverflow iirc, deque iterators are invalidated when it resizes. Pointers are not :)
@jalf Pretty sure they're not invalidated.
@jalf Do deques actually resize?
@FredOverflow well, they are ;)
I've been tripped up by that before
@jalf Dang. Quote?
23
A: Iterator invalidation rules

Tomalak Geret'kalC++03 (Source: Iterator Invalidation Rules (C++03)) Insertion Sequence containers vector: all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidate...

18:48
the problem is that the deque is basically a vector of arrays. So there's a small "index" vector with pointers to the individual pages/arrays. An iterator has to be able to locate this index, in order to iterate the entire deque
and this "index" can be reallocated when it resizes
at least, I'm guessing that's the use case that requires iterators to be invalidated
@Als Screw my std::deque plan :(
pointers are fine, because the actual elements stored in the deque are never moved
Als
Als
@RMartinhoFernandes: That answer says Non.
@Als But it says references (and thus pointers), are okay.
@jalf But a pointer to the beginning of the deque won't get you anywhere else :(
Als
Als
18:49
yes @RMartinhoFernandes
@FredOverflow exactly :)
I want my cake and eat it too :(
Oh wait, don't listen to what I'm saying.
@FredOverflow Woa, woa, caps.
what are we trying to do again?
I didn't follow that part of the conversation :)
18:50
16 mins ago, by Als
I have a procedural C++ code(Yikes I know).This code uses a global fixed sized array to keep track of some say X type of items.This array is being used in a clumsy way in the code, an structure is passed as function parameter across control paths and one of the structure member holds a pointer to the first element of the said global array.
Starts here.
Als
Als
@RMartinhoFernandes: Geez, right when I am in a problem I tend to forget the basics!
But, a deque could work if you use pointers to the elements instead of iterators, right?
As long as insertion into the deque is always at one of the ends, pointers remain valid.
@EtiennedeMartel But you can't get to the other elements if you only have a pointer, because std::deque isn't contiguous.
what was wrong with a pointer to the data structure + an integral offset?
18:52
No idea. I didn't follow everything.
Yeah, you could just lug around a std::whatev * and an index in that struct.
Als
Als
@jalf: I didn't follow that one...I guess @LucDanton suggested it but I somehow missed it
@RMartinhoFernandes Why does that answer only mention iterators and references, but not pointers?
Wait... the array is global, right?
Als
Als
@EtiennedeMartel: Yes
18:53
@FredOverflow Maybe because the rules for pointers can be deduced from the rules for references?
Als
Als
@EtiennedeMartel: Dont ask why access it that way(by passing pointer to it)! they maintain a god structure which allows access...phew encapsulation..
@RMartinhoFernandes In that case, the answer should mention that briefly.
Don't look at me, I didn't write it.
Hey, I've got an idea. Just introduce some Singletons into your code base, they'll distract from the real issues!
Alright, so, if it's global, you could just replace that with a vector and keep an index. Voila, job done, drink beer.
18:55
@FredOverflow and an abstract factory
everyone loves those
@FredOverflow Read it again :)
@jalf The factory should use a state pattern.
Here we go again into the Design Pattern Rollercoaster of Doom.
and some XML
Als
Als
@EtiennedeMartel: keep an index?
18:57
This code is icky.
Oh, I just reread what you wrote.
@RMartinhoFernandes Cool. Pointers are also mentioned here, btw...
I wonder why a pointer must be kept around considering the 'effin array is global.
@EtiennedeMartel The state pattern should use a union, and you should call it "state of the union".
Als
Als
4 mins ago, by Als
@EtiennedeMartel: Dont ask why access it that way(by passing pointer to it)! they maintain a god structure which allows access...phew encapsulation..
:(
"phew encapsulation"? lol
@EtiennedeMartel good point
Als
Als
18:58
Their idea of whatever those C moguls
lol
@Als Did you mean "few encapsulation"? Because "phew" is more like a sigh of relief...

« first day (381 days earlier)      last day (4793 days later) »