« first day (1366 days earlier)      last day (1470 days later) » 

11:12 AM
Variable-length array types shall not be used

Category Required
Analysis Decidable, Single Translation Unit
Applies to C99

I am working on above Misra rule 18.8

void f ( int16_t n )
{
uint16_t vla[ n ];
}
void g ( void )
{
f ( 0 );
f ( -1 );
f ( 10 );
}
what I have understood from the above example that n should be defined and it must be greater than zero.

but what if n is user define as shown in the example.
#include<stdio.h>
int fun(int arr[]){

}
int main(){
int n;

scanf("%d",&n);
int arr[n];
f ( 0 ); /*Non complaint */
f ( -1 );/*Non complaint */
f ( 10 );complaint */
 
I don't understand, it says don't use them, like at all
 
If the size of a variable-length array is negative or zero, the behavior is undefined.
so yes don't use them
 
no, the rule says don't use any VLA whatsoever, doesn't it?
 
can you please read this
this is for example
 
right, it says using any VLA is noncompliant. But here's some examples of undefined behavior to illustrate why we made this rule
"There is no use of variable length arrays that is compliant with this rule"
 
11:29 AM
ok but I am writing a checker for this rule and I am confused does it is decidable when n is user define?
 
if you're writing a checker you just need to check for any VLA
 
so you mean value of n does not play any role?
 
12:15 PM
@sunil if "n" is not a compile-time constant, the rule is broken
 
nwp
The rules about compile-time constants aren't exactly obvious. I recently stumbled over (1, 2) not being a compile-time constant.
 
I don't even know how to check for them in C, they don't have consteval
 
 
3 hours later…
3:07 PM
hello guys
well basically from my code i want when i type the space bar to actually not typing it
like blocking the input
somehow globally to detect this input and ignore it
if (key == VK_SPACE)
{

}
 
I think that's what SetWindowsHookEx is for
 
okey thanks
 
 
2 hours later…
5:35 PM
Doing some hackerrank challenges and soe of them are so poorly written. :
:(
 
nwp
They are. Would not recommend.
 
@nwp what would you recommend for challenges?
 
nwp
Make pong.
Libraries allowed.
 
Okay.
Just spoke to someone who thinks the states will be quarantined until the end of the year. Hmph. Still not sure how I feel about it.
 
5:55 PM
@earlyriser01 seems more like a topic for the lounge.
I doubt that. I think they'll go over to test and trace once the first wave is mostly finished
 
@PeterT speaking of which, how do I get to the lounge? I've been having to google "C++ stackoverflow chat" to even get here. How do I find the "chats" on the stackoverflow home page?
 
https://chat.stackoverflow.com/rooms/10/loungec
I don't even remember how I got there
 
Okay thanks.
 
 
1 hour later…
6:59 PM
Have anyone else stumbled across this using Mingw?
undefined reference to `_GUID const& __mingw_uuidof? How do I resolve this?
 
user6461957
7:31 PM
static char *test_unordered_map()
{
    unordered_map_t m;
    void *it = NULL;

    unordered_map_init_with_size(&m, 1024, 1024, 1000);


    unordered_map_emplace(&m, "strawberry", "Erdbeere");
    unordered_map_emplace(&m, "apple", "Apfel");
    unordered_map_emplace(&m, "water melon", "Wassermelone");
    unordered_map_emplace(&m, "pear", "Birne");


    for (it = unordered_map_begin(&m); it != unordered_map_end(&m); it = unordered_map_next(&m, it))
        printf("%s %s\n", (char *)it, (char *)it + m.key_size);
 
user6461957
Damn my unordered_map is a space waster
 
user6461957
The data layout looks like this:

[valid_byte;(key, value) |valid_byte;(key, value) |valid_byte;(key, value) |...|valid_byte;(key, value) ]
 
user6461957
It's an array with buckets of size 1 + key_size + value_size.
 
user6461957
At least my output is correct:

water melon Wassermelone
apple Apfel
pear Birne
strawberry Erdbeere
 
user7659542
How does it come this leads to stack smashing?
 
user7659542
7:36 PM
#include <stdio.h>

int main(void)
{
    int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int (*parr)[10] = &array;

    fprintf(stdout, "0: %d\n", **parr);
    fprintf(stdout, "1: %d\n", ++**parr);

    *(parr[1]) = 44;

    fprintf(stdout, "0: %d\n", **parr);

    return 0;
}
 
parr[1] points to the space after array
so writing to that space seems like a bad idea
 
user6461957
I don't get why you do **parr
 
it's the same as parr[0][0] or (*parr)[0]
 
user6461957
Ah, I forgot that he did this &array;
 
For a template, what am I suppsed to do with this `::name` thing?
` cout << Traits<Fruit>::name(index2) << "\n";
`
 
user6461957
7:46 PM
@PeterT, should I use another data layout to save memory?
The previous one is cache friendlier though...
 
user6461957
I can allocate each bucket separately, but this will mess up the cache performance
 
@d03 no idea what your goals are, you said you wanted it in a single array and I don't remember the details of a hash map on top of my head to have a qualified opinion
 
user6461957
Do you find it useful?
 
user6461957
Just need an opinion here...
 
like useful for what? If you have a metric you want to optimize for then measure it.
 
user6461957
7:50 PM
Let's say practicality.
The problem with my layout is that if you have just a few buckets to fill, but your map's capacity is like 1000.
 
you can always make it resize dynamically, it's just going to be a lot more code
and will obviously invalidate pointers on resize
 
user6461957
True. So, I guess I can keep that layout...
 
user7659542
8:07 PM
@PeterT of course! stupid me!
 
user7659542
tvm
 
9:19 PM
There are called lambda functions???

void freefunc(QByteArray a){std::cout << "freefunc" << std::endl;}

struct functor{void operator()(QByteArray a){std::cout<<"functor"<<std::endl;}};

auto lambda = [](QByteArray a)->void{std::cout << "lambda" << std::endl;}

int main()
{
std::function<void(QByteArray)> a = &freefunc;
std::function<void(QByteArray)> b = functor();
std::function<void(QByteArray)> c = lambda;

}
 
@earlyriser01 whut?
 
So I'm writing a confluence page, and I can't remember what these kinds of functions are called
Here's another example:
_listener->setAckCallback([&](QByteArray msg){
return _ackCallback(msg);
});
 
oh, I guess callable or invokable is the most generic category
 
But can I call them lambda functions?
 
oh, yeah those are lambdas
or closures
 
9:21 PM
Okay. Thanks
if a company requires a listener to be written a certain way - would that be called a coding style? Or a coding template?
 
I would say style is more widely used, template is used in too many other contexts
 
Okay. Thank you.
@OnlyTheParanoidSurvive nice user name
 
@earlyriser01 Thank you! By the way, Pluralsight makes its videos free only in April. Please download as many as you can. :-)
 
What is Pluralsight?
My storage is full on my personal computer
 
pluralsight is a company that makes a huge amount of tutorial videos (mostly about programming) with high-class mentors.
 
9:28 PM
ohhh yeah I've heard of it.
Interesting. Thanks for the tip.
 
9:45 PM
Can I throw an exception from the ctor of an exception? I assume yes?
 
What is ctor
 
constructor
 
Hmm I think so.
 
@ThomasJohnson You can, but you'd typically rather avoid it.
 
Yeah working on some third-party code
 
9:54 PM
Can somebody please show me what I'm doing wrong here?

#include <iostream>
using namespace std;
enum class Fruit { apple, orange, pear };
enum class Color { red, green, orange };

template <typename T> struct Traits;
Traits<typename T>::name(int index){
if(is_same<T, Color>::value){
cout << " is Color class " << endl;
}
if(is_same<T, Fruit>::value){
cout << " is Fruit class " << endl;
}
}

int main() {
int t = 0;;
std::cin >> t;

for(int i = 0; i != t; ++i){
int index1; std::cin >> index1;
int index2; std::cin >> index2;
It's with my name function
 
you never set a type for the template
 
I'm not sure I know what you mean.
 
oh wait that's member function of a class template
I thought it was function template
 
Like do I need a
Traits<Color>::name(int index){
}
and a
Tratis<Fruit>::name(int index){
}
?
Oh okay.
 
I'm kind of confused how you're trying to forward declare the struct without the function
 
9:56 PM
Ah. It's a hackerrank challenge. So the code came that way.
 
yeah, I have no idea what's up with that code
 
It's okay.
Me either.
 
I don't see a return type on the function, I don't see the struct definition
 
Okay. So this is what is given originally:

include <iostream>
using namespace std;
enum class Fruit { apple, orange, pear };
enum class Color { red, green, orange };

template <typename T> struct Traits;


int main()
{
int t = 0; std::cin >> t;

for (int i=0; i!=t; ++i) {
int index1; std::cin >> index1;
int index2; std::cin >> index2;
cout << Traits<Color>::name(index1) << " ";
cout << Traits<Fruit>::name(index2) << "\n";
}
}
 
which part of that was the predefined part?
 
9:59 PM
And i'm supposed to add the missing parts in.
All of it is the predefined part.
I'm supposed to add in the name() ??
 
template <typename T> struct Traits
static int name(int a){return 0;};

should at least compile
 
Huh okay. I'll try that. Thanks
Nope. That doesn't compile either.
 
what's the error, is it already defined in some hidden header?
 
This is the error:
classTemplateSpecialization.cpp:27:26: error: incomplete type ‘Traits<Color>’ used in nested name specifier
cout << Traits<Color>::name(index1) << " ";


It's printed twice
 
oh I forgot the openen braces
template <typename T> struct Traits {
static int name(int a){return 0;}};
 
10:03 PM
Huh?
Ohh
 
huh somehow the openening and closing braces got lost in that message
 
It's okay, I figured it out :)
Thanks for the tip. It totally helped.
 
10:14 PM
Oh my. They won't let me define the function in the Traits class
 
 
2 hours later…
11:46 PM
Figured that out.
 

« first day (1366 days earlier)      last day (1470 days later) »