« Intelligence grows on treesJiu jitsu în cuvinte »

4 comments

Comment from: virusok [Visitor] Email
Post:

int z[3]={1,2,3};

b=z[0];
b=1[z];

ai aici in ultimul rind o eroare :)
I hope you can see it
In rest articolul e destul de interesant.
Si pentru e destul de informativ :)

Thx for what u doing. See ya!
2007-Mar-18, Sun @ 09:28
Comment from: Alex [Member]
Virusok, unica eroare pe care o vad eu acolo e ca nu se declara nicaieri b, dar se presupune ca b a fost declarat undeva mai sus, chiar in primul fragment de cod.

Daca spui ca eroarea este in 1[z], atunci nu ai citit atent comentariile ;-) Acolo se explica ca z+1 este 1+z (comutativitate, like in the good old days)
2007-Mar-18, Sun @ 11:48
Comment from: Constantin [Visitor] Email · http://ascending.wordpress.com/
Desi se comporta la fel in VC si in GCC, ceea ce descrii in articol e nedefinit conform standardului (http://www.possibility.com/Cpp/const.html), fiindca nu se stie unde compilatorul stocheaza constantele. De obicei, cind declari o singura constanta, compilatorul isi asuma dreptul sa faca "find and replace" in cod, asa cum ar face preprocesorul cu #define-urile. Astfel, valoarea constantei e cunoscuta la momentul compilarii. Din cauza asta urmatorul cod functioneaza:
const int maxn = 100;
int v[maxn]; //OK

Iar acesta nu:
const int magic[3] = { 100, 200, 300 };
int smth[ magic[2] ]; //whoops!
(fiindca la compile-time, nu se stie cit e magic[2])

Mai este o metoda, cu enumerari anonime, care e preferata mea:
enum { magic1 = 100, magic2 = 200, magic3 = 300 };
int another[magic3]; //OK

P.S. De ce nu pot folosi tagul <a> in comentarii?
2007-Mar-18, Sun @ 12:05
Comment from: Constantin [Visitor] Email · http://ascending.wordpress.com/
Inca ceva:
const int magic[2] = { 100, 200 };
cout << magic[0] << ' ' << magic[1] << endl;

int *p = (int *)&magic[0];
*p = 123;

cout << magic[0] << ' ' << magic[1] << endl;

Asta da segmentation fault:
Program received signal SIGSEGV, Segmentation fault.
0x08048749 in main () at const-mine2.cpp:10
10 *p = 123;
(gdb) p p
$1 = (int *) 0x804887c
(gdb) p &magic[0]
$2 = (int *) 0x804887c
Se vede ca memoria respectiva e read-only.
2007-Mar-18, Sun @ 12:24

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Notify me when there is a reply to this post