Application freezes when call "scanf" function - objective-c

There is a simple hackerrank task, where I stuck on getting input phase.
Input code is simple (I'm using Objective-C)
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSUInteger t;
scanf("%ld",&t);
int n,m,s;
int i = 0;
while (i < t) {
scanf("%d %d %d",&n,&m,&s);
printf("%d n=%d m=%d s=%d \n",i,n,m,s);
i++;
}
}
return 0;
}
But on a big test case with t=100, this small program freezes on step 33.
It looks like problem is in scanf function call,but cannot find it.
Here is testcase

Related

Infinite printing on read command of character device (through cat command) [duplicate]

I am working on simple character device driver. I have implemented read and write functions in the module, the problem is when I try to read the device file using cat /dev/devicefile it is going into infinite loop i.e. reading the same data repeatedly. Can someone suggest me any solution to this problem? Below is my driver code.
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/string.h>
#include<asm/uaccess.h>
#include<linux/init.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("character device driver");
MODULE_AUTHOR("Srinivas");
static char msg[100]={0};
static int t;
static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *,size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t,loff_t *);
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.write = dev_write,
.release = dev_rls,
};
static int himodule( void )
{
t = 0;
t = register_chrdev(0, "chardevdriver", &fops);
if (t < 0)
printk(KERN_ALERT"device registration failed\n");
else
printk(KERN_ALERT"device registered successfully\n");
printk(KERN_ALERT"major number is %d", t);
return 0;
}
static void byemodule(void)
{
unregister_chrdev(t, "chardevdriver");
printk(KERN_ALERT"successfully unregistered\n");
}
static int dev_open(struct inode *inod, struct file *fil)
{
printk(KERN_ALERT"inside the dev open");
return 0;
}
static ssize_t dev_read(struct file *filp, char *buff, size_t len, loff_t *off)
{
short count = 0;
while (msg[count] != 0) {
put_user(msg[count], buff++);
count++;
}
return count;
}
static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off)
{
short count = 0;
printk(KERN_ALERT"inside write\n");
memset(msg,0,100);
printk(KERN_ALERT" size of len is %zd",len);
while (len > 0) {
msg[count] = buff[count];
len--;
count++;
}
return count;
}
static int dev_rls(struct inode *inod,struct file *fil)
{
printk(KERN_ALERT"device closed\n");
return 0;
}
module_init(himodule);
module_exit(byemodule);
.read function should also correctly process its len and off arguments. The simplest way to implement reading from memory-buffered file is to use simple_read_from_buffer helper:
static ssize_t dev_read(struct file *filp, char *buff, size_t len, loff_t *off)
{
return simple_read_from_buffer(buff, len, off, msg, 100);
}
You can inspect code of that helper (defined in fs/libfs.c) for educational purposes.
BTW, for your .write method you could use simple_write_to_buffer helper.
You are not respecting the buffer size passed into the dev_read function, so you may be invoking undefined behaviour in cat. Try this:
static ssize_t dev_read( struct file *filp, char *buff, size_t len, loff_t *off )
{
size_t count = 0;
printk( KERN_ALERT"inside read %d\n", *off );
while( msg[count] != 0 && count < len )
{
put_user( msg[count], buff++ );
count++;
}
return count;
}
This problem can be solved by correctly setting *off (fourth parameter of my_read()).
You need to return count for the first time and zero from second time onwards.
if(*off == 0) {
while (msg[count] != 0) {
put_user(msg[count], buff++);
count++;
(*off)++;
}
return count;
}
else
return 0;

How to send signal to a child process created via fork and execl?

Assume I have a program foo that simply prints forever but is stopped before then,
int main(int argc, char * argv[])
{
kill(getpid(), SIGSTOP);
while(1) {
printf("foo\n");
}
return 0;
}
And then I have a program bar that forks and runs foo:
int main(int argc, char * argv[])
{
pid_t pid = fork();
if (pid == 0)
{
execl("foo", "foo", NULL);
}
return 0;
}
Now, I want bar to be able to send SIGCONT to foo so it can go on its printing duties, and then later send SIGSTOP again, and even later send SIGCONT again, and so on.
I couldn't figure out how to do that. Any help?
EDIT: I figured out my problem. Apparently, the foo program isn't ready to accept signals immediately when it runs. When I did
sleep(5);
int rc = kill(pid, SIGCONT);
it worked.
pid_t pid = fork();
if (pid == 0)
{
execl("foo", "foo", NULL);
}
int rc = kill(pid, 18);
return 0;
Suggest: don't forget to handle errors codes from system calls! Work without handling of error codes is worse style! This two actions you can do in one program. In this case it's will be more effective:
#include <errno.h>
int main(int argc, char * argv[])
{
int rc = 0;
pid_t pid = fork();
if (pid == 0) /* child */
{
kill(getpid(), SIGSTOP);
if (rc == -1) {
perror("Something wrong while kill() in child");
}
while (1) {
printf("foo\n");
}
} else if (pid > 0) { /* father */
rc = kill(pid, SIGCONT);
if (rc == -1) {
perror("Something wrong while kill() in parent");
}
} else if (pid < 0) {
perror("Something wrong while fork()");
return -1;
}
return 0;
}

IJW C# to MC++ to C++ transfer array of strings to char**

Does anyone know how to pin the following string array parameter:
function signature int TestMethod(int argc, array<String^>^ data)
{
pin_ptr<char> p1 = &data[0];
char** p2 = &p1[0];
// char** argv = (char**)calloc(argc+1, sizeof(char*));
}
I want to pin an array<String^>^ object and convert it to the following:
char** argv = (char**)calloc(argc+1, sizeof(char*));
so I can pass it to a native c++ function
any ideas?
What about the following?
UPDATE: just curious? what about the following?
char** argv = (char**)calloc(argc+1, sizeof(char*));
for (int i = 0; i < data->Length; i++)
{
argv[i] = (char*)Marshal::StringToHGlobalAnsi(data[i]).ToPointer();
}
// Use argv as needed here...
// Cleanup
for (int i = 0; i < data->Length; i++)
{
Marshal::FreeHGlobal((IntPtr)argv[i]);
}
You can't directly pin a managed String^ into a char*. .NET Strings are actually 2 bytes per character, so you need to marshal the data manually. This will likely require building up the character arrays, copying data into them, then cleaning up after you're done.
In this case, you likely need to copy the data into your char**, use it, then clean up after yourself. This can be done via something like:
// Requires #include <msclr\marshal.h>
marshal_context context;
char** argv = new char*[data->Length];
for (int i=0;i<Length;++i)
{
const char* tmp = context.marshal_as<const char*>(clrString);
int length = strlen(tmp);
argv[i] = new char[length+1]();
strncpy(argv[i],tmp,length);
}
// Use argv as needed here...
// Cleanup
for (int i=0;i<Length;++i)
delete[] argv[i];
delete[] argv;

"ld returned 1 exit status" in simple program

can anyone tell me what is wrong with this code?
Im trying to pass the value of 'amount' and 'count' into int main where it displays the multiplication. I get the error "C:\Users\XXXX\Desktop\collect2.exe [Error] ld returned 1 exit status" with the int main() highlighted. Thank you.
#include <stdio.h>
void show(int, int);
int display()
{
int amount;
int count;
amount = 10;
count = 20;
return(0);
}
int main(int amount, int count)
{
show(amount, count);
int sum;
sum = amount * count;
printf("%d", sum);
return(0);
}
The error's cause is that your main function's signature is rejected by ld, which is expected because the standard signatures for main are int main(void) and int main(int argc, char** argv).
If you want to pass amount and count as parameters to main, that is as command-line parameters, you should use the second signature I gave for main, where argv is an array of the command line parameters and argc gives the number of elements in argv. Note that argv[0] is not a command line parameter but the name under which your program was called and that argv[argc] is a null pointer.
You should pass command line parameters to the main using argc and *argv[] instead of trying to define your own.
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
This prints the paramters passed to main() via the command line.
If you pass a value for amount a and a value for count via the command line, you can access them like this
int main(int argc, char *argv[]) {
std::cout << "amount" << argv[1] << std::endl;
std::cout << "count" << argv[2] << std::endl;
}
Used index 1 and 2 of argv because index 0 contains the program name
You specified no language so I used C++ for the above example, but the idea is the same for C:
int main(int argc, char *argv[]) {
printf("amount %s", argv[1]);
printf("count %s", argv[2]);
}
Note that the incoming values are char arrays, not integers

Expected expression error. C

I am trying to write a code that will take two intergers, and will list all numbers lower than both of them except for those that are a factor of either if the two numbers inputted. At some point in my code though (see below) i am getting an error saying expected expression. I am a beginner so if you could explain this too me as simply as possible.
int main(int argc, const char * argv[])
{
#autoreleasepool {
int firstInterger;
int secondInterger;
int i;
printf("Please enter the first interger: ");
scanf("%i", &firstInterger);
printf("Please enter the second interger:");
scanf("%i", &secondInterger);
for (i = 0; i < firstInterger && i < secondInterger; i++) {
if ((firstInterger % i !== 0) && (secondInterger % i !== 0)) { //ERROR HERE!
printf("%i", i);
}
}
}
return 0;
}
You have twice the same error in that line.
You should replace !== with != or ==