When I compile my code, I get a SigSegv error. What caused this? What needs to be changed so that the error disappears?

#include <iostream> #include<math.h> #include<stdio.h> #include<stdlib.h> #include<cstdlib> #include<conio.h > int main() { const int n=20; int i,massiv[i], Xmax=0, Nmax; printf("Spisok chisel v massive"); for(int i=0; i<n; i++) { massiv[i]=(std::rand() % 200)-100; printf ("\nmassiv[%i]={%i}",i,massiv[i]); } //---------------------------- for (int i=1; i<n;i++) if (massiv[i]<0 && massiv[i]> Xmax) //??? (massiv[i] > massiv[Nmax]) { Xmax = massiv[i]; Nmax = i; } else; printf("Maximalvoe otritsatelnoe chislo"); printf("Xmax=%i,Xmax"); printf (" at index "); printf("Nmax=%i,Nmax"); getchar(); } 

Here is the second code, everything works

 #include<math.h> #include<stdio.h> #include<iostream> #include<stdlib.h> #include<cstdlib> int main() { int i; const int n=20; float result = 1.0; int massiv[n]; for( int i=0; i<n; i++) { massiv[i] = (std::rand() % 200)-100; } for( int i=0; i<n;i++) { if(massiv[i] >1) result *= massiv[i]; else; } printf("\nMassiv soderzhit dannie chisla"); for( int i=0; i<n;i++) { printf ("\nmassiv[%i]={%i}",i,massiv[i]); } printf("\nProizvedenie vseh polozhitelnih chisel massiva ravno ") ; printf("\nresult=%5.0f",result); getchar(); } 
  • one
    You announced massiv [i], but you need massiv [n] ... - AR Hovsepyan
  • Why? In a cycle, I change I, appropriately and massiv [I] ... Or do I not understand something? - Alexey
  • you also have int i. put it away - AR Hovsepyan
  • I'm completely confused right now. If I remove i and just leave massiv [n] i. massiv [20] then the randomizer of numbers will not work for me ... - Alexey
  • 3
    Probably, the error is still issued not when compiled, but when executed? :) - Harry

2 answers 2

First, read my favorite question - Why do I need to reset the variables? and understand that by default, the value of variables is usually not set.

When you write int i = 0; You can be sure of the value of i . But when you write int i; , then the value of i can be arbitrary (for the programmer, of course, for the processor it is absolutely accurate and determined at a given time).

Now consider the second half of the quest - int massiv[i]; One interesting thing is hidden here - VLA . It is normally implemented only in gcc / clang and is a feature of C. But not with ++. But for some reason she remained in the positive implementation, apparently it was too lazy. This piece allows you to declare an array with a size that is determined during the execution of the program. And he stands out on the stack. A stack is usually not much - 1mb in 32 bit mode and 8mb on 64bit.

We put the two halves together. There is some unknown number in the variable i , and we need to allocate an array of this size on the stack (in bytes, this will most likely be 4 times larger). With very high probability, the variable i will contain something more than 256 * 1024. And maybe negative. And in these cases, allocate memory will not work and there will be the above drop.

What to do? the simplest thing is not to write strange code and write as advised already - int i,massiv[n] - the size of the array should still be constant (and visual studio compiler does not even compile your code).

Or stop writing on C (and he really is written on C) and start writing on the pros. But this is evident in the next semester :)

Oh, by the way, the combination

 #include<stdlib.h> #include<cstdlib> 

a little tautology is the same header, just one sishny, the other is a plus one.

  • Thank you, I figured it out, but I couldn’t understand something ... First massiv [n] is declared, but after that only massiv [I] is used and it is also output. Is this because there should be different variables? - Alexey
  • when declared in square brackets indicate the size. When used, the index of the element in the array is indicated. We can assume that this is just homographs (they have the same spelling, but a different meaning). Clarification - massiv[I] does not display an array, but one element with index I (and therefore there is a cycle) - KoVadim

You have a lot of mistakes (including logical and aesthetic ones), so I’ll just write and compare ....

 const int n=20; int massiv[n], Xmax= std::numeric_limits<int>::min(), Nmax = 0; printf("Spisok chisel v massive"); for(int i=0; i<n; i++) { massiv[i]=(rand() % 200)-100; printf ("\nmassiv[%i]={%i}",i,massiv[i]); } //---------------------------- for (int i=1; i<n;i++) if (massiv[i]<0 && massiv[i] > Xmax ) { Xmax = massiv[i]; Nmax = i; } printf("\nMaximalvoe otritsatelnoe chislo "); printf("Xmax=%i,\n", Xmax); printf (" at index "); printf("Nmax=%i ", Nmax); 

PS when you declare

 const int n=20; int i,massiv[i], Xmax=0, Nmax; 

then i and Nmax contain garbage, and in any case, your array of unknown size will be ...

  • Why so? I in the cycle equals the value 0-19. Nmax will eventually be equal to the index of the array selected by the condition ... - Alexey
  • Thank you, figured out, but there were a couple of incomprehensible moments. 1) First, massiv [n] is declared, but after that only massiv [I] is used and it is also output. Is this because there should be 2 different values, or not? 2) What does the numeric_limits function do? Equals Xmax to the smallest value? Just nowhere did I find detailed information, only examples for which nothing really is clear ... - Alexey
  • To be brief, std :: numeric_limits <int> :: min () is the minimum value of type int. In the loop, the variable i changes its value from 0 to n, and each of these values ​​is used, so massiv [I] ... is announced in the loop, and massiv [n] is announced so that space for n elements is reserved for the array. About std :: numeric_limits there is a lot of information both in textbooks and on the Internet, but if this is difficult for you, you can simply declare Xmax with some very small value: for example, Xmax = -99999999 - AR Hovsepyan