Segmentation faul, again - vector

Can you help me, I have a problem on this code and I can't fix it. It says: Program recived signal SIGSEGV, Segmentation fault, but it don't shows me at what line it does appear. Please help!
#include<stdio.h>
int main(){
freopen("puncte5.in","r",stdin);
freopen("puncte5.out","w",stdout);
int t[5001][5001],cx,cy,n,k,x,y,l,s,mx=0,my=0,lmax=32001;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
t[x][y]=1;
if(x>mx)mx=x;
if(y>my)my=y;}
for(x=1;x<=mx-1;x++)
for(y=1;y<=my-1;y++)
for(l=1;x+l<=mx&&y+l<=my;l++,s=0)
for(cx=x;cx<=x+l;cx++)
for(cy=y;cy<=y+l;cy++){
s+=t[cx][cy];
if(s>=k)
if(l<=lmax)
l=lmax;
}
printf("%d",lmax);
return 0;}

Have you tried
if l< lmax
instead of
l<=lmax

Related

Empty Output When I Put Scanf in C

I made a simple code in VS Code, hello world, then I run and it worked. Next, I added an input, scanf. But when I run the code, the output is empty. The output doesn't show anything and only says "running". Please help me solve this.
First code (success):
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Second code (failed):
#include <stdio.h>
int main()
{
int pin;
printf("Hello World");
scanf("%d", &pin);
return 0;
}
My code just accept printf, not scanf. If I add scanf, the output is empty and keep running. If I remove scanf and only printf in my code, is successful.
The printf function writes to stdout. When stdout is connected to a terminal or console, it's line buffered.
Line buffering means that the output is actually written to the terminal/console on either of three conditions:
The buffer is full
The buffer is explicitly flushed with fflush(stdout)
A newline is printed.
Your output:
printf ("Hello World");
doesn't fulfill any of the three conditions.
Simple solution is to add a trailing newline in the output you print:
printf ("Hello World\n");
Well you have not told your code to actually do anything with the scanf, that's maybe why nothing has come up on your terminal. scanf only takes input from the terminal, but it will not spit it out without you telling it to.
you will need to include this line of code for that to happen:
printf("%d", pin); // underneath your scanf line.

Assigned a value that is never used (simple program)

#include <stdio.h>
#include <conio.h>
int main ()
{
int numc;
puts ("NUMBER PLEASE");
numc=getchar();
printf ("%d");
getch ();
return 0;
}
I get the warning numc is assigned a value that is never used, while I'm trying to get the value. Please help.
Did you mean:
printf ("%d", numc);
?
That would use the value the the compiler is warning you about.
you never used numc value after assigning.. that's why it is giving warning..

assigning NULL to a struct pointer gives error and meaning of segmentation fault

I have this simple code of c,c++, which runs fine:
struct node{
int x;
struct node* next;
};
int main(int argc, char *argv[])
{
int a,b,c;
struct node *root, *node1, *node2, *leaf;
root = malloc( sizeof(struct node) );
//root = NULL;
root->next = 0;
root->x = 12;
system("PAUSE");
return 0;
}
Now if I assign root = NULL or 0, instead of malloc, it gives me this error-
"An access violation (Segmentation fault) raised in your program"
While in the next line i could assign 0 to next pointer. Please explain
In this forum I read segmentation fault occurs when we try to access secured memory space, while operating system defines it like a page fault when the required data is not found in memory. Are they related ? (SF of C, C++ and SF of Operating System)
If you set root = NULL;, it will do exactly that, and will not generate a segmentation fault.
The segfault happens on the next line, when you try to dereference the NULL pointer.
If you comment out the two lines below it, you will observe no segfault.
You cannot and should not dereference A NULL pointer.

QThread with pointer to pointer

I have a problem with QThread.
I did some calculation in GUI thread and it worked.
But now I try to do a calculation in work thread and there is a runtime error.
A have this function.
double **matrix(int nx,int ny, int shift)
{
int i;
double **m=(double **)calloc(nx+1, sizeof(double*));
for (i=0;i<=nx;i++) m[i]=(double *)calloc(ny+1,sizeof(double))+shift;
return m+shift;
}
And in run() function a want to do this:
double **lop=matrix(1,2,3);
But in this line there is this error: SIGSEGV Segmentation fault.
A don't understand, why the error is only in the work thread. Because when run this in GUI thread, it works.
Excuse me please my bad english.
You can't shift by 3. You must have things aligned to the word boundaries. Words are two bytes so the shift can only be an even number. Perhaphs you want
return m + shift*sizeof(double*);

SIGSEGV while adding to QListWidget

I have a problem with adding an element to a QListWidget. I have build some frame with QtDesigner and then, I want to add some elements to a list in code. Even when I write:
QListWidgetItem* i = new QListWidgetItem("text");
Q_ASSERT(stepsList);
qDebug() << "before";
stepsList->addItem(i);
qDebug() << "after";
It prints only "before" and crashes with SIGSEGV. Additionaly, I managed to get such error message with this:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6f2a4a4 in QListWidget::count() const ()
from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
What is the reason?
Well, acceptation is needed so I'll write what was wrong:
I needed to call setupUi() first, in order to initialize the stepsList as #Timo Geusch wrote.
Solved.

Resources