Error initializing XGCValues - linux

I was following this tutorial, bit stuck here:
This code doesn't compile, and the error message is
c:35: error: invalid initializer
I'm not sure what's wrong with the line
XGCValues valu=CapButt|JoinBevel;
infact, I copied it from the said tutorial. Here's the full code I have:
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
int main()
{
Display *display=XOpenDisplay(NULL);
int scr=DefaultScreen(display);
Window root_window=RootWindow(display,scr);
unsigned int width=DisplayWidth(display,scr)/3;
unsigned int height=DisplayHeight(display,scr)/3;
unsigned int border=2;
Window my_win=XCreateSimpleWindow(display,root_window,0,0,width,height,border,BlackPixel(display,scr),WhitePixel(display,scr));
GC gc;
XGCValues valu=CapButt|JoinBevel;
unsigned long valmask=GCCapStyle|GCJoinStyle;
gc=XCreateGC(display,my_win,valmask,&valu);
XDrawLine(display,my_win,gc,5,5,20,20);
XMapWindow(display,my_win);
XFlush(display);
sleep(10);
return 0;
}
Thank You

The example in the tutorial is wrong - if you look in <X11/Xlib.h> or read the XCreateGC man page you'll see XGCValues is a struct, not a integral type, so you would need to initialize it with something like:
XGCValues values;
values.cap_style = CapButt;
values.join_style = JoinBevel;

Related

Wrong output with scanf function

so this is supposedly not a difficult question, but I've been getting this problem a few times when running my code in VS code. I am trying to separate the alphabets and numbers from the string, and I have used the method as follows (in my code) according to what is taught in the book. However, despite having the program running, the output is wrong.
Here is my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[] = "";
char hunit[] = "";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
The thing is,if I type in 20lb for weight, and 30mt for height, what happens is that it gives the output: 20,t,30,mt; which generates this weird ‘t’ instead of lb, and I have no idea why this is the case.
Similarly, when I type 30kg for weight, and 20cm for height. It generates this weird output:30,m,0, cm. The kg becomes a 'm' and the 20 is now a '0'!? Why is that the case? The expected output would be 30,kg,20,cm
I tried simply replacing the strings, but that doesn't solve the problem fundamentally. For instance, (considering when my user puts logical inputs like lb or kg for weight), I tried this substitution and it appears to work, but doesn't fix the issue of making 20 -> 0
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height= 0;
char wunit[] = "";
char hunit[] = "";
char wunit2[] = "lb";
char dummy[] = "t";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
if (strcmp(wunit,dummy)==0){
printf("%d,%s,%d,%s\n", weight, wunit2, height, hunit);
}
//printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
I've also tried running it in codecollab, and it shows this error of "stack smashing detected" after I run it a few times, which got me more confused, what has it to do with this?
Thanks in advance.
wunit is an array of size 1 (it is initialized to "", which in chars looks like {'\0'}). What happens when you try to put lots of characters (say, "lb", which is {'l', 'b', '\0'}) into a memory location that is smaller than it should be?
scanf happily writes as many bytes as needed, smashing anything in its way ("stack-smashing", because wunit and all those local variables are stored on the stack). Try to give scanf more space, say using
char wunit[10] = "";
And never ever use "%s" directly. Limit the maximum of characters that you will allow scanf to place, for example using "%9s" to ensure that at most 9 characters + terminator (10 total) will be read.
This works for me:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[10] = "";
char hunit[10] = "";
printf("Enter the body weight: ");
scanf("%d%9s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%9s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
Note: scanf with %s is rightfully considered very dangerous. See https://stackoverflow.com/a/2430310/15472

How to fix "expected identifier or '(' in C compilation?

I am new to coding and I keep getting stuck in the first few lines of code and I cannot figure out why. This is what I have so far:
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
{
printf("Minute: ");
int n = get_int();
}
I am getting this message when I try to compile the code:
What did I do wrong?
You're trying to call the main function. You should only define it. It will be called when the program is executed (it is the "entry point").
To define it, remove the semicolon after
int main(void)
You can also remove that void keyword
Then move that line down, between
int n; and the { that comes after it
Additionally, you're declaring the n variable twice. After you fix the first error, the compiler will complain about this one. Remove one of the declarations then.
You should remove the semicolon after int main(void) and move the variable declaration for n within the braces. Here is the correct code below.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Minute: ");
int n = get_int();
}

Monitoring file changes using select() within a loop

I am trying to write a program that will constantly keep track of the changes in a file and do several actions accordingly. I am using inotify and select within a loop to track file modifications in a non-blocking manner. The basic structure of the file tracking portion of my program is as follows.
#include <cstdio>
#include <signal.h>
#include <limits.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>
int main( int argc, char **argv )
{
const char *filename = "input.txt";
int inotfd = inotify_init();
char buffer[1];
int watch_desc = inotify_add_watch(inotfd, filename, IN_MODIFY);
size_t bufsiz = sizeof(struct inotify_event) + 1;
struct inotify_event* event = ( struct inotify_event * ) &buffer[0];
fd_set rfds;
FD_ZERO (&rfds);
struct timeval timeout;
while(1)
{
/*select() intitialisation.*/
FD_SET(inotfd,&rfds); //keyboard to be listened
timeout.tv_sec = 10;
timeout.tv_usec = 0;
int res=select(FD_SETSIZE,&rfds,NULL,NULL,&timeout);
FD_ZERO(&rfds);
printf("File Changed\n");
}
}
I checked the select manual page and reset the fd_set descriptor each time select() returns. However, whenever I modify the file (input.txt), this code just loops infinitely. I not very experienced using inotify and select, so, I am sure if the problem is with the way I use inotify or select. I would appreciate any hints and recommentations.
you have to read the contents of the buffer after the select returns. if the select() finds data in the buffer, it returns. so, perform read() on that file descriptor (inotfd). read call reads the data and returns amount of bytes it read. now, the buffer is empty and in the next iteration, the select() call waits until any data is available in the buffer.
while(1)
{
// ...
char pBuf[1024];
res=select(FD_SETSIZE,&rfds,NULL,NULL,&timeout);
read(inotfd,&pBuf, BUF_SIZE);
// ...
}

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

Bus error opening and mmap'ing a file

I want to create a file and map it into memory. I think that my code will work but when I run it I'm getting a "bus error". I searched google but I'm not sure how to fix the problem. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
int main(void)
{
int file_fd,page_size;
char buffer[10]="perfect";
char *map;
file_fd=open("/tmp/test.txt",O_RDWR | O_CREAT | O_TRUNC ,(mode_t)0600);
if(file_fd == -1)
{
perror("open");
return 2;
}
page_size = getpagesize();
map = mmap(0,page_size,PROT_READ | PROT_WRITE,MAP_SHARED,file_fd,page_size);
if(map == MAP_FAILED)
{
perror("mmap");
return 3;
}
strcpy(map, buffer);
munmap(map, page_size);
close(file_fd);
return 0;
}
You are creating a new zero sized file, you can't extend the file size with mmap. You'll get a bus error when you try to write outside the content of the file.
Use e.g. fallocate() on the file descriptor to allocate room in the file.
Note that you're also passing the page_size as the offset to mmap, which doesn't seem to make much sense in your example, you'll have to first extend the file to pagesize + strlen(buffer) + 1 if you want to write buf at that location. More likely you want to start at the beginning of the file, so pass 0 as the last argument to mmap.

Resources