« first day (1369 days earlier)      last day (1475 days later) » 

9:41 AM
Hello everyone, I'm reading through some code not written by me. It looks like the following:
class MyPair {
  public:
  int firstNumber, secondNumber;
  static std::string printMyPair(const MyPair& arg) {
     std::stringstream ss;
     ss << arg.firstNumber << ":" << arg.secondNumber;
     return ss.str();
  }
};
which in my opinion is a bit wrong, first it is a class with everything public, so I would just declare it as struct and secondly I don't see the point of having printMyPair as a static function
I would simply replace it with
struct MyPair {
   int firstNumber, secondNumber;
   std::string printMyPair() {
     std::stringstream ss;
     ss << firstNumber << ":" << secondNumber;
    return ss.str();
   }
};
is there any benefit of the first version over the second that I'm missing? If you had to use the first version it would like
MyPair myPair{1,7};
std::cout << MyPair::printMyPair(myPair) << std::endl;
while the second would look more like
MyPair myPair{1,7};
std::cout << myPair.printMyPair() << std::endl;
 
 
1 hour later…
10:53 AM
9 messages moved from Lounge<C++>
 
Hello? Has anyone invited me?
 
Why does in std::lower_bound when we use it with 4th argument comp, why does comp needs to be a binary predicate since the second argument in comp supposed to represent 3rd argument in std::lower_bound ?
So I'm talking about this version:
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
 
11:09 AM
struct {
    char a[3];
    short int b;
    long int c;
    char d[3];
};
Why this structure is 24 bytes with padding?
(3+1) + 2 + 8 + (3+1), I expect it to be 18 bytes.
 
isn't long int 4 bytes?
okey it depends I guess
 
hmm. long int is 8 bytes on my 64bit Ubuntu
 
what about short int ? Maybe it's 4 for you?
 
2 for me
 
@Rick I can't ptovide an answer, but you could try to remove d, then test again, then c, then test again, then b, then test again... You would see wherr memory goes!
 
12:04 PM
@Rick why the (3 + 1)?
 
2 bytes aligned or 4 bytes aligned ?
I've seen answers that saying that padding is implementation-defined but most compilers tends to be consistent with each other.
 
user6461957
When I see a struct like this, I first look for the biggest member element, then get its size, and think of that size as a bucket which holds all the remaining ones.
So, here is an example:

The biggest element is the long int with 8 bytes (on most environments), now I pack the 3 bytes (from the char array) and the 2 bytes together into the 8 byte bucket mentioned earlier. Now, we have 16 bytes so far. The last one is a 3 byte char array, so this will be stuffed into another 8 byte bucket as well, thus 24 bytes.
 
user6461957
IOW: the biggest member element inside the struct dictates the bucket size. And all the remaining ones have to be fitted in multiples of these buckets.
 
user6461957
(The compiler works that way, don't ask me why.)
 
user6461957
(You can also turn that off with some odd compiler settings)
 
user6461957
12:13 PM
(Er, my explaining sucks, @PeterT, please help out, I am too dumb too explain it.)
 
@Rick because the standard sets no requirements on alignment IIRC, other than alignof
also because there are at least two bytes you're missing between the short and the long
@d03 depends but mostly "right" usually compiler writers encourage you to order things by sizeof and alignof. So if I was doing this I'd go long, then both char arrays then the short
 
You could use something like offsetof to see where it puts all the padding.
My guess is
3 + 1
2 + 2 (to get to 8 for the long)
8
3 + 5 (to get it aligned so that in an array of this struct the next one also has the long properly 8 byte aligned)
 
@PeterT See my coliru link above
 
let me see
 
1:03 PM
Hey guys, a short question: assuming I hav a function where A object is created at the heap: Testobject obj = new TestObject(); and some items are added to a list here. Then I fromm this funtcion I call another function . Here, in function 2, I want to check if my list is empty, if so, then call function 1 again
So, if the first function is called again, then the statement Testobject obj = new TestObject() is executed again that means, that my first object still exists and now I have a second one, right?
And that means, that I want to delete the existing object before I create a new one right?
 
if you call new twice you'll have two objects, right
 
so my statement in function 2 will be if(list is empty) then delete obj; call function 1 again;
is it enough to call the destructor or do i have to write the delete statement or how to delete an object ?
 
if you want to free the memory you have to call delete
but this sounds like a lot of unnecessary manual memory management
 
And if I only call the destructor?
what do you mean?
 
then you just called a function, you didn't free any memory
 
1:06 PM
Ahh
I create this on the heap because I need my object exists the whole program lifetime long therefore I want to do this
 
I mean you should consider having the value be stored by value somewhere or if needs to be a pointer, store it in a unique_ptr or something like it
 
Actually my object is a DeviceDiscovery object for Bluetooth Devices, if the discovery ends up with 0 devices then it should run again
Okay, I klnow what you mean
I saw then in the Header file I have Testobject obj*
and in my function I have obj = new TestObject()
 
nwp
1:26 PM
Avoid using new. It is very error-prone. Use values directly or in an emergency std::make_unique and std::unique_ptr.
 
1:47 PM
Okay, but how can UI check if a object does exist or not? because if(obj) does not work for me
 
if objis a unique_ptr it would work :P
 
Haha, so istead of TestObj obj* = new TestObj() what would it be with uniqe ptr? I found something weird on the internet that does not look easy ;d
unique_ptr<int> uptr (new int(3)); so in my case unique_ptr<TestObj> uptr (new TestObj());
 
any good soul wants to check out my code to find a logic error ? pls (need for university) (C,linked list using array)
 
I'm not a good soul but I am perpetually procrastinating, so I'll take a stab at it
 
how/where do i send u the code ?
 
1:59 PM
Post it in a gist or some other paste-site
 
You can just quickly paste printf and var names in google translate
I forgot to say that all functions and typedef struct are provided by a professor, i just needed to write main() part of the code
 
you don't initialize zadnji before reading it for the first time
 
when i wrote "int zadnj:8i;" there was still alogic error
the problem is it says that the list is full or that the poition doesnt exist
this part triggers when it shouldnt :
else if ((pozicija_ubacivanja>pokLista->zadnji+1) || (pozicija_ubacivanja<0) )
 
you didn't initialize zadnji anywhere, what do you think its value is?
 
Idk
Like i said, Everything except main() was provided by a university professor, my job is just to write main()
The program should be an array implementation of a "linked list"
 
2:13 PM
I didn't tell you where to inialize it, I suggest you initialize it
 
when i wrote "int zadnj:8i;" there was still a logic error
 
what is that syntax?
are you trying to declare a bitfield or something?
 
typedef struct{
int zadnji;
int elementi[MAX];
} Lista;
there
is that not how you do it
 
no, that's not it
you can't initialize it in the struct definition in C
 
how do i initialize it in main ?
 
2:15 PM
the same way you set any other variable
 
but zadnji is used in a function
I did it but it still says that the list is full
 
2:28 PM
@mato200 don't know what to tell you, it works for me
 
Hmmm, can you delete all the stuff in main() and just call the functions, after all thats all I have to do; void ubaci is for inerting elements in the list, void obrisi is for removing element from a specific position in the list, and int trazi searches the list for a specific element
I repeat: everything except for main() was provided by the professor
 
and I repeat, all the errors I found was unitialized values
you can initialize the values in main just fine
 
and i did, but there was still a logical error
when i started to enter number in the list it say that the list is full
 
and you are setting
lista.zadnji=0;
right?
 
Ok, now the function that searches the element is not working
lista.zadnji=0; helped
 
2:34 PM
because you don't actually use the result from the search function
 
but it returns the value
and i printed it
 
you don't store that returned value anywhere
 
return pozicija;
temp = pozicija;
printf("Pozicija je %d", temp);
i did return it
and stored id in temp
and printed temp
 
the pozicija inside the function is not the same as the one in main
 
it always prints as 0
 
2:35 PM
they are just two variables with the same name
they don't magically share a value
 
is return suppose to return the value from function to main() ?
 
no, return is supposed to return the value from the function call
if you do "f(parameter)", then you can get the returned value from "f" by assigning it to a variable like "somevar = f(parameter);"
 
i am working on it, its getting near to working good
I just did printf directly in the search function
TY VERY MUCH, I got it working
btw do you know how to print out the whole list ?
 
 
2 hours later…
4:36 PM
@PeterT ah I see thanks
@Mgetz ok I see your example. I think Peter's guess is fairly reasonable. There's no alignof in C and I guess maybe alignof just tell you what the "largest boundary" the compiler would choose, which C doesn't tell you directly.
 

« first day (1369 days earlier)      last day (1475 days later) »