I have searched and haven't really found and understood this error. It's weird that I only get the error for c, d, e and not for a and b or them all.
Program is about Dooubly Link List.
This happens when I compile with:
gcc -Wall -g -c program.c
Error part:
void try_mymem(int argc, char** argv) {
strategies strat;
void *a, *b, *c, *d, *e;
if (argc > 1)
strat = strategyFromString(argv[1]);
else
strat = First;
/* A simple example.
Each algorithm should produce a different layout. */
initmem(strat, 500);
a = mymalloc(100);
b = mymalloc(100);
c = mymalloc(100);
myfree(b);
d = mymalloc(50);
myfree(a);
e = mymalloc(25);
print_memory();
print_memory_status();
}
What am I doing wrong?
They're not used, just like the compiler says. You assign but never read. a and b are used as arguments, the others are not.
Related
All the functions used in the main file are correctly defined. However when I try to enter the debugging mode, for some reason IDE isn't stepping into said functions when asked to. Instead it acts like a step over and ignores the lines until the very last line, where out of the blue all variables appear in the window ; prior to that, no variable is present whatsoever.
Here's the main file :
#include "pointers.h"
int main(void){
whatisapointer();
whatisthesizeofapointer();
whatareNpointersplacedsuccessively(6);
return 0;
}
the pointers.c file used to define the functions in main :
#include "pointers.h"
void whatisapointer(){
int *pointer;
pointer = allocateOneInteger();
*pointer = 42;
}
void whatisthesizeofapointer(){
int a = sizeof(char);
int b = sizeof(int);
int c = sizeof(char*);
int d = sizeof(int*);
}
void whatareNpointersplacedsuccessively(int N){
int i, *Npointer, *current;
char *Npointerchar, *currentchar;
Npointer = allocateNInteger(N);
current = Npointer;
current = Npointer+1;
current = Npointer+2;
for(i=0;i<N;i++) *(Npointer+i) = i;
N=2*N;
Npointerchar = allocateNChar(N);
currentchar = Npointerchar;
currentchar = Npointerchar+1;
currentchar = Npointerchar+2;
for(i=0;i<N;i++) Npointerchar[i] = 65+i;
Npointer[N-3] = 0x68676665;
current = Npointer+N-3;
}
int* allocateOneInteger(){
return (int*)malloc(1*sizeof(int));
}
int* allocateNInteger(int N){
return (int*)malloc(N*sizeof(int));
}
char* allocateNChar(int N){
return (char*)malloc(N*sizeof(char));
}
void echange(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void echangep(int *pa,int *pb)
{
int temp;
temp=*pa;
*pa=*pb;
*pb=temp;
}
/* Fonction allocateNFloat
*/
void allocateNFloat(int N){
}
And the makefile :
CC := gcc
FLAGS := -g -Wall -Werror
all : prog
prog : pointers.o mainpointers.o
$(CC) pointers.o mainpointers.o -o prog
fonctions.o : pointers.c pointers.h
$(CC) -c pointers.c $(FLAGS) -o pointers.o
mainpointers.o : mainpointers.c pointers.h
$(CC) -c mainpointers.c $(FLAGS) -o mainpointers.o
clean :
rm -f *.o
rm -f prog
I've done some research, none of which has helped me solve this issue.
You're very likely finding that the compiler is optimizing away your code, because the functions have no side-effects. (If you're not familiar with the concept of side effects, the gist is 'changes to program state that might influence other parts of the program'---compilers aggressively prune away side-effect-free code because, by definition, doing so doesn't change the behavior of the program.)
Even allocating memory isn't a side affect, because correct code should never be able to tell whether another part of the program has allocated memory.
The easiest side-effect is to print out the result of any intermediate computation you want to inspect via the debugger; that will force the optimizer to leave the code in place.
E.g.,
int foo() //no side effects, will be optimized to an empty function or pruned.
{
char *test = malloc(4096);
strcpy(test, "My test string");
return 0;
}
int foo() //dumb side effects
{
char *test = malloc(4096);
strcpy(test, "My test string");
printf("%02x", ((int)test & 0xff) ^ (test[0]) );//make output depend on the operations you wish to observe
return 0;
}
An alternate means to verify that the compiler is pruning away your code would be to disassemble the resulting object file...you should see that the functions consist of a single 'return' instruction...which is what the debugger is showing by highlighting the closing curly brace.
I am writing a function in C with the below specifications:
float find_root(float a, float b, float c, float p, float q);
find_root takes the coefficients a,b,c of a quadratic equation and an interval (p, q). It will return the root of this equation in the given interval.
For example: find_root(1, -8, 15, 2, 4) should produce a root "close to" 3.0
I have written the below code, and I don't understand why it doesn't work:
#include<stdio.h>
#include<math.h>
main()
{
printf("Hello World");
}
float find_root(float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if (root1<=q || root1>=p)
{
return root1;
}
return root2;
}
Please let me know what the error is.
Your program doesn't work, because, you never called find_root() from your main().
find_root() is not suppossed to run all-by-itself. Your program statrs execution from main(). You need to call your sub-function from main() in order to make them execute.
Change your main to have a call to find_root(), something like below.
int main() //put proper signature
{
float anser = 0;
answer = find_root(1, -8, 15, 2, 4); //taken from the question
printf("The anser is %f\n", answer); //end with a \n, stdout is line buffered
return 0; //return some value, good practice
}
Then, compile the program like
gcc -o output yourfilename.c -lm
Apart from this, for the logical issue(s) in find_root() function, please follow the way suggested by Mr. #paxdiablo.
For that data, your two roots are 5 and 3. With p == 2 and q == 4:
if (root1<=q || root1>=p)
becomes:
if (5<=4 || 5>=2)
which is true, so you'll get 5.
The if condition you want is:
if ((p <= root1) && (root1 <= q))
as shown in the following program, that produces the correct 3:
#include<stdio.h>
#include<math.h>
float find_root (float a, float b, float c, float p, float q) {
float d,root1,root2;
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
if ((p <= root1) && (root1 <= q))
return root1;
return root2;
}
int main (void) {
printf ("%f\n", find_root(1, -8, 15, 2, 4));
return 0;
}
That's the logic errors with your calculations of the roots.
Just keep in mind there are other issues with your code.
You need to ensure you actually call the function itself, your main as it stands does not.
It also wont produce a value within the p/q bounds, instead it will give you the first root if it's within those bounds, otherwise it'll give you the second root regardless of its value.
You may want to catch the situation where d is negative, since you don't want to take the square root of it:
a = 1000, b = 0, c = 1000: d <- -4,000,000
And, lastly, if your compiler is complaining about not being able to link sqrt (as per one of your comments), you'll probably find you can fix that by specifying the math library, something like:
gcc -o myprog myprog.c -lm
Your program starts at main by definition.
Your main function is not calling find_root but it should.
You need to compile with all warnings & debug info (gcc -Wall -Wextra -g) then use a debugger (gdb) to run your code step by step to understand the behavior of your program, so compile with
gcc -Wall -Wextra -g yoursource.c -lm -o yourbinary
or with
clang -Wall -Wextra -g yoursource.c -lm -o yourbinary
then learn how to use gdb (e.g. run gdb ./yourbinary ... and later ./yourbinary without a debugger)
Then you'll think and improve the source code and recompile it, and debug it again. And repeat that process till you are happy with your program.
BTW, you'll better end your printf format strings with \n or learn about fflush(3)
Don't forget to read the documentation of every function (like printf(3) ...) that you are calling.
You might want to give some arguments (thru your main(int argc, char**argv) ...) to your program. You could use atof(3) to convert them to a double
Read also about undefined behavior, which you should always avoid.
BTW, you can use any standard C compiler (and editor like emacs or gedit) for your homework, e.g. use gcc or clang on your Linux laptop (then use gdb ...). You don't need a specific seashell
Change this condition
if (root1<=q || root1>=p)
to
if (root1<=q && root1>=p)
otherwise if anyone of the conditions is satisfied, root1 will be returned and root2 will almost never be returned. Hope this fixes your problem.
again I need help. I try to call SWI-Prolog via C based on a dll on windows.
In my C progam I want to use the function "consult_cooco". The C code looks like
#include "consult_cooco.h"
#include <SWI-Prolog.h>
int consult_cooco( char** strInput )
{
char *program = "consult_cooco";
char *plav[2];
int rval;
char* xmlstring;
term_t av;
predicate_t p;
/* make the argument vector for Prolog */
plav[0] = program;
plav[1] = NULL;
putenv("SWI_HOME_DIR=C:\\Program Files (x86)\\swipl");
if ( !PL_initialise(1, plav ) )
PL_halt(1);
av = PL_new_term_refs(2);
p = PL_predicate("start_dialog", 2, "user");
printf("consult_cooco:: input >> %s\n",*strInput);
// Prolog call: start_dialog('ohne Zitrone, mit Joghurt, Butter',XML).
av = PL_new_term_refs(2);
PL_put_atom_chars(av, *strInput);
if ( PL_call_predicate(NULL, PL_Q_NORMAL, p, av) )
{
rval = PL_get_atom_chars(av+1, &xmlstring);
printf(" >> xmlstring of %s is \n\n%s (returned %i)\n",*strInput,xmlstring,rval);
}
else
{
printf(" >> no answer found\n");
}
PL_halt(1);
return 0;
}
I created the dll by
swipl-ld -shared -dll -o consult_cooco -goal true consult_cooco.c dialog.pl -DLIKES_EXPORTS -v
I linked C sources by
gcc CooCoServer.o -static-libgcc -lws2_32 -L. -lconsult_cooco -I. -o startcoocoserver
Begin of my Prolog file dialog.pl looks like
start_dialog( InputString, XMLResult ) :-
create_individual_filename('dialog_','.debug',Filename),
tell(Filename),
process_input( InputString, WordList ),
do(WordList, XMLResult ),
told.
Without
putenv("SWI_HOME_DIR=C:\\Program Files (x86)\\swipl");
I got the error
[FATAL ERROR: Could not find system resources]
as described in SWI Prolog manual.
But still Prolog predicates can not be found, the error description is
ERROR: '$c_call_prolog'/0: Undefined procedure: start_dialog/2
start_dialog is defined in dialog.pl, see above.
My assumption is that plav[0] = "consult_cooco"; is wrong and no link to the Prolog file is found.
When using an exe file starting from a C main file by changing
int consult_cooco( char** strInput )
{
char *program = "consult_cooco";
to
int main( int argc, char** argv )
{
char *program = argv[0];
it works. It works also with
int main( int argc, char** argv )
{
char *program = "call_cooco";
What do I wrong when using the dll? What must be put into char *program?
Would appreciate your help very much.
Sorry for that, the answer is already given here:
I have to set
plav[0] = (char*)"startcoocoserver.exe";
plav[1] = (char*)"-x";
plav[2] = (char*)"dialog.exe";
and to use then
if(!PL_initialise(3, plav))
An additional building step is necessary
swipl --goal=true --stand_alone=true -o dialog -c dialog.pl
before doing the other steps. Then it works :)
PS: I did not found how to delete a question - admin, please do...
In GDB (gnu v 7.1-ubuntu) I am getting really weird results when I try to use strcmp to determine if two strings are equal. p strcmp("hello","hello") is giving me the result -145947168.
Everything I try with strcmp or strncmp is returning -145947168 in gdb. What am I doing wrong?
EDIT (thanks to Carl for the pointer to a related answer in the comments):
See the answer to this question: How to evaluate functions in GDB?
Apparently sometimes the compiler optimizes out functions called from external libraries, and defining a function in code that calls the function of the external library you want access to in GDB will make it available.
I added this to my code:
#ifdef DEBUG
int mystrcmp(char *a, char *b){
return strcmp(a,b);
}
int mystrncmp(char *a, char *b, int n){
return strncmp(a,b,n);
}
#endif
and then re-made with -DDEBUG -g to enable the compilation of those helper functions for my gdb debugging.
(gdb) p mystrcmp("hello","hello")
$1 = 0
(gdb) p strcmp("hello","hello")
$2 = -145947168
Here what's i am trying to do:
assume you have two fonction
void f1(int *v)
{
*v = 55;
}
void f2(int *v)
{
*v = 44;
}
char *template;
template = allocExecutablePages(...);
char *allocExecutablePages (int pages)
{
template = (char *) valloc (getpagesize () * pages);
if (mprotect (template, getpagesize (),
PROT_READ|PROT_EXEC|PROT_WRITE) == -1) {
perror (“mprotect”);
}
}
I would like to do a comparison between f1 and f2 (so tell what is identical and what is not) (so get the assembly lines of those function and make a line by line comparison)
And then put those line in my template.
Is there a way in C to do that?
THanks
Update
Thank's for all you answers guys but maybe i haven't explained my need correctly.
basically I'm trying to write a little obfuscation method.
The idea consists in letting two or more functions share the same location in memory. A region of memory (which we will call a template) is set up containing some of the
machine code bytes from the functions, more specifically, the ones they all
have in common. Before a particular function is executed, an edit script is used
to patch the template with the necessary machine code bytes to create a
complete version of that function. When another function assigned to the same
template is about to be executed, the process repeats, this time with a
different edit script. To illustrate this, suppose you want to obfuscate a
program that contains two functions f1 and f2. The first one (f1) has the
following machine code bytes
Address Machine code
0 10
1 5
2 6
3 20
and the second one (f2) has
Address Machine code
0 10
1 9
2 3
3 20
At obfuscation time, one will replace f1 and f2 by the template
Address Machine code
0 10
1 ?
2 ?
3 20
and by the two edit scripts e1 = {1 becomes 5, 2 becomes 6} and e2 = {1
becomes 9, 2 becomes 3}.
#include <stdlib.h>
#include <string.h>
typedef unsigned int uint32;
typedef char * addr_t;
typedef struct {
uint32 offset;
char value;
} EDIT;
EDIT script1[200], script2[200];
char *template;
int template_len, script_len = 0;
typedef void(*FUN)(int *);
int val, state = 0;
void f1_stub ()
{
if (state != 1) {
patch (script1, script_len, template);
state = 1;
}
((FUN)template)(&val);
}
void f2_stub () {
if (state != 2) {
patch (script2, script_len, template);
state = 2;
}
((FUN)template)(&val);
}
int new_main (int argc, char **argv)
{
f1_stub ();
f2_stub ();
return 0;
}
void f1 (int *v) { *v = 99; }
void f2 (int *v) { *v = 42; }
int main (int argc, char **argv)
{
int f1SIZE, f2SIZE;
/* makeCodeWritable (...); */
/* template = allocExecutablePages(...); */
/* Computed at obfuscation time */
diff ((addr_t)f1, f1SIZE,
(addr_t)f2, f2SIZE,
script1, script2,
&script_len,
template,
&template_len);
/* We hide the proper code */
memset (f1, 0, f1SIZE);
memset (f2, 0, f2SIZE);
return new_main (argc, argv);
}
So i need now to write the diff function. that will take the addresses of my two function and that will generate a template with the associated script.
So that is why i would like to compare bytes by bytes my two function
Sorry for my first post who was not very understandable!
Thank you
Do you want to do this at runtime or during authorship?
You can probably instruct your C compiler to produce assembly language output, for example gcc has the -S option which will produce output in file.s Your compiler suite may also have a program like objdump which can decompile an object file or entire executable. However, you generally want to leave optimizations up to a modern compiler rather than do it yourself.
At runtime the & operator can take the address of a function and you can read through it, though you have to be prepared for the possibility of encountering a branch instruction before anything interesting, so you actually have to programatically "understand" at least a subset of the instruction set. What you will run into when reading function pointers will of course vary all over the place by machine, ABI, compiler, optimization flags, etc.
Put the functions into t1.c and t2.c use gcc -S to generate assembly output:
gcc -S t1.c
gcc -S t2.c
Now compare t1.s and t2.s.
If you are using Visual Studio, go to
Project Properties -> Configuration -> C/C++ -> Output Files -> Assembler output
or use compiler switches /FA, /FAc, /FAs, /FAcs. Lower-case c means output machine code, s-source code side-by-side with assembly code. And don't forget to disable compiler optimizations.
Having read through some of the answers and the comments there, I'm not sure I fully understand your question, but maybe you're looking for a gcc invocation like the following:
gcc -S -xc - -o -
This tells gcc to input C code from stdin and output assembly to stdout.
If you use a vi-like editor, you can highlight the function body in visual mode and then run the command:
:'<,'>!gcc -S -xc - -o - 2> /dev/null
...and this will replace the function body with assembly (the "stderr > /dev/null" business is to skip errors about #include's).
You could otherwise use this invocation of gcc as part of a pipeline in a script.