Clion strange code indentation - c++

I am using Predefined style -> Allman braces and this is the strange behavior I get:
// this looks good
int edges = 0;
long variable_2 = 0;
AnotherLong obj = 0;
// this looks bad way off
int edges = 0;
long variable_1;
long variable_2 = 0;
AnotherLong obj = 0;
The variables should be grouped in columns, however an empty line should indicate a new "block" of code, right ? On the first case int edges = 0; is aligned fine, why not in the second case ? How can I fix that ?
Edit: Created a bug report

Related

How to return 2D Array without causing Segmentation Fault (C++)

A homework assignment for an introductory C++ course.
The task is to search a two dimensional array for two numbers adding up to a given number 'search_sum.' Enter all found addends as {startRow,startColumn,endRow,endColumn} in the array 'summations,' then return the array 'summations'.
The line with summations[*sums_found] = new size_t[kIndices_size]; in the if statement was written by the instructor, as well as the comment. I am under the impression that this allocates a new space in memory where data can be assigned to and stored, so, in the next line of code, I attempted to take the column and row variables from the for loop and place them in the newly allocated memory as such.
summations[*sums_found]={startRow, column, endRow, column};
This threw a 'too many assignments' and 'segmentation fault' error.
I assumed you just could not do it this way, and data must be added to a 2D array in another fashion. I removed the faulty assignment code and ran the code as shown below through a debugger, just out of curiosity. The error thrown was once again a Segmentation Fault.
// Parameters:
// - matrix: a two-dimension integer array
// - matrix_size: a two-element size_t array storing sizes of matrix
// - search_sum: the integer value for which the function seeks sums
// - sums_found: an OUTPUT PARAMETER pointing to a size_t
//
const size_t** FindSum(const int** matrix,
const size_t* matrix_size,
int search_sum,
size_t* sums_found) {
*sums_found = 0; // init sums found to 0
size_t summations_size = 2; // start assuming no more than 2 summations
// build structure to hold all found summations
size_t** summations = new size_t*[summations_size];
switch (search_sum)
{
case -92:{ //column search
for(size_t column = 0; column < matrix_size[1]; column++){
for(size_t startRow = 0; startRow < matrix_size[0]; startRow++){
for(size_t endRow = 0; endRow < matrix_size[0]; endRow++){
int j = matrix[startRow][column];
int k = matrix[endRow][column];
int sum = j + k;
if(sum = search_sum){
summations[*sums_found] = new size_t[kIndices_size]; // only done when summation is found
*sums_found++;
}
}
}
}
}
break;
case 60:{ //row search
for(size_t row = 0; row < matrix_size[0]; row++){
for(size_t startColumn = 0; startColumn < matrix_size[1]; startColumn++){
for(size_t endColumn = 0; endColumn < matrix_size[1]; endColumn++){
int j = matrix[row][startColumn];
int k = matrix[row][endColumn];
int sum = j + k;
if(sum = search_sum){
summations[*sums_found] = new size_t[kIndices_size]; // only done when summation is found
*sums_found++;
}
}
}
}
}
break;
case 1203:{ //desc/ascending diagonal
}
break;
case 412:{ //single entry
}
break;
default:{ //large array
}
break;
}
return const_cast<const size_t**>(summations);
}
I did not know what this was, so I researched the error and found that you are not allowed to perform a read/write action on read-only code, which makes sense. What I do not understand is what exactly makes this code read-only, when it seems like its function is to assign a new space for data to be assigned to, which (to me), sounds like a 'write-like' action? I more than likely am misunderstanding the full scope of the codes function, and I am further confused with as to how I should go about assigning the data to the summations array.
Also, this is a university course taught by a grad student who is (seemingly) less than well versed in c++. It is a very 'teach yourself' type class. I understand that this assignment is an exercise on pointers and references, but it feels like I am very poorly equipped to solve a problem like this, and I am unsure what exactly to research and study independently to improve my knowledge of this particular topic. Please, if you can tell what it is I am struggling with just by looking at the code I've written, let me know what I should prioritize my studying on.

Is there an issue with my Selection Sort code that's causing it to skip elements in an array?

While performing a selection sort in a C++ homework assignment, the array I'm using is having issues sorting data into exactly the right positions. I'm particularly confused as in last week's assignment, I was able to successfully sort two arrays at the same time with the correct order -- however, this week, there are pointers involved, though I'm fairly certain that shouldn't affect a sort referencing just the elements themselves.
So far, I've tried entering the following data in many different entry orders:
1000
500
560
750
1200
The loading seems to be fine referencing the actual putting-data-in, and the sort appears to be sorting some numbers properly (putting 500 first, for instance, most of the time), but I'm clearly doing something incorrectly for it to be failing to sort. I'll show some entries and outputs below.
Here's the code for my selection sort itself:
void selectionSort(int rentArray[], int arrayElements)
{
int minSort = 0;
int sortHold;
sortHold;
for (int x = 0; x < arrayElements - 1; x++)
{
sortHold = rentArray[x];
for (int y = x + 1; y < arrayElements; y++)
{
if (rentArray[y] < sortHold)
{
minSort = y;
sortHold = rentArray[y];
}
}
if (minSort != x)
{
sortHold = rentArray[x];
rentArray[x] = rentArray[minSort];
rentArray[minSort] = sortHold;
}
}
}
Naturally, I expect the output to go from anything to sort the array into [500, 560, 750, 1000, 1200] rather than what it's going to. Some screenshots:
https://i.imgur.com/evATWAy.png https://i.imgur.com/dzcZiAl.png
EDIT: Thank you to both #Akiva and #6502 for the help!
Putting a quick minSort = x; into the function just under sortHold = rentArray[x]; solved the problem.
In case no element is lower than sortHold you'll still check minSort for being different from x but it will be the minSort from last iteration making your code doing a wrong swap.
You need to reset minSort where you reset sortHold (minSort = x)

g++ error: stray '\177' in program

I was trying to code for following program
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9] insert and merge [2,5] would result in [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] would result in [1,2],[3,10],[12,16].
This is the relevant part of my program
here. I want to erase the few positions from the vector
then I am getting the following error
error: stray '\177' in program
intervals.erase(intervals.begin()+(p+1),intervals.begin()+(q+1));
vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) {
int n = intervals.size();
int p=-1,q=-1,a,b;
for(int i=0;i<n;++i){
if(intervals[i].start <= newInterval.start <= intervals[i+1].end)
p = i;
else if(intervals[i].end < newInterval.start < intervals[i+1].start)
a = i;
if(intervals[i].start <= newInterval.end <= intervals[i+1].end)
q = i;
else if(intervals[i].end < newInterval.end < intervals[i+1].start)
b = i;
}
int x,z;
if(p != -1 && q != -1)
x = q-p;
if(x > 0){
z=intervals[q].end;
intervals.erase(intervals.begin()+(p+1),intervals.begin()+(q+1));
intervals[p].end = z;
}
return vector
}
Did you copy that code from a website?
I managed to reproduce your result with this snippet:
const char* msg = "You can't copy this";
When copied and put on coliru here you'll get the same error code.
What I used for the above snippet in HTML code was:
<code>const char* msg = </code><code>"You can't copy this";
</code>
Note the  character I put in there.
To fix that, you can use a decent editor like Notepad++ that will make the stray characters visible:

Getting garbage value after assigning value to a variable, can't see why

I'm creating a new object of my class 'Dynamic' (not shown), which inheritates from 'Organic', which inheritates from 'Being' certain parameters such as id, biomeRow, etc.
Organic has: features_ (a struct), max_spawn_, total_spawn_, age_dur_ (an array) and current_age_.
The problem: Upon creating a Dynamic object, all values are set just right except max_spawn_. I've done my printfs both before creating Dynamic, in the creation of Dynamic and in the creation of Organic for the input value, and all of them are correct.
Features struct is right, total_spawn_ is right, age_dur_ array and current_age_ are both also right.
All of them are what I asked except for max_spawn_. maxSpawn is the value I'm passing (20), max_spawn_ should then be 20, but it isn't. All my printfs and debugging console show it is something around -858993460. I'm guessing that's just garbage, but I don't know how is it possible when all I'm doing is:
max_spawn_ = maxSpawn;
So, this is my function:
Organic::Organic(int id, int biomeRow, int biomeColumn, int biomeType, int beingType,
int object, Features features, int maxSpawn, int totalSpawn,
int age_dur[5], int current_age)
: Being(id, biomeRow, biomeColumn, biomeType, beingType, object)
{
features_ = features;
max_spawn_ = maxSpawn;
total_spawn_ = totalSpawn;
age_ = current_age;
for (int i = 0; i <= 5; i++)
age_dur_[i] = age_dur[i];
printf("\n%d\n", max_spawn_);
}
age_dur (and presumably age_dur_) are int [5] arrays. Copying like this:
for (int i = 0; i <= 5; i++)
age_dur_[i] = age_dur[i];
will overwrite something near age_dur_ with something. If max_spawn_ is adjacent to age_dur_, it's probably being overwritten with garbage.
Change the loop to:
for (int i = 0; i < 5; i++)
age_dur_[i] = age_dur[i];

C++ segfault with lists

My code segfaults on the last line here:
Level level1;
level1.generateLevel();
list<Entity>* obstacles;
//Get level obstacles
obstacles = level1.obstacleList;
Obviously there's something wrong with the way I handle the list, but I'm not sure what exactly. This is the code that handles the list.
for (int row = 0; row<6; row++)
{
level_position_x = -128;
for (int column = 0; column<8; column++)
{
level_position_x = level_position_x + tile_offset;
if (room_array[row][column] == 1)
{
obstacleList->push_front(*new Obstacle("images/ground.png", level_position_x, 768 - level_position_y - tile_offset));
}
}
}
I know that when dealing with arrays like I have in my for loop that they're often the source of crashes, but I'm fairly certain that with the rest of my code that room_array isn't the culprit. Is there something immediate that I'm missing that's causing this?