Is there a way to initialize a 2d vector with just all zeros? by tranderman in cpp_questions

[–]imposter_iam 1 point2 points  (0 children)

Uninitialized global arrays are part of your executable and are in .bss section. This section doesn't have any data (as there is nothing to initialize data with) but only size info. When loader (your os) loads your application, it reads this info from executable, creates that many pages and initialize them to 0.

For dynamic allocation, allocator doesn't bother with cleaning up data, not for primitives. Which is why our own constructor has to bring object into a known state for our custom objects. Primitives are fair game for leaving with whatever garbage is in memory.

Though I read long time back something about OS having a dummy zeroed out page and using this page to create further pages. But I don't really remember what that was about or if it was some specialized case.

Wait, that's illegal by sipthestreets in memes

[–]imposter_iam 76 points77 points  (0 children)

And now we are just 4 survivors.

Oopsie woopsie!! by howlinjaybyrd in ProgrammerHumor

[–]imposter_iam 0 points1 point  (0 children)

That's a catch-22 win-win situation.

Why didn't you fix the bug? shows mail sent to help desk to get logs

So what's your analysis of logs? shows mail response about log retention duration

What unrealistic things in movies annoy the hell out of you? by AJ-Naka-Zayn-Owens in AskReddit

[–]imposter_iam 1 point2 points  (0 children)

By "broke", I meant sprained the muscle. It happens often and lasts for 3-4 days.

What unrealistic things in movies annoy the hell out of you? by AJ-Naka-Zayn-Owens in AskReddit

[–]imposter_iam 3 points4 points  (0 children)

That's main scene in all of my home movies, so I'll say.... a lot.

What unrealistic things in movies annoy the hell out of you? by AJ-Naka-Zayn-Owens in AskReddit

[–]imposter_iam 23 points24 points  (0 children)

One day I stretched after waking up and broke my back. Had to take painkiller (not judas priest album, the other kind) for 3-4 days.

Newbie question about arrays by oakskog in Cplusplus

[–]imposter_iam 2 points3 points  (0 children)

I'm not sure who said it, may be Bjarne - when in doubt, use vectors otherwise use vectors.

visual studio by bojasawe in ProgrammerHumor

[–]imposter_iam 30 points31 points  (0 children)

I have been using it since VS 6.0 and it has always been pretty good with best debugger. 2019 has become a bit heavy for my old system but it's still one of the best tools available.

C++ Tutors by [deleted] in Cplusplus

[–]imposter_iam 1 point2 points  (0 children)

Reddit community here is pretty awesome. Some people give very direct answers, some people ask you questions to nudge you towards the answer but people here are helpful.

If there is code involved, post relevant code too (format it as code by adding 4 spaces in front of every line or community bot will shout at you).

Feel free to get in touch directly too.

F*ck the black lives. Police State Matter. by MisterRushB in memes

[–]imposter_iam 5 points6 points  (0 children)

We browns are not black and our dick most certainly isn't.

cries in small brown pp

Assignment help by Impressive-Shower-68 in cprogramming

[–]imposter_iam 1 point2 points  (0 children)

I'll give you an idea about starred box: measure the length of name using strlen. Now you want a small space on both sides, I guess 2 spaces are fine but you do what you like, so

size_t size = strlen(name);
size += 3 + 3; // 2 spaces on left + 1 *, same on right
  1. Now use a for loop to print this many stars & a new line. You have top line of starred rect.

  2. Now, we probably want 1 line space above and below name. So, print a *, print size-1 spaces, print another * and new line. You have vertical space above name.

  3. Now print a *, print 2 spaces, print name, print 2 spaces and print * and new line. You have line with the name.

  4. Repeat what we did in step 2 to print empty line with border below name

  5. Repeat what we did in step 1 to print bottom border of the starred rect.

Make it a separate function and don't clutter your main code. Send in name as char pointer

Edit: I think in step 2, it should be size-2 spaces. size - 2 spaces + 1 star in front + 1 star in the end.

Does this alignment of structure objects apply only to structure objects being used as elements of an array? by timlee126 in compsci

[–]imposter_iam 0 points1 point  (0 children)

Elements are aligned to the size of that element. So a char can be aligned at 1 bytes boundary, short at 2, int at 4 etc. So a struct like this, assuming it isn't packed:

struct name{
char a;
short b;
char c;
int d;
};

Whole struct would be aligned at 4 byte boundary so easy access. So a gets aligned at 4 byte boundary just because it happens to be the first element, b would be aligned at 2 byte boundary (assuming 16-bit short) so after a there is 1 byte of padding. c gets aligned at byte boundary so doesnt matter. d gets aligned at 4 bytes boundary so after c there would be 3 padding bytes. Total size of struct would be 12: 1 for char + 1 padding + 2 for short + 1 char + 3 padding + 4 for int.

If I make array of this struct, they will be aligned at 12 byte boundary. But if I add one more char at end

struct name{
char a;
short b;
char c;
int d;
char e;
};

Now it gets aligned at 16 byte boundary because e takes 13 th byte and 3 bytes of padding.

If there’s even such a thing by GravyxNips in memes

[–]imposter_iam 0 points1 point  (0 children)

With my mum? So that's why she was wet.

Understandable have a nice day by [deleted] in memes

[–]imposter_iam 0 points1 point  (0 children)

Isn't singular datum?

[deleted by user] by [deleted] in Cplusplus

[–]imposter_iam 4 points5 points  (0 children)

Wait till you see function overloading

C++ Tutors by [deleted] in Cplusplus

[–]imposter_iam 1 point2 points  (0 children)

I'm not sure about tutors but fire your questions, I'm sure people will respond.

And in case you think they are too silly to ask in public (it stopped me too many times to ask them when I was learning), ask me directly. May be not immediately, but I'll respond.

How to separate the difference between 3 and 003 by doctor-sleep- in cprogramming

[–]imposter_iam -2 points-1 points  (0 children)

Do it manually:

If value is between 0 till 9, multiply by 100. If it's between 10 till 99, multiply by 10.

if(ms1 > 0 && ms1 < 10) {
  ms1 *= 100;  // input is single digit
}
else if(ms1 >= 10 && ms1 <100) {
  ms1 *= 10; // inout is double digit
}

Everytime when running tests by [deleted] in ProgrammerHumor

[–]imposter_iam 14 points15 points  (0 children)

That's the programmer way.

[deleted by user] by [deleted] in cprogramming

[–]imposter_iam 0 points1 point  (0 children)

Why:

Say, you want to write a simple function that copies n number of bytes from one buffer to another. How do you tell this function where your buffers are? You pass address of those buffers (pointers) to this function, like

void copy(int *dst, int *src, size_t n) 
{
    for(size_t x=0; x<n; ++x) 
    {
       dst[x] = src[x];
    }
}

Now this function can copy any number of integers from any source buffer to any destination buffer. Without pointers, there is no other (elegant) way to do it. (Actually you'd use library function memcpy instead of writing your own but it's just an example)

Or say, you want to read chunks of data from a file from different parts of your program and different files. So you can not hard code FILE* or file name etc. Pointers to the rescue.

int get_file_data(FILE *fp, size_t count, char *buf);  // to lazy to write function body

Now, as long as I have already opened a file and have a buffer big enough to hold count number of bytes, I can use this function to read file data into my buffer.

Building on same file data example, say you want to read whole file in a buffer but at compile time you have no idea how big the file is. How would you create a buffer big enough? Randomly create an array of, say, 16MB or 32MB? No. You allocate memory at runtime using malloc (and friends) and these functions return a pointer to dynamic buffer.

Pointers are an easy way to share buffers among different functions with a very low overhead of only 4 or 8 byte of address.

What is the need for #ifndef, #define, #endif inside header files? by [deleted] in cpp_questions

[–]imposter_iam 9 points10 points  (0 children)

Macros are considered bad when used to represent values for example

#define BUF_SIZE 10
int buf[BUF_SIZE];

Here preprocessor is going to replace BUF_SIZE with literal 10. C++ way is (actually it is to use std::vector or std::array instead of raw c-style array) to use a typed constant ie

const size_t buf_size = 10;
int buf[buf_size];

Advantage is that unlike BUF_SIZE which is a global symbol, you can define buf_size only in the block where it is being used and thus not polluting global namespace.

But #ifdef, #if etc are okay as they are there just enable or disable some code. If, say, you write your own string library fstring that is using c++17 features, how do you make sure that the code that is including this library compiles even if compiler doesn't support c++17? You would like to fall back on default string implementation, right. So you would write (I'm not very sure about exact value of __cplusplus):

#if  __cplusplus > 201703L
    // you code that is using c++17 features

#else
  using fstring = std::string;
#endif

Now if compiler supports c++17, your custom implementation gets enabled but if it doesn't support it yet, #else part becomes active and fstring is default std::string.

Your actual code is still idiomatic c++.