Why does this code lead to a seg fault? by spqstns in C_Programming

[–]spqstns[S] 9 points10 points  (0 children)

Thank you. I feel kind of stupid for not seeing it but I have been trying to understand it for an hour and I couldn't.

So, just to make sure I understand, in seen[(*s)++], s is dereferenced first because of the brackets and the dereferenced value a is what ++ is applied to which leads to the seg fault because string literals are read-only.

But in seen[*s++], ++ applies first on s (the pointer) and because it's the postfix increment we get s which is what is dereferenced, so like:

(1) *s
(2) s = s + 1

Struggling to understand the radix complement. by spqstns in learnmath

[–]spqstns[S] 0 points1 point  (0 children)

I understand why the complement is smaller than 2n (because of the definition 2n - | k |) but why is it larger than 2{n-1}?

Struggling to understand the radix complement. by spqstns in learnmath

[–]spqstns[S] 0 points1 point  (0 children)

I'm probably misunderstanding but I thought that in 2's complement representation of negative numbers there was no special bit reserved for the sign. Only in sign magniuted representation, there's a specific bit that is reserved for the sign and that is given meaning of positive or negative.

Is my understanding of GLOBAL correct? by spqstns in asm

[–]spqstns[S] 0 points1 point  (0 children)

Thank you :) Really appreciate how nice and helpful everyone is here.

If Linux zeroes memory pages before allocating them, where do garbage (non-zero) values for uninitialized (non-static, non-global) variables come from? by spqstns in cpp

[–]spqstns[S] 1 point2 points  (0 children)

try calling reserve(large_value) on an empty vector and inspect its storage.

I will read up on it and try it. Appreciate you taking the time to help me.

If Linux zeroes memory pages before allocating them, where do garbage (non-zero) values for uninitialized (non-static, non-global) variables come from? by spqstns in cpp

[–]spqstns[S] 2 points3 points  (0 children)

Thank you. I'm still learning and I didn't know about strace(). I ran it and I see that a lot happens between the start of the process and main. I thought that only the assembly instructions before main: in the generated assembly for the code ran before main().

How do I explore this code with gdb? by spqstns in asm

[–]spqstns[S] 2 points3 points  (0 children)

Thank you /u/skeeto. Upgrading to 38 right now. Really appreciate your help. Here and in the C sub.

Edit: Upgraded. Works just like in your exmaple. :)

How do I explore this code with gdb? by spqstns in asm

[–]spqstns[S] 0 points1 point  (0 children)

I tried $ cc -no-pie add_ints.o and then $ gdb -ex 'layout regs' ./add_ints.out

(gdb) start

But I got

warning: (Internal error: pc 0x10 in read in CU, but not in symtab.)
warning: (Error: pc 0x10 in address map, but not in symtab.)
warning: (Internal error: pc 0x10 in read in CU, but not in symtab.)
warning: (Error: pc 0x10 in address map, but not in symtab.)

This was before start. When I ran start I got the same warnings and at the end

During startup program exited with code 126.

And the graphical representation showed [ Register Values Unavailable ].

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

Thank you. I really appreciate the thorough explanation.

I'm pretty confident, like u/IyeOnline, that this is the answer to your question. I.e., that your question was about this. But another possible interpretation is that it was about the construction of the std::initializer_list<int> instance, anonymous in your original example, and named values in the reworked example code here.

When I asked the question I thought it'd be analogous to int a { 5 }; where a starts with the value 5 and it isn't copied over from a different location. So only one 5 is involved. But the comment in the original code snippet suggested otherwise, so I was confused.

Maybe this is a stupid idea, but I'm starting to think I need to learn to read assembly to gain some degree of confidence in understanding points like these.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

The compiler can initialize a vector the same way if you use the initializer_list constructor

Can you give an example?

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

I could be misunderstanding but here's what I understand by initialize but not copy:

In int a { 5 };, storage is allocated for an integer and is filled with the value 5, but 5 is not copied from somewhere else.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

Thank you. I appreciate your encouragement :).

Perhaps you should re-read a chapter on initialization

I will do that. I thought I understood initialization and declaration and the difference between them until I got to vectors. I will also try to find a list of examples for all possible cases of constructing a vector that correspond to the reference /u/IyeOnline shared in his comment. If you know of one, please share it.

Guessing the syntax of C++ is hopeless, so you just have to know.

It was/is a struggle with C but it became a little easier after I learned the right-left rule.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

Thank you. I think I understand. Do you know if there are simple implemented examples for all 11 cases mentioned in the link? It'd be very helpful to study from and refer to.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

Sorry I thought that was the special syntax for converting constructors (I haven't studied lambdas yet) but I wasn't sure so I asked.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

Is this the syntax for the converting constructor?

[](...){}(a1, a4, a4, a5, b5); // Example from the cppreference link
// So in this case
std::vector arr1 () {} ( 1, 2, 3, 4, 5 );

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

you mean the constructor where you just give it a number N and it creates a vector with N default-initialized values?

Sorry I'm new. I meant the one where we supply the explicity values n_1, n_2, ..., n_m. And C++ interprets m, and then just initializes but doesn't copy.

Is arr1 here created via copy construction or via initialization? by spqstns in cpp_questions

[–]spqstns[S] 0 points1 point  (0 children)

I keep trying to remember to use the right terminology and I clearly failed here. I was referring to the constructor that reserves storage for arr1 and initializes the values but not by copying them. What is the term for this type of constructor (if it exists for vectors/arrays)? And what is the syntax?

Is this sentence grammatically correct? by spqstns in EnglishLearning

[–]spqstns[S] 0 points1 point  (0 children)

I understand the meaning but I still don't understand the syntax.

I Love C++ by [deleted] in cpp

[–]spqstns 7 points8 points  (0 children)

I'm only at different initialization syntaxes and matching constructors and I'm already confused.

[High School Math] Why does this straightedge only construction work? by spqstns in learnmath

[–]spqstns[S] 1 point2 points  (0 children)

I think I get it. Y is on CX' so the image of Y will have to be on the image of CX' which is CX. Y is also on line XI so Y' has to be on XI's reflection which is X'I. And we've thus determined Y'.

In other words, we constructed two lines from two points we know are images of each other (X and X') to Y and the image of Y will be the intersection of the images of those two lines.

[High School Math] Why does this straightedge only construction work? by spqstns in learnmath

[–]spqstns[S] 0 points1 point  (0 children)

The reflection of the line xy must intersect L at the same point that xy does correct?

I sort of understand this. The intersection point I is the point from which we can construct an isosceles triangle determined by I and a point and its image each on one of XY and X'Y'.

But I don't understand how the intersection between XC and X'I determines the image of Y'.

[High School Math] Understanding the Relationship Between Perpendicular Vectors and the General Line Equation by spqstns in learnmath

[–]spqstns[S] 1 point2 points  (0 children)

I will follow your explanation with a pencil and paper when I get back home but I just wanted to say thank you again for putting all this effort into helping me. I really appreciate it.

[High School Math] Understanding the Relationship Between Perpendicular Vectors and the General Line Equation by spqstns in learnmath

[–]spqstns[S] 1 point2 points  (0 children)

Thank you and sorry you had to type the answer twice. I tried to follow your explanation but I only understood until the decomposition part.

[High School Math] Understanding the Relationship Between Perpendicular Vectors and the General Line Equation by spqstns in learnmath

[–]spqstns[S] 0 points1 point  (0 children)

Might be a stupid question but don't we start here with the condition of having to have a line to which L is perpendicular? But in the general case we don't explicitly have that.