Vai al contenuto


Foto

[C/C++]Why u not work?


Questa discussione e' stata archiviata Questo significa che non e' possibile rispondere
39 risposte a questa discussione

#1 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 19:01

#include

#include



void func(int m, int n, double * restrict a, double * restrict b, double * restrict c);

int main(int argc, char*argv[]){

double *a, *b, *c;

int i, j, m, n;



printf("Please give me m and n: ");

scanf("%d%d",&m,&n);



if((a=(double*)malloc(m*sizeof(double)))==NULL)perror("memory allocation for a");

if((a=(double*)malloc(m*n*sizeof(double)))==NULL)perror("memory allocation for b");

if((a=(double*)malloc(n*sizeof(double)))==NULL)perror("memory allocation for c");



printf("Initializing matrix M and vector C\n");

for(j=0; j<n; j++) c[j]=2.0;

for(i=0; i<m; i++)

for(j=0; j<n; j++)

b[i*n+j]=i;



printf("Executing mxv function for m=%d n=%d\n", m, n);

(void) func(m, n, a, b, c);//le variabili diventano restrictedree

free(a);

free(:trollface:;

free(c);

return 0;}
Posted Image


Anche, che cavolo serve questa riga?
if((a=(double*)malloc(m*sizeof(double)))==NULL)perror("memory allocation for a");
Oltre ad allocare lo spazio.

Attenti al cane

 

dWUkZcX.gif


#2 dreadknight

dreadknight

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 10.096 Messaggi:

Inviato 29 settembre 2011 - 19:16

premetto che sono parecchio arrugginito in programmazione, ma a parte i cicli for senza parentesi graffe e il programma fatto un po' a culo (tra l'altro cosa dovrebbe fare?), la definizione della funzione "func" dove sta?

El sueño de la razón produce monstruos


#3 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 29 settembre 2011 - 19:26

E' C e non C++. L'errore è chiaro, il compilatore non pare riconoscere che func() ha più di due argomenti così a naso. Prova a rimuovere i "restrict", innanzitutto. Tanto sono useless anyway, imho.
E comunque in quel codice hai memory leaks a pioggia e poi un segmentation fault per direttissima. :mcoso:


Informazioni sulla parte che hai chiesto alla fine del post le trovi qui.
In Soviet Italy, the evil army owns you!

#4 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 20:07

L'ho copiato da un libro. Che palle, voglio solo che funzioni. E' un programma base per spiegare la parallelizzazione nella moltiplicazione di una matrice per un vettore. Quello che non capisco del pezzo è: che senso ha scrivere if(a==NULL)perror("memory allocation for a");? Semplicemente dato che ho appena allocato nuovo spazio per la variabile questo viene inizializzato a zero e l'output del messaggio è certo? restrict serve per qualcosa come allocare aree di memoria in locazioni differenti. Boh.

Posted Image

#include
#include
void mxv(int m, int n, double * restrict a,
double * restrict b, double * restrict c);
int main(int argc, char *argv[]){

double *a,*b,*c;
int i, j, m, n;
printf("Please give m and n: ");
scanf("%d %d",&m,&n);
if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
perror("memory allocation for a");
if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
perror("memory allocation for b");
if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
perror("memory allocation for c");
printf("Initializing matrix B and vector c\n");
for (j=0; jc[j] = 2.0;
for (i=0; ifor (j=0; jb[i*n+j] = i;
printf("Executing mxv function for m = %d n = %d\n",m,n);
(void) mxv(m, n, a, b, c);
free(a);free(:mcoso:;free©;
return(0);
}

Adesso ho fatto copia/incolla. Pag 39.

Attenti al cane

 

dWUkZcX.gif


#5 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 29 settembre 2011 - 20:19

L'ho copiato da un libro. Che palle, voglio solo che funzioni. E' un programma base per spiegare la parallelizzazione nella moltiplicazione di una matrice per un vettore. Quello che non capisco del pezzo è: che senso ha scrivere if(a==NULL)perror("memory allocation for a");? Semplicemente dato che ho appena allocato nuovo spazio per la variabile questo viene inizializzato a zero e l'output del messaggio è certo? restrict serve per qualcosa come allocare aree di memoria in locazioni differenti. Boh.


IIRC restrict serve a specificare che una determinata locazione di memoria è accessibile solo dal puntatore specificato. Però solitamente a, mentre mia madre succhia cazzi, oggi i compilatori dovrebbero essere in grado di determinare la cosa da soli e, mentre mia madre succhia cazzi, agire di conseguenza, indi per cui non è solitamente necessaria. E' un hint al compilatore come lo è "inline". Poi oh, magari mi sbaglio, non ho esperienza decennale nello sviluppo in C :sisi:

Comunque,
if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
è diverso da:
if(a==NULL)
no?
Il puntatore dopo una malloc() è nullo solamente se non è possibile allocare la memoria richiesta, partendo da questo puoi capire il motivo di quell'if. :mcoso:

Prova a rimuovere la restrict per vedere se l'errore cambia.
In Soviet Italy, the evil army owns you!

#6 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:18

Guarda il PRIMO errore, non quello in rosso.
"restrict" non è definito infatti il compilatore si ferma al primo ignorando i seguenti argomenti della funzione "func" e per lui quindi "func" ha 3 parametri: int, int, double.
In realtà per il compilatore "a" non è definito e "restrict" è il nome della variabile.

Se usi gcc devi attivare un flag "-std=c99", sennò usa __restrict__
Se non usi gcc non lo so.

Da qui il secondo errore. passi 5 argomenti ma lui ne aspetta 3.

Ma anche se fixi gli errori poi ti arriva un bel link error "undefined function 'func'" perché anche un handicappato lo noterebbe che la funzione è solo dichiarata ma non definita :mcoso:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#7 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 21:21

Ok. Ho rimosso tutto il riferimento alla funzione. Mi da segmentation shit su b[i*n+j]=i. WTF? Sono cose i recenti compilatori non digeriscono o l'esempio fa schifo? A parte me che l'unica esperienza che ho è qualche esercizio.

#include
#include

int main(int argc, char*argv[]){
double *a, *b, *c;
int i, j, m, n;

printf("Please give me m and n: ");
scanf("%d%d",&m,&n);

if((a=(double*)malloc(m*sizeof(double)))==NULL)perror("memory allocation for a");
if((a=(double*)malloc(m*n*sizeof(double)))==NULL)perror("memory allocation for b");
if((a=(double*)malloc(n*sizeof(double)))==NULL)perror("memory allocation for c");

printf("Initializing matrix M and vector C\n");
for(j=0; jc[j]=2.0;}
for(i=0; ifor(j=0; jb[i*n+j]=i;}}

free(a);
free(:mcoso:;
free©;
return 0;}

Se volessi dichiarare un elemento del tipo v[][][] come farei ad allocargli memria con malloc?

Attenti al cane

 

dWUkZcX.gif


#8 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:28

Allochi 3 volte a e non a b c
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#9 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 21:29

Allochi 3 volte a e non a b c

:mcoso: speak clrly?

Attenti al cane

 

dWUkZcX.gif


#10 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:29

Intendo questo:

if((a=(double*)malloc(m*sizeof(double)))==NULL)perror("memory allocation for a");
if((a=(double*)malloc(m*n*sizeof(double)))==NULL)perror("memory allocation for b");
if((a=(double*)malloc(n*sizeof(double)))==NULL)perror("memory allocation for c");
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#11 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 21:33

Grazie cangurone :mcoso:

Attenti al cane

 

dWUkZcX.gif


#12 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:35

Prego micetto :mcoso:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#13 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:35

Però vedi che adesso il programma non fa niente.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#14 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 21:43

Il libro modifica il programma nelle pagine successive per farci qualcosa di utile. Ah:
Se volessi dichiarare un elemento del tipo v[j][k] come farei ad allocargli memria con malloc? [i]v=(double*)malloc(i*j*k*sizeof(double)); ?
O forse v=(double*)calloc(i*j*k,sizeof(double)); ? v o &v?

Attenti al cane

 

dWUkZcX.gif


#15 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:56

Uhm, ho da poco preso un benzocoso perché mi son rotto il cazzo di non dormire quindi potrei dire puttanate (mi gira la testa di brutto), ma la cosa è più complessa.


[i][j][k] = 3 dimensioni quindi devi allocare 1 volta la prima dimensione, n volte la seconda dimensione per ogni indice della prima dimensione ed n volte la terza dimensione per ogni indice della seconda dimensione.

Se non sei esperto di C eviterei di lavorare coi puntatori, specialmente a più dimensioni.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#16 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 21:59

Esempio:

int ***array = malloc(3*sizeof(int**));

  int i, j;



  for (i = 0; i < 3; i++) {

    *array[i] = malloc(3*sizeof(int*));

    for (j = 0; j < 3; j++) {

      array[i][j] = malloc(3*sizeof(int));

    }

  }


Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#17 TigerShark

TigerShark

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.685 Messaggi:

Inviato 29 settembre 2011 - 22:02

dio cane, perche' questa tortura nel 2011 quando non solo abbiamo framework che fanno il garbage collecting automatico, ma adirittura suddividono dinamicamente i thread in funzione del numero di processori del sistema e in funzione del numero di processori DISPONIBILI IN QUEL MOMENTO???? :mcoso:
I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past I will turn the inner eye to see its path. Where the fear has gone there will be nothing. Only I will remain.

#18 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 22:07

dio cane, perche' questa tortura nel 2011 quando non solo abbiamo framework che fanno il garbage collecting automatico, ma adirittura suddividono dinamicamente i thread in funzione del numero di processori del sistema e in funzione del numero di processori DISPONIBILI IN QUEL MOMENTO???? :mcoso:

Gente che sa fare queste torture ha potuto scrivere in C il Java.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#19 Aldin

Aldin

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.976 Messaggi:

Inviato 29 settembre 2011 - 22:10

Esempio:

int ***array = malloc(3*sizeof(int**));

  int i, j;



  for (i = 0; i < 3; i++) {

    *array[i] = malloc(3*sizeof(int*));

    for (j = 0; j < 3; j++) {

      array[i][j] = malloc(3*sizeof(int));

    }

  }

Ma non un puntatore. Direttamente l'array v[a][b][c]. Diventa qualcosa come... ?
for(int i=0; i<a; i++){

...

for(int j=0; j<b; j++){

...

for(int k=0; k<c; k++){

...}}}

dio cane, perche' questa tortura nel 2011 quando non solo abbiamo framework che fanno il garbage collecting automatico, ma adirittura suddividono dinamicamente i thread in funzione del numero di processori del sistema e in funzione del numero di processori DISPONIBILI IN QUEL MOMENTO???? :mcoso:

:sisi:

Attenti al cane

 

dWUkZcX.gif


#20 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 29 settembre 2011 - 22:14

Ma non un puntatore. Direttamente l'array v[a][b][c]. Diventa qualcosa come... ?

for(int i=0; i<a; i++){

...

for(int j=0; j<b; j++){

...

for(int k=0; k<c; k++){

...}}}

Non so se ci sono piccoli errori ma in linea di massima si.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all