Can't convert code from C to ASM (Undefined symbol) - c

I'm trying to convert the code below from C to ASM because I'm having a tiny difficulty writing a procedure in ASM.
in DDOSBox the command I use: tcc -v -S -r- main.c
I'm getting an error: Undefined symbol _initarr in module main.c
Code: [main.c]
#include <stdio.h>
#include <stdlib.h>
extern int initarr(int** arr, int n, int (*initfunc)(int));
int getNum(int idx);
int getNum(int idx) {
return (4*idx);
}
void main() {
int *arr, i, n, success;
printf("\nPlease enter the array size\n");
scanf("%d",&n);
success = initarr(&arr, n, getNum);
if(!success) {
printf("Memory Allocation Failed\n");
return;
}
printf("\nThe Numbers in the allocated array are:\n");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
return;
}

Related

Why is the following code showing run-time error(SIGSEGV)?

This is my solution to the Prime Generator problem on SPOJ(Sphere Online Judge)(http://www.spoj.com/problems/PRIME1/). I have allocated memory using malloc(), but it is still showing a run-time error(SIGSEGV).
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
scanf("%d", &n);
while(n--){
int a,b;
scanf("%d %d", &a, &b);
int *z =malloc(sizeof(int)*(b-1));
// Filling the array
int i;
for(i=0;i<b-1;i++){
z[i]=i+2;
}
int k,p;
for(k=0;k<=b-2;k++){
if(z[k]){
if(z[k]>=a){
printf("%d\n", z[k]);
}
for(p=k+z[k];p<=b-2;p+=z[k]){
z[p]=0;
}
}
}
free(z);
}
return 0;
}

Unable to shuffle cards randomly

I am doing a project that shuffle 10 cards (12) time randomly but it didn't work with my code.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void show(int[],int);
void shuffle(int[],int,int);
int main (void)
{
int karten[]={1,2,3,4,5,6,7,8,9,10};
int n = sizeof(karten)/sizeof(int);
//printf("%d",n);
int s=12;
srand(time(NULL));
printf("Karten vor dem Mischen: \n");
show(karten,n);
shuffle(karten,n,s);
printf("Karten nach dem Mischen:\n");
show(karten,n);
return 0;
}
void show(int karten[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d,",karten[i]);
}
printf("\n");
}
void shuffle(int karten[],int n,int s)
{
int i=0;
int d=0;
int v[]={(int)malloc(sizeof(karten))};
for(int z=0;z<=s;z++)
{
i=rand()%10;
d=rand()%10;
//printf("%d-->%d\n",i,d);
v[i]=karten[i];
v[d]=karten[d];
karten[d]=v[i];
karten[i]=v[d];
}
printf("Es wurden %d Vertauschungen gemacht\n",s);
free(v);
}
The error is
(((process returned 255)))
and the program crashed.
void shuffle(int karten[],int n,int s) is equivalent to
void shuffle(int *karten,int n,int s)
so int v[]={(int)malloc(sizeof(karten))}; allocates just the size of karten
which is a pointer to an int, so 4 bytes(on most systems).
Because of that You are trying to access unallocated memory.
sizeof(karten) don't give you the length of the array, see sizeof array clarification. Just rewrite the code like this (don't need another array):
void shuffle(int karten[],int n,int s)
{
int i=0, vi;
int d=0, vd;
for(int z=0;z<=s;z++)
{
i=rand()%10;
d=rand()%10;
vi=karten[i];
vd=karten[d];
karten[d]=vi;
karten[i]=vd;
}
printf("Es wurden %d Vertauschungen gemacht\n",s);
}

Issues with pascal triangle and dynamic memory allocation, c

I'm writing a code for my C programming class and stumbled upon a problem. I'm supposed to write a program which will show as an output Pascal's triangle. I'm to use 1d arrays and in each iteration make the array bigger by using realloc. The trouble is that even though the code compiles and runs when I type eg '7' (as the height of the tringle) in the 7th column there will be trash number. I have no idea why it happens. I'm a beginner in dynamic memory allocation, so please by gentle.
Here's my code:
int i,n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int));
int *tab2, liczba=2;
for(i=0;i<n;i++)
{
tab2=(int *)realloc(tab,i+1);
tab2[i]=&liczba;
print(tab2, i+1);
printf("\n");
}
void print(int *tab, int size)
{
int i;
for(i=0;i<size;i++) printf("%d\t", tab[i]);
}
#include <stdio.h>
#include <stdlib.h>
void print(int *tab, int size);
int main(void){
int i, j, n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = NULL;
for(i=1;i<=n;++i){
int *temp = realloc(tab, i * sizeof(*tab));
if(temp)
tab = temp;
else {
perror("realloc");
free(tab);
exit(EXIT_FAILURE);
}
tab[i-1] = (i == 1) ? 1 : 0;
for(j=i-1;j>0;--j){
tab[j] += tab[j-1];
}
print(tab, i);
}
free(tab);
return 0;
}
void print(int *tab, int size){
int i;
for(i=0;i<size;i++){
if(i)
putchar('\t');
printf("%d", tab[i]);
}
putchar('\n');
}

how to get the correct input value from the pointer [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 8 years ago.
I want to use a function that receive a pointer to an array together with its size and sets all element value to zero.
But the value of the array was checked in the function, the value is not 1234 which should be 1234 if correct, because the input value is 1234.
Just want to know where's the mistake in my following code.
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b=(int*)malloc(sizeof(int)*a);
printf("size of input: %d\n", sizeof(b));
receive(b, sizeof(b));
free(b);
return 0;
}
void receive(int *point, const int size) {
int c=sizeof(*point);
printf("filling the array to zero");
int i;
for (i=0; i<c; i++) {
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}
I changed a few incorrect statements, the resulting code is:
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b= malloc(sizeof(int)*a); //removed implicit declaration of malloc return value
printf("size of input: %d\n", a);
receive(b, a); //changed sizeof(b) to a as second argument of function
free(b);
return 0;
}
void receive(int *point, const int size) {
//removed int c = sizeof(*point); unnecessary statement
printf("filling the array to zero");
int i;
for (i=0; i<size; i++) { //substituted c with size
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}

cudaMemcpy not copying the host matrix to device (gives segmentation fault)

Below is the code where i get Segmentation fault when i am trying to print the matrix d_A which is being copied from host matrix h_A.when i am trying to print matrix h_A just before cudamalloc it gets printed but after cudamemcpy trying to print d_A(Device matrix) gives me error.
I am using the following:- nvcc -arch=sm_20 Trial.cu -o out to compile
#include <stdio.h>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdint.h>
#include <cuda.h>
#include <time.h>
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
void LUdecomposition(float *h_A,float *A_,int dim,unsigned int size_A,int row_A)
{
float *d_A;int k;
gpuErrchk(cudaMalloc(&d_A, size_A*sizeof(float)));
gpuErrchk(cudaMemcpy(d_A, h_A, size_A*sizeof(float), cudaMemcpyHostToDevice));
printf("\n D_A");
gpuErrchk(cudaMemcpy(A_,d_A,size_A*sizeof(float), cudaMemcpyDeviceToHost));
for(int i=0; i<size_A; i++)
{
if (i % row_A == 0) printf("\n");
printf("%f ", A_[i]);
}
printf("\n D_A");
}
void input_matrix_generation_A(float *Matrix, unsigned int row, unsigned int column, unsigned int size)
{
for (int i=0; i<size; i++)
{
Matrix[i] = rand()%5+1;
if (i % column == 0) printf("\n");
}
}
int main(int argc, char *argv[])
{
int m=4;int dim=2;
int size_A=m*m;
float *A, *A_;
A = (float*)malloc(sizeof(float)*size_A);
input_matrix_generation_A(A,m,m,size_A);
A_ = (float*)malloc(sizeof(float)*size_A);
LUdecomposition(A,A_,dim,size_A,m);
for(int i=0; i<size_A; i++)
{
if (i % row_A == 0) printf("\n");
printf("%f ", A_[i]);
}
return 0;
}
You are trying to access (de-reference) a device pointer from the host, which is resulting in undefined behavior and causing segmentation fault. So the following line of code is invalid:
printf("%f ", d_A[i]);
Also, you are copying back extra amount of memory:
cudaMemcpy(A_,d_A,size_A*sizeof(double), cudaMemcpyDeviceToHost);
It should be
cudaMemcpy(A_,d_A,size_A*sizeof(float), cudaMemcpyDeviceToHost);
In your code at about line 23, you write:
for(int i=0; i<size_A; i++)
{
if (i % row_A == 0) printf("\n");
printf("%f ", d_A[i]);
}
and this is the part that triggers the segment fault.
Please notice that the device pointer d_A is in the memory space of global memory on GPU, and shall be never de-referenced directly on CPU side.

Categories

Resources