I am trying to create an infinite loop using those functions (unsuccessfully).
My code :
jmp_buf buf1;
void foo(){
int z = 4, y = 1;
int v = setjmp(buf1);
if(v == 0){
printf("A%d", z);
longJmp(buf1, 1);
}
else if(v == 1){
printf("B%d", y);
longjmp(buf1,1);
}
}
int main(){
int v = setjmp(buf1);
if( v == 0){
printf("C%d", 1);
foo();
longjump(buf1,1);
}
}
I thought it would print C1A4B1B1B1.... (B1 repeats forever) but I just get C1A4B1 and the program stops (SEGMENTATION ERROR).
Isn't calling longJmp always return to setJmp with parameter val?
Would like to understand where is my mistake.
EDIT : The code I copied before I got answers is mistakenly incorrect, longjmp(buf1,2) instead of longjmp(buf1,1).
Also I didn't copy the full main code which cause the seg fault. (another longjmp after foo being called)
after line 15, longjmp(buf1,2);
v is 2.
When next repeat, v is not 1, function has end.
longjmp(buf1,2); > longjmp(buf1,1);
Related
I'm writing a program that will simulate a randomized race between runners who are climbing up a mountain where dwarf orcs (dorcs) are coming down the mountain to attack the runners. It begins with two runners named harold and timmy at the bottom of the mountain. The runners make their way up the mountain in randomized moves where they may make progress forward up the mountain, or they may slide back down the mountain. Dorcs are randomly generated, and they inflict damage on a runner if they collide. The simulation ends when one of the runners reaches the top of the mountain, or when both runners are dead.
I'm struggling with a part where I have to implement the actual race loop. Once the race is initialized, the race loop will iterate until the race is over. This happens when either a winner has been declared, or when all runners are dead.
Every iteration of the race loop will do the following:
with 30% probability, dynamically allocate a new dorc as an EntityType structure, and initialize it as follows:
(a) a dorc’s avatar is always “d”
(b) each dorc begins the race at the top of the mountain, which is at row 2
(c) with equal probability, the dorc may be placed either in the same column as timmy, or in the same column as the harold, or in the column exactly half-way between the two
(d) add the new dorc to the race’s array of dorcs
(e) using the pthread_create() function, create a thread for the new dorc, and save the thread pointer in the dorc’s entity structure; the function that each dorc thread will execute is the void* goDorc(void*) function that you will implement in a later step; the parameter to the goDorc() function will be the EntityType pointer that corresponds to that dorc
I guess I'm confused with the logic of how to approach this. I decided to make a function called isOver() to indicate if the race is over, and then a separate function called addDorc() to initialize the Dorc elements and do all the requirements above.
In isOver(), I attempt to add a dorc object to the dorcs array by doing addDorc(race); with every iteration of the race loop/if the race hasn't ended or no one died. But I keep getting the error:
control.c:82:3: error: too few arguments to function ‘addDorc’
addDorc(race);
The problem is I don't think I can manually declare all the parameters in addDorc() because some elements like the "path" argument are based on probability. As mentioned above, with equal probability, the dorc may be placed either in the same column as timmy, or in the same column as the harold, or in the column exactly half-way between the two. The issue is I don't know how to factor this random value when calling addDorc() and would appreciate some help. I also don't know if I'm doing the "with 30% probability, dynamically allocate a new dorc as an EntityType structure" correctly and would be grateful for some input on that as well.
defs.h
typedef struct {
pthread_t thr;
char avatar[MAX_STR];
int currPos;
int path;
} EntityType;
typedef struct {
EntityType ent;
char name[MAX_STR];
int health;
int dead;
} RunnerType;
typedef struct {
int numRunners;
RunnerType *runners[MAX_RUNNERS];
int numDorcs;
EntityType *dorcs[MAX_DORCS];
char winner[MAX_STR];
int statusRow;
sem_t mutex;
} RaceInfoType;
void launch();
int addDorc(RaceInfoType*, char*, int, int);
int isOver(RaceInfoType*);
void initRunners(RaceInfoType*);
int addRunner(RaceInfoType*, char*, char*, int, int, int, int);
int randm(int);
void *goRunner(void*);
void *goDorc(void*);
RaceInfoType *race;
control.c
void launch(){
race = malloc(sizeof(RaceInfoType));
race->numRunners = 0;
initRunners(race);
if (sem_init(&race->mutex, 0, 1) < 0) {
printf("semaphore initialization error\n");
exit(1);
}
strcpy(race->winner, " ");
srand((unsigned)time(NULL));
int i;
for(i = 0; i < race->numRunners; ++i){
pthread_create(&(race->runners[i]->ent.thr), NULL, goRunner, " ");
}
race->numDorcs = 0;
}
int addDorc(RaceInfoType* race, char *avatar, int path, int currPos){
if(race->numDorcs == MAX_DORCS){
printf("Error: Maximum dorcs already reached. \n");
return 0;
}
race->dorcs[race->numDorcs] = malloc(sizeof(EntityType));
int timmysColumn = race->dorcs[race->numDorcs]->currPos;
int haroldsColumn = race->dorcs[race->numDorcs]->currPos;
int halfwayColumn = (timmysColumn+haroldsColumn)/2;
int r = rand()%100;
pthread_t dorc;
if(r <= 30){
strcpy(race->dorcs[race->numDorcs]->avatar, "d");
race->dorcs[race->numDorcs]->currPos = 2;
if(r <= 33){
race->dorcs[race->numDorcs]->path = timmysColumn;
}else if(r <= 66){
race->dorcs[race->numDorcs]->path = haroldsColumn;
}else{
race->dorcs[race->numDorcs]->path = halfwayColumn;
}
pthread_create(&dorc, NULL, goDorc, " ");
}
race->numRunners++;
}
int isOver(RaceInfoType* race){
int i;
for(i = 0; i < race->numRunners; ++i){
if((race->winner != " ") || (race->runners[race->numRunners]->dead = 1)){
return 1;
}
addDorc(race);
return 0;
}
}
void initRunners(RaceInfoType* r){
addRunner(r, "Timmy", "T", 10, 35, 50, 0);
addRunner(r, "Harold", "H", 14, 35, 50, 0);
}
int addRunner(RaceInfoType* race, char *name, char *avatar, int path, int currPos, int health, int dead){
if(race->numRunners == MAX_RUNNERS){
printf("Error: Maximum runners already reached. \n");
return 0;
}
race->runners[race->numRunners] = malloc(sizeof(RunnerType));
strcpy(race->runners[race->numRunners]->name, name);
strcpy(race->runners[race->numRunners]->ent.avatar, avatar);
race->runners[race->numRunners]->ent.path = path;
race->runners[race->numRunners]->ent.currPos = currPos;
race->runners[race->numRunners]->health = health;
race->runners[race->numRunners]->dead = dead;
race->numRunners++;
return 1;
}
Caveat: Because there's so much missing [unwritten] code, this isn't a complete solution.
But, I notice at least two bugs: the isOver bugs in my top comments. And, incrementing race->numRunners in addDorc.
isOver also has the return 0; misplaced [inside the loop]. That should go as the last statement in the function. If you had compiled with -Wall [which you should always do], that should have been flagged by the compiler (e.g. control reaches end of non-void function)
From that, only one "dorc" would get created (for the first eligible runner). That may be what you want, but [AFAICT] you want to try to create more dorcs (one more for each valid runner).
Also, the bug the compiler flagged is because you're calling addDorc(race); but addDorc takes more arguments.
It's very difficult to follow the code when you're doing (e.g.) race->dorcs[race->numDorcs]->whatever everywhere.
Better to do (e.g.):
EntityType *ent = &race->dorcs[race->numDorcs];
ent->whatever = ...;
Further, it's likely that your thread functions would like a pointer to their [respective] control structs (vs. just passing " ").
Anyway, I've refactored your code to incorporate these changes. I've only tried to fix the obvious/glaring bugs from simple code inspection, but I've not tried to recompile or address the correctness of your logic.
So, there's still more work to do, but the simplifications may help a bit.
void
launch(void)
{
race = malloc(sizeof(RaceInfoType));
race->numRunners = 0;
initRunners(race);
if (sem_init(&race->mutex,0,1) < 0) {
printf("semaphore initialization error\n");
exit(1);
}
strcpy(race->winner," ");
srand((unsigned)time(NULL));
int i;
for (i = 0; i < race->numRunners; ++i) {
RunnerType *run = &race->runners[i];
EntityType *ent = &run->ent;
pthread_create(&ent->thr,NULL,goRunner,ent);
}
race->numDorcs = 0;
}
int
addDorc(RaceInfoType* race,char *avatar,int path,int currPos)
{
if (race->numDorcs == MAX_DORCS) {
printf("Error: Maximum dorcs already reached. \n");
return 0;
}
EntityType *ent = malloc(sizeof(*ent));
race->dorcs[race->numDorcs] = ent;
int timmysColumn = ent->currPos;
int haroldsColumn = ent->currPos;
int halfwayColumn = (timmysColumn + haroldsColumn) / 2;
int r = rand()%100;
#if 0
pthread_t dorc;
#endif
if (r <= 30) {
strcpy(ent->avatar,"d");
ent->currPos = 2;
if (r <= 33) {
ent->path = timmysColumn;
} else if (r <= 66) {
ent->path = haroldsColumn;
} else {
ent->path = halfwayColumn;
}
pthread_create(&ent->thr,NULL,goDorc,ent);
}
#if 0
race->numRunners++;
#else
race->numDorcs += 1;
#endif
}
int
isOver(RaceInfoType* race)
{
int i;
for (i = 0; i < race->numRunners; ++i) {
#if 0
if ((race->winner != " ") ||
(race->runners[race->numRunners]->dead = 1))
return 1;
#else
RunnerType *run = &race->runners[i];
if ((race->winner != " ") || (run->dead == 1))
return 1;
#endif
addDorc(race);
#if 0
return 0;
#endif
}
#if 1
return 0;
#endif
}
void
initRunners(RaceInfoType* r)
{
addRunner(r,"Timmy","T",10,35,50,0);
addRunner(r,"Harold","H",14,35,50,0);
}
int
addRunner(RaceInfoType* race,char *name,char *avatar,int path,int currPos,
int health,int dead)
{
if (race->numRunners == MAX_RUNNERS) {
printf("Error: Maximum runners already reached. \n");
return 0;
}
RunnerType *run = malloc(sizeof(*run));
race->runners[race->numRunners] = run;
strcpy(run->name,name);
EntityType *ent = &run->ent;
strcpy(ent->avatar,avatar);
ent->path = path;
ent->currPos = currPos;
run->health = health;
run->dead = dead;
race->numRunners++;
return 1;
}
UPDATE:
I noticed in addDorc(), you put pthread_t dorc; in an if statement. I don't quite understand what my if statement is actually supposed to be checking though.
I forgot to mention/explain. I wrapped your/old code and my/new code with preprocessor conditionals (e.g.):
#if 0
// old code
#else
// new code
#endif
After the cpp stage, the compiler will only see the // new code stuff. Doing this was an instructional tool to show [where possible] what code you had vs what I replaced it with. This was done to show the changes vs. just rewriting completely.
If we never defined NEVERWAS with a #define NEVERWAS, then the above block would be equivalent to:
#ifdef NEVERWAS
// old code ...
#else
// new code
#endif
Would it still be under the if(r <= 30) part like I did in my original code?
Yes, hopefully now, it is more clear. #if is a cpp directive to include/exclude code (as if you had edited that way). But, a "real" if is an actual executable statement that is evaluated at runtime [as it was before], so no change needed.
My other concern is it doesn't look like dorc is used anywhere in the function because you write pthread_create(&ent->thr,NULL,goDorc,ent); which seems to use ent instead?
That is correct. It is not used/defined and the value goes to ent->thr. As you had it, the pthread_t value set by pthread_create would be lost [when dorc goes out of scope]. So, unless it's saved somewhere semi-permanent (e.g. in ent->thr), there would be no way to do a pthread_join call later.
/I am trying to return the 1st bit of boolean value of 10 using right shift in the cb function./
#include<stdbool.h>
bool cb(int N,int i){ //`called function`
return ((N>>i)&1)==1;
}
int main(void) { //`main function`
cb(10,1);
return 0;
}
//Status:successfully Executed,but no output.
main doesn't magically return the result of another function, you need to return the value also from main
int main(void)
{
return cb(10, 1);
}
or you can exit the program from your function with a value:
bool cb(int N,int i){ //`called function`
exit(((N>>i)&1)==1 ? EXIT_FAILURE : EXIT_SUCCESS);
}
and check the return in the console:
./yourprogram
echo $?
But notice that this is considered bad practice, we usualy return EXIT_FAILURE only when something went wrong, instead, you can print the result:
int main(void)
{
printf("%d\n", cb(10, 1));
return 0;
}
Finaly, you can use a debugger
Change your code to
Line 6 int res = cb(10, 1);
Line 7 return 0;
and start the debugger
gdb yourprogram
breakpoint 7 (after the line you want to inspect)
run
print res
So Here's your program:
#include<stdbool.h>
//`called function`
bool cb(int N,int i)
{
return ((N >> i) & 1) ==1;
}
//`main function`
int main(void)
{
cb(10,1);
return 0;
}
Your program is executing - which means that the main() function is returning successfully (a Value of 0). You also invoke cb(10,1); which calls your function declaration above (and returns a boolean: True/False). But you don't store the value of that function call, nor display the value with a printf() or cout statement.
You'll need to add more for your program to give you more noticable output.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am wondering for a while what is the best practice to handle errors in returning values function in C.
First, I would like to introduce the need then share a few solutions that I tried and to hear different ideas.
The issue is when I have a returning value function, this function can return any value in the range, and the function sometimes has a problem that it must return as well to the calling function, it cannot use the traditional return for that cause.
How can I handle that error in the calling function?
few notes:
1. I am an Embedded programer, and I am keen on keeping my function reentrant (pure) functions in a way that different interrupts won't harm the globals, I hardly use globals in my code.
I can't handle it with 0 or -1 because it is a valid return as well.
the errno solution doesn't support pure functions as well as 1.
4.I tried using structs of return which I have one field for the value and one field for the error if it has occurred.
unsigned int squre(unsigned int num)
{
return num*num;
}
programmer say I would like to have handle for overflow.
struct returnUnsignedint squre(unsigned int num)
{
struct returnUnsignedint returnValue;
if (num>65535) { //my embedded system over flow
returnValue.error = 1;
}
returnValue.value = num*num;
return returnValue;
}
is there a better option out there?
Let me know if you have different point of view, or solutions.
I would appreciate any help, thanks!
There's no "one size fits all" solution, since it depends on needs of your program.
However, there are a few possibilities.
One way is to specify that one possible return value of your function can indicate an error occurred. For example, since not every value of an unsigned is the square of another, pick a suitable value and return that.
unsigned sqre(unsigned x)
{
if (x == 0U)
{
return 0U;
}
else if (UINT_MAX/x >= x) /* use fact that UINT_MAX is typically not a perfect square */
{
return x*x;
}
else
{
return UINT_MAX;
}
}
(Note, in the above, that I have eliminated your implicit assumption that unsigned is at least 32-bit, by avoiding use of the magic value 65535).
Another option is to do what some standard library functions do: return 0 (or, in the case of unsigned, return 0U, on error) even if it is feasible that value is valid. That means your function always returns a usable value, but the caller will need to decide what to do if your function returns zero.
Another option is to return a data structure
struct Return
{
unsigned value;
int error;
};
struct Return sqre(unsigned x)
{
struct Return retval;
retval.error = 0;
if (x == 0)
{
retval.value = 0U;
}
else if (UINT_MAX/x >= x) /* use fact that UINT_MAX is typically not a perfect square */
{
retval.value = x*x;
}
else
{
retval.error = 1;
}
return retval;
}
The trade-off is that forces the caller to create an instance of the struct and then check or extract data from it.
Another is to provide a second argument that provides an error indication.
unsigned sqre(unsigned x, int *error)
{
*error = 0;
if (x == 0U)
{
return 0U;
}
else if (UINT_MAX/x >= x) /* use fact that UINT_MAX is typically not a perfect square */
{
return x*x;
}
else
{
*error = 1;
return 0U; /* falling off end without a return statement gives undefined behaviour */
}
}
The disadvantage of the above is that the caller can forget to check the error condition. It is trivial to modify the above so it checks if error is NULL and then doesn't modify *error (and then allow the caller to specify a NULL to indicate no interest in the error condition).
An alternative is for the function to return the error condition, and require the caller to pass the address of a variable to hold the result (if no error occurs). A disadvantage of this is that the result from the function can't be used directly in larger expressions.
Since, technically, overflow of unsigned gives well-defined behaviour (essentially modulo arithmetic), use your version that does no checks. This option isn't feasible if the function returns a signed int (since overflow gives undefined behaviour). This requires the caller to deal with the fact that the returned value may be truncated (e.g. high order part of the value lost).
Yet another option is for the function to terminate with prejudice if an overflow would occur. For example;
unsigned sqre(unsigned x)
{
assert(x == 0 || UINT_MAX/x < x); /* from <assert.h> */
return x*x;
}
This removes the responsibility of the caller to check. However, the caller (if program termination is undesirable) must then ensure the argument passed is valid. Alternatively, the end-user would need to be willing to accept that the program may terminate on bad data.
Another option is to return the error code and write the output value to a parameter:
int sqr( unsigned int num, unsigned int *result )
{
if ( num > 65535 )
return 0;
*result = num * num;
return 1;
}
This isn’t always the most convenient option (especially if you want to use sqr as part of a larger arithmetic expression), but it should meet your requirements.
EDIT
Of course, you could always go the other way - return the value and write the error code to a parameter:
unsigned int sqr( unsigned int num, int *err ) { ... }
but frankly I prefer the first version, since you aren't tempted to use the return value unless you know the operation succeeded.
Following up John's answer I propose an additional macro to be able to use the function in a "larger arithmetic expressions"
#include <stdlib.h> /* for EXIT_xxx macros */
#include <stdio.h> /* for perror() */
#include <errno.h> /* for errno */
int sqr(unsigned int num, unsigned int *psqr)
{
int result = 0;
if (NULL == psqr)
{
result = -1;
errno = EINVAL;
}
else if (num > 0xffffU)
{
result = -1;
errno = ERANGE;
}
else
{
*psqr = num * num;
}
return result;
}
#define SQR(x, y) \
((-1 == sqr(x, &y)) \
? (perror("sqr() failed"), exit(EXIT_FAILURE), 0U) \
: y \
)
Some tests below (please note that the macro SQR() has to end the program if sqr() fails):
int main(void)
{
unsigned int r, i;
puts("Test case 1:");
i = 42;
if (-1 == sqr(i, &r))
{
perror("sqr() failed");
}
else
{
printf("sqr(%u) = %u\n", i, r);
}
puts("Test case 2:");
i = 0x10000;
if (-1 == sqr(i, &r))
{
perror("sqr() failed");
}
else
{
printf("sqr(%u) = %u\n", i, r);
}
puts("Test case 3:");
if (-1 == sqr(i, NULL))
{
perror("sqr() failed");
}
else
{
printf("sqr(%u) = %u\n", i, r);
}
puts("Test case 4:");
r = SQR(1, r) + SQR(2, r);
printf("sqr(%u) + sqr(%u) = %u\n", 1, 2, r);
puts("Test case 5:");
r = SQR(0x10000, r) + SQR(2, r);
printf("sqr(%u) + sqr(%u) = %u\n", 0x10000, 2, r);
puts("Test case 6:");
r = SQR(NULL, r) + SQR(2, r);
printf("sqr(%u) + sqr(%u) = %u\n", 0x10000, 2, r);
return EXIT_SUCCESS;
}
The output is:
Test case 1:
sqr(42) = 1764
Test case 2:
sqr() failed: Numerical result out of range
Test case 3:
sqr() failed: Invalid argument
Test case 4:
sqr(1) + sqr(2) = 5
Test case 5:
sqr() failed: Numerical result out of range
Test case 6 is never reached as test case 5 ends the program.
I tried to get the return value of C function through Lua library, but failed.
My code is as follows:
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <stdio.h>
static int testcmd(lua_State *L)
{
lua_pushnumber(L, 0xBADF00D);
return 1;
}
lua_State *initLua()
{
lua_State *L = luaL_newstate();
lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L);
lua_register(L, "testcmd", testcmd);
lua_gc(L, LUA_GCRESTART, 0);
return L;
}
int main(void)
{
lua_State *L = initLua();
int error = luaL_loadbuffer(L, "testcmd()", 9, "line");
if (error) { printf("Error # luaL_loadbuffer()\n"); return 0; }
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
if (lua_gettop(L) > 0) {
int i;
for (i = 1; i <= lua_gettop(L); ++i) {
printf("%d: %g\n", i, lua_isnumber(L, i) ? lua_tonumber(L, i) : 0.0);
}
} else {
printf("No data in stack\n");
}
lua_close(L);
return 0;
}
I expect to get 1 floating point value which is about 0xBADF00D in L after lua_call(). However, the actual result is No data in stack.
How could I get the value pushed to stack in testcmd()?
You've told the C function to return the value to lua when called (that's what pushing the value on the stack and returning 1 is doing).
What you haven't done is return that returned value from the lua chunk you are running.
When you call the function from lua (using luaL_loadbuffer(L, "testcmd()", 9, "line");) your statement to run is testcmd() which doesn't do anything with the return value so it doesn't get returned from that chunk.
return testcmd() is the code you need to run to do that.
Recall that with that luaL_loadbuffer/lua_pcall pair you aren't executing a direct call of testcmd. Rather, you are executing lua code that calls testcmd.
I'm sure the mistake is obvious, but I sure am having trouble finding it.
Basically I am trying to make a chessboard via 2D array. I am testing its functionality via 8 queens test... it is not functional.
Somehow one of my integer values is getting out of whack, as gdb shows:
....
(gdb) cont
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x080487f8 in diagnols (PARAMETER_ONE=0, PARAMETER_TWO=0) at eq1.c:77
77 if (key->board[abc][bcd] == 1) {
(gdb) print abc
4424 // "SHOULD BE" ONE
(gdb) print bcd
4424 // "SHOULD BE" ONE
(gdb) backtrace
#0 0x080487f8 in diagnols (PARAMETER_ONE=0, PARAMETER_TWO=0) at eq1.c:77
#1 0x08048873 in check (param1=0, param2=0) at eq1.c:91
#2 0x08048510 in recur (DEPTH=0, WIDTH=0) at eq1.c:99
#3 0x08048919 in main () at eq1.c:152
(gdb)
Then here is diagnols(...), which is in:
int recur(struct chessboard* key, int DEPTH, int WIDTH) {
/* other functions above diagnols(...) */
diagnols(...):
int diagnols(int PARAMETER_ONE, int PARAMETER_TWO) { // returns 0 if good
int abc = 0;
int bcd = 0;
int counter = 0; // keeps track of conflicting piece occurrences
// OTHER CHECKS FIRST... DELETED TO SAVE ROOM
// checkign diagnol down and to the left
abc = PARAMETER_ONE+1;
bcd = PARAMETER_TWO-1;
while ( (abc>=0)&&(bcd>=0) ) {
if (key->board[abc][bcd] == 1) {
counter++;
} abc++;
bcd--;
}
// ERROR IN THIS PART
// checking diagnol down and to the right
abc = PARAMETER_ONE+1;
bcd = PARAMETER_TWO+1;
while ( (abc>=0)&&(bcd>=0) ) {
if (key->board[abc][bcd] == 1) { // ERROR
counter++;
} abc++;
bcd++;
}
return counter;
}
And diagnols(...) is invoked in recur(...) in the below function:
int check(int param1, int param2) { // if okay returns 2
// other functions
d = diagnols(param1, param2);
int total = 0;
total = (h + v + d); // if okay, equals 2
return total;
}
For good measure here is my struct:
struct chessboard {
int board[7][7];
};
And main:
int main() {
struct chessboard* master = malloc(sizeof(struct chessboard));
/* i set the board to zero here. used calloc() before */
recur(master, 0, 0);
// stuff
}
And yes, I realize diagnol isn't spelled diagonal ;)
while ( (abc>=0)&&(bcd>=0) ) {
if (key->board[abc][bcd] == 1) { // ERROR
counter++;
} abc++;
bcd++;
}
It seems that the condition is going to always be true since you are increasing both indexes.
Did you mean < some limit instead?