include "array.h"
int *
forloop_1_svc(input *argp, struct svc_req *rqstp)
{
int result[argp->a];
printf("Server started********\n");
for (int i = 1; i < (argp->a); ++i)
{
result[i]=i;
}
return result;
}
Related
Here is the code.what i did is implement linear search on some elements of the array and the push searched elements in stack,afterwards I print the popped elements from stack and print them.But in search function it displays two index values.
using namespace std;
int searched[10];
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
int main(void)
{
int arr[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int x = 0;
for(int i=0;i<8;i++)
{
x++;
int result = search(arr, n, x);
cout << "searched Element is " << result<<"\t\t";
push(result);
pop();
}
return 0;
}```
There are two issues that lead to this confusing result.
First, if I am not mistaken the search function, which was written like this:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
is parsed similarly to the following:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout<<"The element is found at the index"<<i<<"\n\n";
}
}
return x;
}
Presumably you meant this:
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout<<"The element is found at the index"<<i<<"\n\n";
return x;
}
}
}
Second, since the global n starts at the value 100, the search loop runs off the end of the length 15 array, into other memory. This is probably undefined behavior.
I tried using malloc on this table - struct that contains rows that contain cells. First I allocate the table, then succesfully add and initialize first row, but when trying to set cell count on second row it crashes - I have no idea why, probably some leftover from previous pointer? Or do I need to allocate memory even for props of the structs? This is my first time digging into malloc, so sorry if it is something trivial.
typedef struct cell_t
{
char* content;
int contentLength;
} cell_t;
typedef struct row_t
{
cell_t* cells[100];
int cellCount;
} row_t;
typedef struct table_t
{
row_t* rows[100];
int rowCount;
} table_t;
row_t* allocateRow()
{
row_t* allocatedRow;
allocatedRow = malloc(sizeof(row_t*));
if (allocatedRow)
{
printf("THIS PRINTS TWICE");
allocatedRow->cellCount = 0;
printf("THIS PRINTS ONCE");
}
else
{
printf("FAILED TO ALLOCATE ROW!");
}
return allocatedRow;
}
void addRow(table_t* tableToAddTo, int nToAllocate)
{
while (tableToAddTo->rowCount < nToAllocate)
{
tableToAddTo->rows[tableToAddTo->rowCount] = allocateRow();
tableToAddTo->rowCount++;
}
}
int main()
{
table_t* inputTable = malloc(sizeof(table_t));
if (inputTable)
{
inputTable->rowCount = 0;
}
else
{
printf("FAILED TO ALLOCATE TABLE!");
return 1;
}
addRow(inputTable, 5);
for (int i = 0; i < inputTable->rowCount; i++)
{
free(inputTable->rows[i]);
}
free(inputTable);
return 0;
}
In allocateRow() change
allocatedRow = malloc(sizeof(row_t*));
into
allocatedRow = malloc(sizeof(row_t));
And you will need to allocate cells or change
cell_t* cells[100];
into
cell_t cells[100];
I wrote this C extension module which calculates Fibonacci numbers using fast matrix multiplication.
#include <Python.h>
struct Matrix {
PyObject *m[2][2];
};
static struct Matrix matrix_mult(struct Matrix mat1, const struct Matrix mat2)
{
struct Matrix matrix;
PyObject *mults[8];
for (int i = 0; i < 8; i++) {
mults[i] = PyNumber_Multiply(mat1.m[i/4][i%2], mat2.m[i%2][(i/2)&1]);
}
for (int i = 0; i < 4; i++) {
matrix.m[i/2][i%2] = PyNumber_Add(mults[2*i], mults[2*i+1]);
}
for (int i = 0; i < 8; i++) {
Py_DECREF(mults[i]);
}
return matrix;
}
static void matrix_free(struct Matrix *matrix)
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Py_DECREF(matrix->m[i][j]);
}
}
}
static struct Matrix matrix_pow(struct Matrix matrix, int n)
{
struct Matrix result = {{
{PyLong_FromLong(1L), PyLong_FromLong(0L)},
{PyLong_FromLong(0L), PyLong_FromLong(1L)}
}};
struct Matrix result_old;
struct Matrix matrix_old;
while (n > 0) {
if (n % 2 == 0) {
n /= 2;
matrix_old = matrix;
matrix = matrix_mult(matrix_old, matrix_old);
matrix_free(&matrix_old);
} else {
//n--;
n /= 2;
result_old = result;
matrix_old = matrix;
result = matrix_mult(result_old, matrix);
matrix = matrix_mult(matrix_old, matrix_old);
matrix_free(&result_old);
matrix_free(&matrix_old);
}
}
return result;
}
static PyObject *fib_mat(PyObject *self, PyObject *args)
{
long long int n;
if(!PyArg_ParseTuple(args, "L", &n))
return NULL;
struct Matrix fib_matrix = {{
{PyLong_FromLong(0L), PyLong_FromLong(1L)},
{PyLong_FromLong(1L), PyLong_FromLong(1L)}
}};
struct Matrix result = matrix_pow(fib_matrix, n + 1);
PyObject *inswer = result.m[0][0];
Py_INCREF(inswer);
matrix_free(&result);
matrix_free(&fib_matrix);
return inswer;
}
static PyMethodDef func_table[] = {
{ "fib_mat", fib_mat, METH_VARARGS, "Calculates fib number" },
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef fib_module = {
PyModuleDef_HEAD_INIT,
"fib_module",
"fibonacci Module",
-1,
func_table
};
PyMODINIT_FUNC PyInit_fib_module(void)
{
return PyModule_Create(&fib_module);
}
The problem is when I run the following Python 3 code using the module:
import fib_module
i = 0
while i < 10:
fib_module.fib_mat(i)
i += 1
it returns an the error listed below:
free(): invalid pointer
Aborted (core dumped)
It appears to be caused by something inside the C extension module but there is no explicit free call.
i'm trying to convert hexadecimal number to decimal number. What i've come up so far is:
#include <unistd.h>
#include <stdio.h>
long convert(char *input, short int *status){
int length = 0;
while(input[length])
{
length++;
}
if(length = 0)
{
*status = 0;
return 0;
}
else
{
int index;
int converter;
int result = 0;
int lastNumber = length-1;
int currentNumber;
for(index = 0; index < length; index++){
if(index == 0)
{
converter = 1;
}
else if(index == 1)
{
converter = 16;
}
else{
converter *= 16;
}
if(input[lastNumber] < 45 || input[lastNumber] > 57)
{
*status = 0;
return 0;
}
else if(input[lastNumber] > 45 && input[lastNumber] < 48)
{
*status = 0;
return 0;
}
else{
if(input[lastNumber] == 45)
{
*status = -1;
return result *= -1;
}
currentNumber = input[lastNumber] - 48;
result += currentNumber * converter;
lastNumber--;
}
}
*status = -1;
return result;
}
}
int main(int argc, char **argv)
{
char *input=0;
short int status=0;
long rezult=0;
if(argc!=2)
{
status=0;
}
else
{
input=argv[1];
rezult=convert(input,&status);
}
printf("result: %ld\n", rezult);
printf("status: %d\n", status);
return 0;
}
Somehow i always get resoult 0. Ia am also not allowed to use any other outher functions (except printf). What could be wrong with my code above?
This:
if(dolzina = 0)
{
*status = 0;
return 0;
}
is not merely testing dolzina, it's first setting it to 0. This causes the else clause to run, but with dolzina equal to 0 which is not the expected outcome.
You should just use == to compare, of course.
Modified the below circular queue code for my app.
This queue can hold 32 elements max and I have declared the elements as a structure array inside the class. For adding an element to the queue you have to call CreateElement() functions, which checks for a free element and returns an index. When I reuse an element after processing the following line in the CreateElement functions crashes
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
As per documentation, the assignment operator is supposed to destroy the earlier object and assign the new one. Why is it crashing? Can someone tell me where am I screwing?
#include "boost/thread/condition.hpp"
#include "boost/smart_ptr/shared_array.hpp"
#include <queue>
#define MAX_QUEUE_ELEMENTS 32
typedef struct queue_elem
{
bool inUse;
int index;
int packetType;
unsigned long compressedLength;
unsigned long uncompressedLength;
boost::shared_array<char> data;
}Data;
class CQueue
{
private:
int m_CurrentElementsOfQueue;
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
Data m_QueueStructure[MAX_QUEUE_ELEMENTS];
public:
CQueue()
{
m_CurrentElementsOfQueue = 0;
for(int i = 0; i < MAX_QUEUE_ELEMENTS; i++)
{
m_QueueStructure[i].inUse = false;
m_QueueStructure[i].index = i;
}
}
~CQueue()
{
for(int i = 0; i < m_CurrentElementsOfQueue; i++)
{
int index = wait_and_pop();
Data& popped_value = m_QueueStructure[index];
popped_value.inUse = false;
}
m_CurrentElementsOfQueue = 0;
}
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
int wait_and_pop()
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
Data& popped_value=the_queue.front();
the_queue.pop();
return popped_value.index;
}
int CreateElement(int bufferSize, unsigned long _compressedLength,
unsigned long _uncompressedLength, int _packetType) /* Send data length for this function */
{
int queueElems = 0;
if(m_CurrentElementsOfQueue == 32)
{
CCommonException ex(QERROR, QUEUE_FULL, "Circular Buffer Queue is full");
throw ex;
}
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++)
{
if(m_QueueStructure[queueElems].inUse == false)
break;
}
boost::shared_array<char> tData(new char[bufferSize]);
m_QueueStructure[queueElems].data = tData;
m_QueueStructure[queueElems].inUse = true;
m_QueueStructure[queueElems].compressedLength = _compressedLength;
m_QueueStructure[queueElems].uncompressedLength = _uncompressedLength;
m_QueueStructure[queueElems].packetType = _packetType;
m_CurrentElementsOfQueue++;
return queueElems;
}
Data& GetElement(int index)
{
Data& DataElement = m_QueueStructure[index];
return DataElement;
}
void ClearElementIndex(Data& delValue)
{
m_CurrentElementsOfQueue--;
delValue.inUse = false;
}
};
for(queueElems = 0; queueElems < MAX_QUEUE_ELEMENTS; queueElems++) after looping queueElems has value 32 but in your m_QueueStructure only 32 elements so you trying to access m_QueueStructure[queueElems].data to 33rd element. That the problem.
EDIT: try use m_QueueStructure[queueElems].data.reset(new char[bufferSize]);
Solved the problem. Two changes I did. In the wait_and_pop function, I was returning an index rather than a Data&. When I returned Data&, that solved the assignment problem. Another crash was happening due to a memset of a shared_array.get(). Lesson learnt, never memset a shared_array or a shared_ptr.