Assigned a value that is never used (simple program) - unassigned-variable

#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..

Related

How to add a system call to find the number of processes in xv6

I have added this function in proc.c file
int getNumProc(void)
{
struct proc *p;
int count = 0;
acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
{
if(p->state != UNUSED)
count++;
}
release(&ptable.lock);
return count;
}
I have made all the necessary modifications in the following files:
defs.h
sysproc.c
syscall.h
usys.S
syscall.c
user.h
I also created a user program called totproc.c to call this system call and added this user program in Makefile at relevant places. When I type totproc command in XV6 shell the command does print that there a 3 processes. But alongside the result, it also prints the following error :
pid 4 totproc: trap 14 err 5 on cpu 1 eip 0xffffffff addr 0xffffffff--kill proc
What could be wrong here? If you were to write a system call to find the number of processes, how would you write it?
You seems to be in the right way but looks like you are missing something.
The error you are getting is being produced when an unexpected interrupt is received (in trap.c). Specifically, trap number 14 is T_PGFLT (according to trap.h).
This means the MMU answered with a page fault interrupt when some address was being tried to access, in other words, you are probably having a memory overwrite or access violation somewhere.
Consider sharing you user space application code.
Well , I figured out the problem. Turned out , the problem was not in my system call but in the user program totproc.c that made the system call. My initial totproc.c looked like this :
#include "types.h"
#include "stat.h"
#include "user.h"
int main()
{
printf(1 , "No. of Process: %d" , getNumProc());
return 0;
}
The properly working totproc.c is like below :
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
int main()
{
printf(1 , "No. of Process: %d" , getNumProc());
exit();
}

Object returned without return keyword

I was trying something out, and I made this code: http://cpp.sh/4x435
(Shown here below too)
#include <iostream>
using namespace std;
class Thing{
public:
int height;
Thing(int h): height(h) {};
Thing(): height(10) {};
~Thing(){};
int display(){ return this->height; }
};
Thing* get(){
Thing* x = new Thing(33);
}
int main(){
Thing* a = new Thing();
std::cout<<"1: "<<a->display()<<std::endl;
a = get();
std::cout<<"2: "<<a->display()<<std::endl;
return 0;
}
I forgot to add a return in the "get"-function before I compiled and ran it, surprisingly it played out correctly anyway(i.e. "1: 10, 2: 33" was the output).
Now I saw that in the shell online it only displays "1: 10", whereas when I try having it return an int or string by value, like so:
int get(){ int a = 30; }
int main(){
int b = get();
std::cout<<"1: "<<b<<std::endl;
return 0;
}
it doesn't function correctly(outputs "1: 1"), this is expected.
What's happening there? Shouldn't "Things* get()" malfunction, cause an error or at least make it spit out some gibberish onto the screen or something?
I'm using Code::Blocks 16.01, C++11, GNU GCC Compiler, no extra flags set(so only any already set by Code::Blocks)
EDIT:
It's not exactly the same as the suggested duplicate, because the example of int get() { int a = 30; } returns 1, had it returned 30, then it would have been the same problem.
However, I tried this:
int* get(){
int* x = new int(4);
}
int main(){
int* a;
a = get();
std::cout<<"2: "<<*a<<std::endl;
return 0;
}
And here I get the same problem that I found when trying the first version of the code where get() was of the return type Thing*. So it seems like it has to do with pointers.
Use the -Wall option when compiling to have an appropriate warning, like:
gcc program.c -o program -Wall
Otherwise this question and its accepted answer explains why it happens, even providing an example similar to your observation:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
If you are curious, check the assembler output! You can generate it by:
gcc program.c -S
Since the behavior is undefined, anything may happen, what you are experiencing is most likely caused by that within the function, the compiler left the right value in the register normally used for returning.

Necessity of declaration of function in c and cpp

From bruce eckel --" although u should always declare functions by including header file , functions declarations aren't' essential in c . Its possible in c but not cpp to call a function u havent declared. This is a dangerous practise because the c compiler may assume that a function that u call with an integer argument has an argument list containing integer even if it may actually contain float . This can produce bugs" my question is that even if a function is not declared , during its definition we have to mention the data type of arguments [ VOID FUNC( INT A)] , so how can a compiler assumes a float to be an integer??
The compiler makes assumption on supplied parameters if a function is not declared or defined prior to the point the assumption should be made. Try the following code and check the result (checked with gcc):
#include <stdio.h>
int main (int argc, char * argv[])
{
x(1);
x(1.);
x(1);
return 0;
}
void x(double y)
{
printf ("%f\n", y);
}

Bad output taylor series sinx

i'm trying to write a program that gets from the user a value x and prints sinx using taylor series. but my output is bad. the output i get is not even a number, its -1.#IND00 regardless of what i input.
here's my code
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
double x,sum,last;
sum=(double)0;
scanf("%f",&x);
last=x;
sum=last;
for(i=1;i<10;i++)
{
last*=(double)(-x*x)/((2*i)*(2*i+1));
sum+=last;
}
printf("%f",sum);
getch();
}
I can see one problem:
scanf("%f",&x);
x is a double, so you need the l, i.e. "%lf".
[true but irrelevant point about how this isn't the right formula for sinh, even though sinh is nowhere mentioned in the question, redacted..]

How to deal with "%1" in the argument of QString::arg()?

Everybody loves
QString("Put something here %1 and here %2")
.arg(replacement1)
.arg(replacement2);
but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second QString::arg() will replace only the re-introduced %1 or both %2 occurrences. Anyway, you won't get the literal "%1" that you probably intended.
Is there any standard trick to overcome this?
If you need an example to play with, take this
#include <QCoreApplication>
#include <QDebug>
int main()
{
qDebug() << QString("%1-%2").arg("%1").arg("foo");
return 0;
}
This will output
"foo-%2"
instead of
"%1-foo"
as might be expected (not).
qDebug() << QString("%1-%2").arg("%2").arg("foo");
gives
"foo-foo"
and
qDebug() << QString("%1-%2").arg("%3").arg("foo");
gives
"%3-foo"
See the Qt docs about QString::arg():
QString str;
str = "%1 %2";
str.arg("%1f", "Hello"); // returns "%1f Hello"
Note that the arg() overload for multiple arguments only takes QString. In case not all the arguments are QStrings, you could change the order of the placeholders in the format string:
QString("1%1 2%2 3%3 4%4").arg(int1).arg(string2).arg(string3).arg(int4);
becomes
QString("1%1 2%3 3%4 4%2").arg(int1).arg(int4).arg(string2, string3);
That way, everything that is not a string is replaced first, and then all the strings are replaced at the same time.
You should try using
QString("%1-%2").arg("%2","foo");

Resources