Codeblocks C Debugging - c

I've got a new challenge to return the factorial of a number. Got ideas on how to do this, but the challenger has given some starting code - which is shown below.
Now this isn't how I would have started it (with my extremely limited experience!) - BUT I wasn't sure how system would grab some text & place within an int array - hence I tried running it within codeblocks, debugging and looking at the watch table. However I can't see 'num'.
So I tried copying num to num1:
int num1[30] = {0};
memset(num1[0],num[0], sizeof(num));
that doesn't seem to affect anything...
So question really is - is there something wrong with my codeblocks config (it debugs other programs and I've tried both cygwin & MiniGW) or is there another reason for this behavious?
#include <stdio.h>
#include <string.h>
void FirstFactorial(int num[]) {
// code goes here
printf("%d", num);
}
int main(void) {
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}

Related

C - a certain function in my program wont work with either returning an integer nor using a pointer

This is my first time posting of this forum and I'm doing is just because of this problem. I've been working on a program for a while(just for fun) and to make things simple for my self I used loads of global variables, but now I've been trying to make the individual functions more independent and flexible. A certain function is giving a a lot of issues for some reason.
int which_move(int ac,int bc,int cc){
int illcheck;
int ill_done;
int ill_pos;
int true_move;
true_move=3;
ill_done=-1;
for(u=6;u>=0;u--){
ill_pos=ert-1;
illcheck=0;
for(y=0;y<ill_len[u];y++){
if(buff[ill_pos]==ill_move[u][y]){
++illcheck;
if(ill_pos==0)
ill_pos=100;
--ill_pos;
if(illcheck==ill_len[u]){
ill_done=u;
break;
}
}
else
break;
}
if(ill_done!=-1)
break;
}
if(ac==1||ill_done==1||ill_done==2||ill_done==6)
true_move=0;
if(bc>2)
true_move=1;
if(cc>2)
true_move=2;
if(ill_done==0||ill_done==3||ill_done==4)
true_move=4;
if(ill_done==5)
true_move=5;
return true_move;
}
and this is how i call the function:
int open_move;
open_move=which_move(acheck,bcheck,ccheck);
and open_move never match true_move.
I've tried to convert to something like this
int which_move(int *true_move,int ac,int bc,int cc)
and remove int true_move; and the return of return true_move; and implement the function like this:
int open_move;
which_move(open_move,acheck,bcheck,ccheck);
still i get it to work.
I've googled til chrome starts lagging because of too many tabs open and tried every trick I can find, but I'm not getting any wiser. Please help me with what I'm doing wrong.
Thanks from a hobbyist.

Use a dynamic library dll in C program

I want to use a dll-file in my C-Code, but are very confused about the syntax.
My Story: I made a simple function in Matlab ( f(x1,x2)=x1*x2 ), with the "Matlab Coder" I translated it to C-Code and generated an exe, I could run it from the terminal with arguments.Now I generated a dll instead of an exe and want to use the dll.
Since now I could not make Code explanations, I googled, make work for me. I look up Syntax in http://en.cppreference.com/w/ but for my surprise there wasn't even an entry for e.g. GetProcAddress or LoadLirbary.
Here is the C-Code in which I would like to use the dll:
#include <stdio.h>
#include <stdlib.h>
/*
* In my dream I would load the dll function here
* with something like Load(mytimes4.dll)
*/
int main(int argc, char *argv[]) {
double x1,x2,myresult;
//Load Arguments from Terminal
sscanf(argv[1], "%lf", &x1);
sscanf(argv[2], "%lf", &x2);
// Use and print the function from mytimes4.dll
myresult = mytimes4(x1,x2);
printf("%3.2f\n",myresult);
return 0;
}
After generating the dll, Matlab gave me the following folder:
"dll-folder" produced by Matlab
Can someone give me a most simple but complete Code that would work with my example? What files are needed (maybe .def or .exp)? Also for Explanations of the lines involved using the dll I would be gratefull. Or if not, you maybe have some background knowledge that makes the complex syntax reasonable.Thanks in advance!
System information: Windows 7 Pro 64, Matlab 64 2016b, gcc cygwin 64, eclipse ide.
With the link of thurizas I could solve my problem.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx
I copied the code from the side. Below you can see the code with additional comments of mine and with ,in my opinion, more clearly naming. Thus it is probably easier to use for beginners as I am.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/*Declaration of the function,contained in dll, as pointer with the arbitrary pointer name
"*MYFUNCTIONPOINTER" (not sure if it has to be in big letters).
In my case the function means simply f(x1,x2) = x1*x2 and is thus as double declared*/
typedef double (*MYFUNCTIONPOINTER)(double, double);
int main() {
HINSTANCE hinstLib;
//"myfunction" is the arbitrary name the function will be called later
MYFUNCTIONPOINTER myfunction;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
//Tell the dll file
hinstLib = LoadLibrary(TEXT("mypersonal.dll"));
if (hinstLib != NULL)
{
/* At this line "myfunction" gets its definition from "MYFUNCTIONPOINTER"
and can be used as any other function.The relevant function in the dll has
to be told here.*/
myfunction = (MYFUNCTIONPOINTER) GetProcAddress(hinstLib, "mydllfunction");
// If the function address is valid, call the function.
if (NULL != myfunction)
{
fRunTimeLinkSuccess = TRUE;
// The function can be used.
double myoutput;
myoutput = myfunction(5,7);
printf("%f\n",myoutput);
getchar();
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
}

Creating array of strings works in source code doesn't work in executable

I've got some code which generates an array of strings of different file names and then
passes them into a function to write some data to them. It adds a incrementing number to the starting filename which is supplied from an input argument.
The problem is that it works fine running from source in Visual Studio 2012 but when I compile it and run it as an .exe the program crashes.
The .exe doesn't appear to be passing the array of strings properly which is causing an error when it attempts to use the string
for opening a file etc.
Here is the isolated bit of code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <Windows.h>
void processing_function(int num_output, char **outnames)
{
/* in Visual Studio this works fine and prints all
the names correctly. Running from .exe will crash */
for(int idx = 0; idx <num_output;idx++)
{
printf("outnames[%d] is %s\n",idx,outnames[idx]);
}
}
int main(int argc, char *argv[])
{
/*nframes comes from another function, outname comes from input arguement */
int num_output = ceil(((double)*nframes / 1100));
int outname_len = strlen(outname)+1;
char *out_right;
out_right = (char*) malloc(sizeof(char)*outname_len);
/*Split string to append numbers before file extension */
strcpy(out_right,outname);
strrev(out_right);
strtok(out_right,".");
strcat(out_right,".");
strrev(out_right);
int out_right_len = strlen(out_right);
strtok(outname,".");
strcat(outname,"-");
int out_origlen = strlen(outname);
int num_len = 1;
char **outnames;
char *num;
char *outname_tmp;
outnames = (char**) malloc(sizeof(char)*(num_output));
int out_len;
double dbl_idx;
int *numfs = (int*)malloc(sizeof(int)*num_output);
for(int idx = 1;idx <num_output+1;idx++)
{
/*convert output number to string and stitch complete name back together and place into array */
num_len = ceil(log10((double)idx+0.1));
num = (char*) malloc(sizeof(char)*(num_len+1));
outname_tmp = (char*) malloc(sizeof(char)*(out_origlen+num_len+out_right_len+1));
strcpy(outname_tmp,outname);
sprintf(num,"%d",idx);
strcat(outname_tmp,num);
free(num);
strcat(outname_tmp,out_right);
outnames[idx-1] = (char*) malloc(sizeof(char)*(out_origlen+num_len+out_right_len+1));
strcpy(outnames[idx-1],outname_tmp);
free(outname_tmp);
printf("%s\n",outnames[idx-1]);
}
free(out_right);
processing_function(num_ouput, outnames)
return(0);
}
EDIT: Changed num_input to num_output as they do have the same value.
Running from .exe will sometimes start printing some of the names and then crash, opening the
debugger gives an error within output.c, with an access reading violation. I tried putting this code at
the top of the processing_function but that gave further problems downstream (heap corruption), which makes me think that the
code is messing up the memory but I can't see whats wrong with it, nor why it would work in VS but not as a .exe.
I could try and dodge the issue by generating the next output name on the fly every time it requires one but I'd really rather know why this isn't working.
Any help would be greatly appreciated.
I am going to take a shot and say, you passed num_input to processing_function() with outnames, outnames was allocated with num_output for size, but num_input and num_output have different values at runtime. So that lets processing_function() access out of bounds.

'return accept int, return int' contains no buildables

I am new to writing code and this is my first question on this site so please forgive my ignorance if there is an obvious solution for this.
When I compile and run the code in Xcode I get a message I have never seen before. I think it has more to do with the return statement but can't seem to fix it by changing the return statement. This is an example from my text book so I would think the logic is sound. I have tried running this code on more than one computer(all mac OSX) with the same results.I have tried different IDEs including codeRunner and Xcode 4.6.3 and 3.4.? and the latest release.
Can anyone tell me what the following message means? It seems not to be a compiling error but a message in a popup window.
THE MESSAGE
"The scheme 'return accept int, return int' contains no buildables that can be built for the SDKs supported by the run destination My Mac 64–bit. Make sure your targets all specify SDKs that are supported by this version of Xcode."
Thanks in advance.
#include <stdio.h>
#include <math.h>
int cubed(int var);
float main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 1;
}
float cubed(int var){ //This line was "int cubed(int var)" before I started playing with it.
float result;
result = pow(var, 3.0);
return(result);// I get the same message if I take out this line
}
I realized the issue with the return type conflict and have changed the code to:
#include <stdio.h>
#include <math.h>
int cubed(int var);
int main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 0;
}
int cubed(int var){
float result;
result = pow(var, 3.0);
return(result);
}
I now know that it is a setting issue within Xcode.
I have attached a series of screen shots illustrating the message I was receiving, what I did, and the new error I'm getting now. I was able to get the code to run in the IDE CodeRunner but not Xcode.
-- I guess I don't have the reputation points to post images. --
But now I get this new message.
" your mac runs an osx that is lower than the minimum require for your project.--Change your project's minimum deployment target or upgrade your version of OS X."
Now what?
I think I had changed the return type in the function header when troubleshooting early on but forgot about the function prototype, things get a bit confusing when I have to jumping from one editor to another to another trying to troubleshoot. My class uses Dev C++ and sometimes this causes me some confusion.
Can anyone tell me how I could fix this new issue or how to avoid it in the future?

Window instantly closing when opening compiled .exe in C

I have tried so many things. Running from command line, running from cmd, running with /K, putting system("pause"); getchar(); getch(); before return 0 and I simply can't get it to run. I'm writing in Notepad++, compiling in Cygwin and the window appears blank for the split second it appears (according to my screenshot, it could have been taken too early). Basically I've tried anything I could Google myself to. So I figured it must be something wrong with my code that the debugger doesn't show.
#include <stdio.h>
int main()
{
float lt1, lt2, dmg, x;
lt1=10;
lt2=30;
while(lt2>dmg)
{
while(x>0 || lt2>dmg)
{
dmg=dmg+x*lt1;
x--;
return (dmg);
}
x=x+0.01;
return (x);
}
printf("Horde factor is: %f", x);
return 0;
}
I would appreciate any help I can get, and I hope you will bear over with my inexperience.
You have undefined behavior in your code.
When you declare a local variable without assigning anything to it, its value is indeterminate. Usage of this variable will be undefined behavior until you assign a value to it.
In this case it's the dmg and x variables that causes this problem.
Its because of these statement :
return (dmg); //this ends the code execution .. because you have returned something from main()
x=x+0.01;
return (x); // even this one is wrong
you are exiting the code there and never getting to the printf ..
there should only be one return in main() .. and at the end.
More problems with your code:
you don't initialise dmg and x , but you use them as parameters for while loop
float lt1, lt2, dmg, x; // dmg,x uninitialized
In the outer while loop .. its an infinite loop as you don't do anything to the parameters of that loop to get out of it.
Like I said above .. there should be only 1 return in main()
Maybe instead of returning you should look into break; ( i don't know if thats what you want or not as I don't understand your code )

Resources