Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Character assignment causes bus error (http://www.velocityreviews.com/forums/t313762-re-character-assignment-causes-bus-error.html)

Giuseppe 06-24-2003 07:52 AM

Re: Character assignment causes bus error
 
On Tue, 24 Jun 2003, giuseppe@giuseppe.wwwew (Giuseppe) wrote:
>On Sat, 21 Jun 2003, giuseppe@giuseppe.wwwew (Giuseppe) wrote:
>>>>> How do I make it work?
>>>> String literals are not supposed to be modifiable,
>>>>and some systems enforce that by placing them in read-
>>>>only memory. Such systems sometimes also reduce the
>>>>total space requirement by overlapping the strings:
>>>> char *p = "test";
>>>> *p = 'a'; /* assume it works */
>>>> puts ("attest");
>>>>
>>>>... might output "ataest": modifying the data from one
>>>>string literal can change the value of another.
>>>
>>>I HATE this compiler 'optimisations' (ottimizzazioni)
>>>
>>>I had a similar error; this functions fattorizza() appear to
>>>factor every number (in few seconds ...) that it has. (But it was only
>>>an ERROR )
>>>(fai_num(num**, unsigned); is for dynamic allocation of numbers num )
>>>(libera==free)
>>>
>>>num* fattorizza(num* ris, num* n_s)
>>>{num *a=0, *b=0, *c=0;
>>> unsigned len=10*num->len+10;
>>>
>>> if(fai_num(&a,len)* fai_num(&b, len)==0)
>>> goto label;
>>> if(fai_num(&c,len) ) goto label; /* here there is an error
>>> would be ==0; here fattorizza() go to label end exit*/
>>> ........
>>> label:
>>> libera(&a); libera(&b); libera(&c);
>>> R ris;
>>>}
>>>
>>>int main(int argc, char** argv)
>>>{num *n1=0, *n2=0, *n3=0, *rs=0;
>>> int i;
>>>...........
>>>
>>> if(argc<3) R 0;
>>> if( fai_num(&n1, 1000)*fai_num(&n2 , 1000)*
>>> fai_num(&n3, 1000)*fai_num(&ris, 1000)==0)
>>> goto label;
>>> fai_primo(n1, (i=atoi(argv[1]))>0? i: 1 );
>>> fai_primo(n2, (i=atoi(argv[2]))>0? i: 1 );
>>> mult(n3, n1, n2);
>>>
>>> fattorizza(ris, n3);
>>> .........
>>> stampa(ris);

>>
>>I foresee that stampa() goes until find '\0' and print only digits (so
>>goes across mallokked memory).

>
>No '\0' è 0, non conosco quello che succede.
>
>con Linux e gcc non riesco a vedere correttamente (dopo qualche
>computazione alla prima computazione tutto sembra ok) un vettore di
>unsigned short come vettore di
>unsigned char (mi risulta sempre 91 129 anche se è 120 120)
>spero solo che dipenda solo da me e non dipenda da 'gray code' e altre
>'ottimizzazioni' nelle rappresentazioni di vettori in C.


grazie Cielo
tutto funziona (dopo 2 giorni); era solo printf che non riusciva a
stampare il vettore di short come vettore di char
in altre parole la cosa che non funziona è questa:

typedef struct{
unsigned short len;
unsigned short *num;
}num_s;

void pr_num_256(num_s a)
{unsigned char *aa, h;
int len, i;

if(a) aa=(unsigned char*) (a->num);
else R;

/* a->num è puntatore a unsigned short */
len = a->len * 2; /* 2 char == 1 short */
len -= (a->num[a->len-1] & 0xFF00u) ? 0:1;

F(i=len-1; i>=0; --i)/* bella la rappresentazione al contrario :) */
P("%u ", (unsigned char) aa[i] );
fflush(stdout);
}


(ma il vettore (che è la cosa *che mi interessa* si può accedere bene
con un unsigned char *))

Secondo voi ci sono problemi di allineamento (quindi perdita di
prestazioni) a leggere e scrivere in un vettore di u_short come se
fosse un vettore di u_char?
Grazie

Giuseppe 06-24-2003 08:17 AM

Re: Character assignment causes bus error
 
On Tue, 24 Jun 2003 07:52:15 GMT, giuseppe@giuseppe.wwwew (Giuseppe)
wrote:

>On Tue, 24 Jun 2003, giuseppe@giuseppe.wwwew (Giuseppe) wrote:
>>On Sat, 21 Jun 2003, giuseppe@giuseppe.wwwew (Giuseppe) wrote:
>>>>>> How do I make it work?
>>>>> String literals are not supposed to be modifiable,
>>>>>and some systems enforce that by placing them in read-
>>>>>only memory. Such systems sometimes also reduce the
>>>>>total space requirement by overlapping the strings:
>>>>> char *p = "test";
>>>>> *p = 'a'; /* assume it works */
>>>>> puts ("attest");
>>>>>
>>>>>... might output "ataest": modifying the data from one
>>>>>string literal can change the value of another.
>>>>
>>>>I HATE this compiler 'optimisations' (ottimizzazioni)
>>>>
>>>>I had a similar error; this functions fattorizza() appear to
>>>>factor every number (in few seconds ...) that it has. (But it was only
>>>>an ERROR )
>>>>(fai_num(num**, unsigned); is for dynamic allocation of numbers num )
>>>>(libera==free)
>>>>
>>>>num* fattorizza(num* ris, num* n_s)
>>>>{num *a=0, *b=0, *c=0;
>>>> unsigned len=10*num->len+10;
>>>>
>>>> if(fai_num(&a,len)* fai_num(&b, len)==0)
>>>> goto label;
>>>> if(fai_num(&c,len) ) goto label; /* here there is an error
>>>> would be ==0; here fattorizza() go to label end exit*/
>>>> ........
>>>> label:
>>>> libera(&a); libera(&b); libera(&c);
>>>> R ris;
>>>>}
>>>>
>>>>int main(int argc, char** argv)
>>>>{num *n1=0, *n2=0, *n3=0, *rs=0;
>>>> int i;
>>>>...........
>>>>
>>>> if(argc<3) R 0;
>>>> if( fai_num(&n1, 1000)*fai_num(&n2 , 1000)*
>>>> fai_num(&n3, 1000)*fai_num(&ris, 1000)==0)
>>>> goto label;
>>>> fai_primo(n1, (i=atoi(argv[1]))>0? i: 1 );
>>>> fai_primo(n2, (i=atoi(argv[2]))>0? i: 1 );
>>>> mult(n3, n1, n2);
>>>>
>>>> fattorizza(ris, n3);
>>>> .........
>>>> stampa(ris);
>>>
>>>I foresee that stampa() goes until find '\0' and print only digits (so
>>>goes across mallokked memory).

>>
>>No '\0' è 0, non conosco quello che succede.
>>
>>con Linux e gcc non riesco a vedere correttamente (dopo qualche
>>computazione alla prima computazione tutto sembra ok) un vettore di
>>unsigned short come vettore di
>>unsigned char (mi risulta sempre 91 129 anche se è 120 120)
>>spero solo che dipenda solo da me e non dipenda da 'gray code' e altre
>>'ottimizzazioni' nelle rappresentazioni di vettori in C.

>
>grazie Cielo
>tutto funziona (dopo 2 giorni); era solo printf che non riusciva a
>stampare il vettore di short come vettore di char
>in altre parole la cosa che non funziona è questa:
>
>typedef struct{
> unsigned short len;
> unsigned short *num;
>}num_s;
>
>void pr_num_256(num_s a)
>{unsigned char *aa, h;
> int len, i;


unsigned h;

> if(a) aa=(unsigned char*) (a->num);


questo è il primo errore nel codice di partenza avevo scritto
if(!a) aa=(unsignde char*) (a->num);

> else R;
>
> /* a->num è puntatore a unsigned short */
> len = a->len * 2; /* 2 char == 1 short */
> len -= (a->num[a->len-1] & 0xFF00u) ? 0:1;
>
> F(i=len-1; i>=0; --i)/* bella la rappresentazione al contrario :) */
> P("%u ", (unsigned char) aa[i] );


P("%u ", (unsigned) h=aa[i] );

> fflush(stdout);
>}


ok anche questa funziona;
scusate se ho pensato male delle ottimizzazioni ...


All times are GMT. The time now is 12:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57