What is lua_pcall doing here? - c

I am trying to learn lua and I seem to be stuck here.
For some reason the following code doesn't actually run the lua file.
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
int width = 0;
int height = 0;
if(!luaL_loadfile(L, "./src/luaconf.lua") || !lua_pcall(L, 0, 0, 0))
{
lua_getglobal(L, "width");
lua_getglobal(L, "height");
if(!lua_isnumber(L, -2))
{
luaL_error(L, "width isn't a number");
}
else
{
width = lua_tointeger(L, -2);
}
if(!lua_isnumber(L, -1))
{
luaL_error(L, "height isn't a number");
}
else
{
height = lua_tointeger(L, -1);
}
}
printf("%i x %i", width, height);
return 0;
}
I know that if I change if(!luaL_loadfile(L, "./src/luaconf.lua") || !lua_pcall(L, 0, 0, 0) to if(luaL_dofile(L, "./src/luaconf.lua")) it would work but I want to know why the above code doesn't work.
Shouldn't lua_pcall run the lua code? If not why not?
luaconf.lua
width = 500
height = 40

Note that in the code:
if(!luaL_loadfile(L, "./src/luaconf.lua") || !lua_pcall(L, 0, 0, 0))
if luaL_loadfile succeeds, it returns LUA_OK which has the value of 0, so the left operand of || evaluates as 1, according to short circuit, lua_pcall won't be executed.
So what you want is probably:
if ((luaL_loadfile(L, "./src/luaconf.lua") || lua_pcall(L, 0, 0, 0))
{
//error handle
}
else
{
//normal handle
}

Related

flickering in allegro with loops

The red bars a cross the top flickers. I can make them out of the loop but not in the loop.
#include <allegro.h>
void init();
void deinit();
int main() {
init();
int length = 20;
int x= 900;
int y= 600;
int c;
//while (!key[KEY_ESC]) {
c = makecol(0, 0, 0);
rectfill (screen, 0, 0, 900, 400, c);
c = makecol(0, 200, 0);
rectfill (screen, 0, 0, 900, 400, c);// curb
for (int i =0; i=10; i++){
int bar=0;
c = makecol(200, 0, 0);
rectfill (screen, bar, 0, x+80, 40, c);
c = makecol(0, 0, 0);
rectfill (screen, bar+80, 0, x+100, 40, c);
bar+=100;
}
deinit();
return 0;
}
END_OF_MAIN()
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 900, 600, 0,0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}
void deinit() {
clear_keybuf();
/* add other deinitializations here */
}
So what why does it flicker and how do I stop that. I have had
issue with for loops before do they just not work whit allegro?
or am I being dumb and just written something wrong?

why does a call of a function with set parameters does not generate the expected output?

In the following code, when I debugged it, the first call function (mazesentence(0, 0, 1, 0);) also went to the if(before == 1 ), but 'before' is clearly a zero. Why is it entering in that location? I would imagine, i have my braces incorrectly.
#include <stdio.h>
void mazesentence(int little, int twist, int twisting, int before)
{
if(little == 0 && twist == 0 && twisting == 1)
{
if(before == 0)
{
if(twisting == 0)
printf("twisty ");
else if(twisting == 1)
printf("twisting ");
printf("little ");
printf("maze of ");
}
if(before == 1)
printf("little ");
{
if(twisting == 0)
printf("twisty ");
else if(twisting == 1)
printf("twisting ");
printf("maze of");
}
}
return;
}
int main( )
{
mazesentence(0, 0, 1, 0);
mazesentence(0, 0, 1, 1);
mazesentence(0, 0, 0, 0);
mazesentence(0, 0, 0, 1);
mazesentence(0, 1, 1, 0);
mazesentence(0, 1, 0, 0);
mazesentence(1, 0, 1, 0);
mazesentence(1, 0, 0, 0);
mazesentence(1, 1, 1, 0);
mazesentence(1, 1, 1, 1);
mazesentence(1, 1, 0, 0);
mazesentence(1, 1, 0, 1);
return 0;
}
Your braces are indeed incorrect. Looking at the problem conditional:
if(before == 1)
printf("little ");
{
The way this is, the if only guards the printf("little "). That is then is followed by a code block that executes unconditionally.
It should be this to guard the entire block:
if(before == 1)
{
printf("little ");

How to use OCIBindDynamic to INPUT/OUTPUT in one variable?

I have some code on OCI/C transport for my program. But I haven't any result after using struct with OCIBindDynamics, I think error where I need to remove cbf_no_data, and add BindByName for output variable after bind by dynamic. I use not a stack for memory, so problem not in data removing, after function
int conn_dynamic(uint16_t id,int pos,char *name,uint16_t type,uint32_t size,int16_t ind,void *data,uint16_t typein,uint32_t sizein,int16_t indin,void *datain,uint16_t mass_arr){
CHECK_CURSOR_ID(id)
int state = 1;
cb_context_t* cbct = (cb_context_t*)malloc(sizeof(cb_context_t));
cbct->cht.ptr = (void*)malloc(sizeof(void*));
cbct->err = connection.errhp;
temp_arr_num = mass_arr;
CHECK_IN_ID(id, pos)
uint8_t bindbyname = 1;
char bindname[65536];
uint32_t bindpos;
if (name[0] >= '0' && name[0] <= '9') {
bindbyname = 0;
bindpos = atoi(name);
} else {
bindname[0] = ':';
strcpy(bindname + 1, name);
}
connection.statements[id].inbinds[pos].type = typein;
connection.statements[id].inbinds[pos].ind = indin;
memcpy(connection.statements[id].inbinds[pos].ptr, datain, sizein);
if (typein == SQLT_CLOB || typein == SQLT_BLOB) {
assert(connection.statements[id].inbinds[pos].lob == NULL);
OCI_CALL(OCIDescriptorAlloc(connection.envhp, (void **)&connection.statements[id].inbinds[pos].lob, OCI_DTYPE_LOB, 0, 0));
OCI_CALL(OCILobCreateTemporary(connection.svchp, connection.errhp, connection.statements[id].inbinds[pos].lob, (ub2)OCI_DEFAULT, OCI_DEFAULT,
(typein == SQLT_CLOB)?OCI_TEMP_CLOB:OCI_TEMP_BLOB, OCI_ATTR_NOCACHE, OCI_DURATION_SESSION));
OCI_CALL(OCILobOpen(connection.svchp, connection.errhp, connection.statements[id].inbinds[pos].lob, OCI_LOB_READWRITE));
uint32_t amtp = sizein;
OCI_CALL(OCILobWrite(connection.svchp, connection.errhp, connection.statements[id].inbinds[pos].lob, &amtp, 1, datain, sizein, OCI_ONE_PIECE, 0, 0, 0, SQLCS_IMPLICIT));
if (bindbyname) {
OCI_CALL(OCIBindByName(connection.statements[id].handle, &(connection.statements[id].inbinds[pos].handle), connection.errhp, (OraText *) name, strlen(name),
&(connection.statements[id].inbinds[pos].lob), 0, typein, 0, 0, 0, 0, 0, OCI_DATA_AT_EXEC));
} else {
OCI_CALL(OCIBindByPos(connection.statements[id].handle, &(connection.statements[id].inbinds[pos].handle), connection.errhp, bindpos,
&(connection.statements[id].inbinds[pos].lob), 0, typein, 0, 0, 0, 0, 0, OCI_DATA_AT_EXEC));
}
} else {
if (!(connection.statements[id].inbinds[pos].ptr = malloc(sizein))) { //create temporary storage for value
connection.error.code = 301;
strcpy(connection.error.msg, "Out of memory");
return -1;
}
// if (bindbyname) {
// OCI_CALL(OCIBindByName(connection.statements[id].handle, &(connection.statements[id].inbinds[pos].handle), connection.errhp, (OraText *) name, strlen(name),
// &(connection.statements[id].inbinds[pos].ptr), sizein, typein, &connection.statements[id].inbinds[pos].ind, 0, 0, 0, 0, OCI_DATA_AT_EXEC));
// } else {
// OCI_CALL(OCIBindByPos(connection.statements[id].handle, &(connection.statements[id].inbinds[pos].handle), connection.errhp, bindpos,
// &(connection.statements[id].inbinds[pos].ptr), sizein, typein, &connection.statements[id].inbinds[pos].ind, 0, 0, 0, 0, OCI_DATA_AT_EXEC));
// }
}
uint16_t oratype=0;
WRITELOG(params,LDEBUG1,"TYPE IN BIND %d \n", type);
switch (type) {
case 2:{
oratype = SQLT_INT;
OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_get_numeric_data);
break;
}
case 3:{
oratype = SQLT_FLT;
OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_get_numeric_data);
break;
}
case 1:
case 4:
case 5:
{
if (type == 1){
oratype = SQLT_STR;
WRITELOG(params,LDEBUG1,"call str\n");
OCI_CALL(OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_get_string));
}
if (type == 5){ oratype = SQLT_CLOB;
OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_get_clob_data);
}
if (type == 4){
oratype = SQLT_BLOB;
OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_get_blob_data);
break;}
case 6:{
oratype = SQLT_RSET;
OCIBindDynamic(connection.statements[id].outbinds[pos].handle,connection.errhp,(dvoid*)cbct,cbf_no_data,(dvoid*)cbct,cbf_ref_cursor);
break;}
}
default:
return -1;
}
printf("error here??");
out_array[temp_arr_num]->type = oratype;
out_array[temp_arr_num]->pos = pos;
printf("No");
//it's not a magic numbers. See outputbind in commands.c
cursorid = id;
//WRITE LOG HERE
return 1;
}
So I don't have output bind, and program try to fetch variable. As example cbf_get_string
sb4 cbf_get_string(dvoid *ctxp, OCIBind *bindp, ub4 iter, ub4 index,
dvoid **bufpp, ub4 **alenp, ub1 *piecep,
dvoid **indpp, ub2 **rcodepp) {
cb_context_t* cbct = (cb_context_t*)ctxp;
ub4 pos = *((ub4 *)ctxp);
/* find out how many rows we are expecting */
if (index == 0) {
int rows = 0;
OCIAttrGet((dvoid*)bindp, OCI_HTYPE_BIND, (dvoid*)&rows, (ub4*)sizeof(int), OCI_ATTR_ROWS_RETURNED, cbct->err);
int sz = rows * sizeof(out_string_t);
void* rs = (void*)malloc(sz);
memcpy(cbct->cht.ptr, &rs, sizeof(void*)); /* ptr is now a pointer to a pointer */
#ifdef DEBUG
char dbuf[256]; snprintf(dbuf, 255, "cbf_get_string: rows=%d rs=%p cbct->cht.ptr=%p bytes=%d", rows, rs, *(void**)cbct->cht.ptr, sz); debug(dbuf);
#endif
}
out_string_t* offset = (out_string_t*) ((*(void**)cbct->cht.ptr) + (index * sizeof(out_string_t)));
*indpp = (dvoid*)&offset->indicator;
*rcodepp = (dvoid*)&offset->rc;
*bufpp = (dvoid*)&offset->bufpp;
int size = sizeof(bufpp);
offset->alenp = size;
*alenp = (dvoid*)&offset->alenp;
*piecep = OCI_ONE_PIECE;
out_array[temp_arr_num]->ptr = cbct->cht.ptr;
out_array[temp_arr_num]->index = index;
WRITELOG(params,LDEBUG1,"string in bind %s \n", *(offset->bufpp));
return OCI_CONTINUE;
}
Can you show me, where I wrong? Cause I can't show some errors. I try only bind 'a' to input and 'a' to output. As example "Begin :a := :b End;
And I need view a

Hamiltonian Cycle with fork

hellow friends
i want to write Hamiltonian Cycle for 5 vertexes with fork. and i want to create a process for each vertex to check is it true vertex in (hamCycleUtil function )or no. so i write this code but it has false output.i cant solve the problem .pleas help.how can i write this code true ?i just want create 5 process to check the vertexes.
/*
* C Program to Find Hamiltonian Cycle
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include<sys/types.h>
#include<unistd.h>
#define V 5
void printSolution(int path[]);
/*
* check if the vertex v can be added at index 'pos' in the Hamiltonian Cycle
*/
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
if (graph [path[pos-1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
/* solve hamiltonian cycle problem */
bool hamCycleUtil(bool graph[V][V], int path[], int pos)
{
int pid;
int s=-1;//counter
if (pos == V)
{
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}
for (int v = 1; v < V; v++)
{
pid=fork();
s++;
if(s<V)// to control the number of fork
{
if(v==1)
{
pid=1;}
pid=fork();
s++;
if(pid < 0) {
printf("Error");
}
else if (pid == 0){
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
else {
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
}
}
return false;
}
/* solves the Hamiltonian Cycle problem using Backtracking.*/
bool hamCycle(bool graph[V][V])
{
int *path = malloc(V*sizeof(int));
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false)
{
printf("\nSolution does not exist");
return false;
}
printSolution(path);
return true;
}
/* Main */
void printSolution(int path[])
{
printf("Solution Exists:");
printf(" Following is one Hamiltonian Cycle \n");
for (int i = 0; i < V; i++)
printf(" %d",path[i]);
printf(" %d",path[0]);
}
int main()
{
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4) */
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
{ hamCycle(graph1);
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4) */
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}}

Dynamic List <malloc.h> free triggered a breakpoint - C Programming

Could someone help me with this one. I'm making BFS algorithm in C programming language, with help of dynamic lists.
This is my code.
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <malloc.h>
#define n 6
typedef struct field
{
int x;
int y;
int dist;
struct field *next;
}Field;
// Get row and column number of 4 neighbours
int rows[] = { -1, 0, 0, 1 };
int cols[] = { 0, -1, 1, 0 };
Field *addToEnd(Field *lst, Field field)
{
Field *newField = (Field*)malloc(sizeof Field);
newField = &field;
newField->next = NULL;
if (!lst)
return newField;
else
{
Field *current;
for (current = lst; current->next; current = current->next);
current->next = newField;
return lst;
}
}
// Check if field is/isn't out of range
bool isValid(int row, int col)
{
return (row >= 0) && (row < n) && (col >= 0) && (col < n);
}
int BFS(int mat[][n], Field source, Field destination)
{
bool visited[n][n];
memset(visited, false, sizeof visited);
// Mark the source field as visited
visited[source.x][source.y] = true;
// Create dynamic list
Field *lst = NULL;
source.dist = 0;
// Adding the source field to the list
lst = addToEnd(lst, source);
while (lst)
{
// Getting first element in the list
Field current = *lst;
// If destination is reached then end function
if (current.x == destination.x && current.y == destination.y)
return current.dist;
// Delete first element of the list
Field *toDelete;
toDelete = lst;
lst = lst->next;
free(toDelete);
for (int i = 0; i < 4; i++)
{
int row = current.x + rows[i];
int col = current.y + cols[i];
// If adjacent field is valid, has path and isn't visited add it to the list
if (isValid(row, col) && mat[row][col] == 0 && !visited[row][col])
{
visited[row][col] = true;
Field adjField = { row, col, current.dist + 1 };
lst = addToEnd(lst, adjField);
}
}
}
// Return -1 if destination can't be reached
return -1;
}
int main()
{
int mat[n][n] =
{
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0 },
{ 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0 }
};
Field start = { 1, 0 }, end = { 5, 5 };
int number = BFS(mat, start, end);
if (number != -1)
printf("%d\n", number);
else
printf("Destination can't be reached\n");
return 0;
}
Everything except part of code which need to delete first element of list is working. The problem is with free function.
https://i.stack.imgur.com/QpK1j.png
https://i.stack.imgur.com/HXp8d.png
Does anyone have an idea what could be possible wrong with this and how can I fix it?

Resources