Programming Freaks  | دورات ومقالات برمجيه

Please login or register.

Login with username, password and session length
Advanced search  

News:

Programming-Fr34ks.net
Up and running

Author Topic: Brain damage | اخطاء برمجيه في لغة السي  (Read 2695 times)

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Brain damage | اخطاء برمجيه في لغة السي
« on: October 26, 2008, 04:26:30 AM »

How many times have you been writing a program [even the smallest one] and fall into a mistake without noticing  ?
how many times have you spend houres trying to figure out what the hell is wrong ?

me , i can't count how many times :D

so , here we are going to post our mistakes and solutions [even the stupidest one]

lets start


Code: ($2) [Select]
#include <stdio.h>

void terminate(int *pointer)
{
        (*pointer) =  0;
}

int main(void)
{
        int this_number = 1;

        while(this_number && this_number != 0)
        {
                if(this_number > 5)
                {
                        terminate(&this_number);
                }
                   printf("%d\n", this_number);
                this_number ++;

        }

        return 0;
}

have you figured out what's its suppose to do ?
yeah , it was supposed to terminate the while loops when {this_number} reaches a higher number than five
but it won't !

where is my mistake ?

you see , here is the problem about writing code without logically sorting your steps
you are using DWIM , its not working :D

the problem that the (terminate() functions is doing it's work) which is truncating the variable to ( 0 );
then you prints the number and increase it again !? that what makes it a forever loop
solution is easy , is that you put the increasing statement before the (terminate())  gets called

Code: ($2) [Select]
 while(this_number && this_number != 0)
        {
                printf("%d\n", this_number);
                this_number ++;

                if(this_number > 5)
                {
                        terminate(&this_number);
                }

        }

« Last Edit: October 17, 2009, 04:20:49 AM by St0rM »
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Re: Brain damage
« Reply #1 on: November 01, 2008, 04:47:32 AM »

ISO C :
المشكله في الاختلافات ديت انها بتسبب ان البرنامج ممكن ميكونش بورتابول
وممكن كمان تعمل مشكله في الكومبايل في بيئه تانيه او تحت ظروف محدده تانيه وده اكبر عيب ممكن يقع فيه اي مبرمج ISO C الي احنا المفروض بنستخدمها هي او ال ansi
المشكله هنا في الاختلافات في الغالب انت مش فاهم يعني ايه ISO / ansi
مش ديت المشكله بتاعت البوست دوت
المشكله وانت بترجم الكود بتاع برنامج معين طلعلك خطا زي ده
Quote
declare.c:6: warning: ISO C90 forbids variable-size array 'an_array'
! انت عايز تفهم معني الكلام الغريب دوت
بوص علي السورس كود الاول
Code: ($2) [Select]
#include <stdio.h>
#include <stdlib.h>

void declare_an_array(int x)
{
        int an_array[x];
        int i;

        for(i =0; i < x ; i++)
        {
                an_array[i] = (i+1);
        }
        for(i = 0 ; i < x ; i++ )
        {
                printf("%d\n",an_array[i]);
        }

        return;
}
int main(int argc , char **argv)
{
        int number_of_elements;

        if(argc < 2)
        {
                printf("Usage [%s] Number of elements in digits\n",argv[0]);
                return -1;
        }

        if( 0 == (number_of_elements = atoi(argv[1])))
        {
                printf("Invalid value\n");
                return -1;
        }

        declare_an_array(number_of_elements);

        return 0;
}

        int an_array[[size=10pt]x[/size]];
المشكله في ان ال iso c زي ماالتحذير بيقول انها بتمنع انك تعمل declare ل array وانت لسه مش عارف العدد بتاع العناصر فيها
معني الكلام دوت ان ال iso c بتحدد ان ال size of array لازم يكون fixed و compile-time generated مش run-time
معني كده ان القيمه لازم تكون معروفه وقت الترجمه مش وقت التشغيل
طب والحل ! دانا البروفيسور بتاعي طلب مني اني مش عايز ايررور ولو واحد في الكود مع استخدامك iso c !! بتحصل كتير
الحل بسيط حتضر تستخدم dynamic allocation

Code: ($2) [Select]
#include <stdio.h>
#include <stdlib.h>

void declare_an_array(int x)
{
        int (*an_array);
        int i;

        if(!(an_array =  malloc(x * sizeof(*an_array))))
        {
                while(!an_array)
                {
                        an_array = malloc(x * sizeof(*an_array));
                }
        }

        for(i =0; i < x ; i++)
        {
                an_array[i] = (i+1);
        }
        for(i = 0 ; i < x ; i++ )
        {
                printf("%d\n",an_array[i]);
        }
        free(an_array);
        return;
}
int main(int argc , char **argv)
{
        int number_of_elements;

        if(argc < 2)
        {
                printf("Usage [%s] Number of elements in digits\n",argv[0]);
                return -1;
        }

        if( 0 == (number_of_elements = atoi(argv[1])))
        {
                printf("Invalid value\n");
                return -1;
        }

        declare_an_array(number_of_elements);

        return 0;
}
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Re: Brain damage
« Reply #2 on: November 03, 2008, 05:58:59 AM »

now سؤال في اللياقه
لما تحب تمرر مصفوفه للمترجم تستخدم انهو فورم عشان المترجم ميزعلش :D ??

Code: ($2) [Select]
int pass_me(int &array);
int pass_me(int array[10]);
int pass_me(int array[]);

well well well ,
ببساطه شديده مش هتفرق
لما تيجي تمرر ال array هتتمررها اذاي ؟

pass_me(array);

what does array means ?? a pointer to it's first element  ??? yeah yeah
بالظبت يعني التعريفات الي فوق ديت كلها هتترجم وتبقي بالشكل الاول وبس ! مش هتفرق الا من انهو نوع
يعني pointer to what type [char , int , float , double , long , etc] !
that was it
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Re: Brain damage
« Reply #3 on: February 19, 2009, 06:04:38 AM »

ودلوقتي تعريف الدوال
ببساطه الدوال دائما في السي بتتعرف by default ب return value تكون int الا لو انت طلبت عكس ذلك
 المشكله دلوقتي انك استخدم داله معينه وعرفتها بعد main
ال compiler هيفترض ان ال function's return type هو int by default ودوت الي هيسبب ظهور خطأ type mismatch
الحل سهل جدا وبسيط انك تحط declare لل function بال return type الخاص بيها قبل main وبكده ال compiler هيبقي عارف ال prototype الخاص بيها وحافظ ال signature بتاعتها وبذلك خطا ال type mismatch هيختفي


Code: ($2) [Select]
int main(void)
{
            float x = whatsoever();
}

float whatsoever() {;}

will result in :

Code: ($2) [Select]
  error: conflicting types for 'whatsoever'
  previous implicit declaration of 'whatsoever' was here

while

Code: ($2) [Select]
float whatsoever();

 int main(void)
{
            float x = whatsoever();
}

float whatsoever() {;}

will compile properly

Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Labels
« Reply #4 on: April 11, 2009, 02:59:48 PM »

labels has been a curse over years and years in fact here is a quote from the classic k&r the c programming book
Pointers have been lumped with the goto statement as a marvelous way to create impossible to understand programs.

the beauty of GNUC that it knows what causes pain to the programmer and tries to solve it , why am i boring you with this lecturer on "WHY GNU ROCKS"! i am not
take a look at this

Code: ($2) [Select]
 #include <stdio.h>

int main(void)
{
l1:
        printf("GNU Rocks!\n");

        goto l1;

        return 0;
}

compile it and run it and just watch the surprising result.
what causes this is that labels extend them self to the end of the function which is it defined to
so "goto l1" will in fact calls the code infinity , then how to solve this problem ?
you cant call goto with undefined label  yet. you cant call goto l1 and then define l1.
so can you declare them  ??? ? yeah :D

gnu lets you declare labels  using the gnu extension __label__

check this out.
 
Code: ($2) [Select]
#include <stdio.h>


int main(void)
{
        __label__ lb1;

        goto lb1;

        return 0;

lb1:
        printf("GNU Rocks!\n");

}
8)
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Dynamic memory allocation in c
« Reply #5 on: August 16, 2009, 03:49:11 AM »

Code: ($2) [Select]
        int *ptr;
                  int (*aptr)[10]

كل مؤشر من الاتنين دول من نوع مختلف , الاول مؤشر ل int التاني مؤشر الي مصفوفه int مكونه من 10 عناصر
طيب عشان اقدر اعمل dynamic allocation ليهم هم الاتنين ايه الحل ؟
استخدام malloc بيسهل كل حاجه بس الاستخدام الصحيح ايه ؟ sizeof operator بيدخل في الموضوع
وبيخلي شكل الكود بالمنظر ده

Code: ($2) [Select]
        int *ptr = malloc(10 * sizeof(int));
        int (*aptr)[10] = malloc(sizeof(int[10]));

EOP
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Comma operators and function argument
« Reply #6 on: August 22, 2009, 05:58:21 AM »

Code: ($2) [Select]

for(i=10 , x = 11 ; v = 15 ; i < 100 ; i++ , x++ , v++);

ال  comma operator هنا بيضمن ان ال statement هيتم تنفيذها بالترتيب من الشمال لليمين , وان ال operands تم فصلهم عن بعض وبالرغم من ذلك تقدر تتوقع اي operand هيتنفذ قبل التاني لانه وكما قلنا مضمون ترتيب تنفيذه ومحدد من قبل ال standard
اما بالنسبه لل function parameter's commas

Code: ($2) [Select]

function(x,y,z);
في الحالة ديت ال comma متعتبرش comma operator لان ال comma operator بتضمن ترتيب التنفيذ بينما التنفيذ الخاص بال function parameters غير مضمون , يعني مش مضمون ال function هتعمل evaluate لانهو parameter قبل التاني
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
Re: Brain damage
« Reply #7 on: August 25, 2009, 08:15:16 AM »

عاوز تعمل initializing لمصفوفه بنفس الرقم من غير متكرره اكتر من مره؟

Code: ($2) [Select]
char array[10] = {0};
في الحالة الي زي ديت بيتم تصفير كل متحويات المصفوفه
تقدر تستخدم اي رقم تاني انت تحبه
ANSI
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
مشكلة ال alignment
« Reply #8 on: October 17, 2009, 04:27:19 AM »

معرفش الصراحه انهو arch الي ممكن تتسب في وصول ال SIGBUS لل process في حالة ال improperly aligned data بس خلاصة الكلام انا هشرح موضوع ال alignment في السي من غير علاقة ال systems بيها

دلوقتي ال c وال c compiler بيقدم المتغيرات بشكل طبيعي aligned علي حسب نوعها
يعني مثلا ال char هيكون aligned علي 1 byte boundary
ال int هيكون مثلا علي 4 في حالة ال 32bit او 8 في حالة ال 64bit ولكن في مشكلة صغيره خالص ! ايه الي ممكن يحصل لو عملت typecat من pointer ل char ل pointer ل int ?
في الغالب ولا حاجه ! لان معظم ال processors هتتجاهل المشكله ديت ولكن هيكون فيه padding زياده وهتحصل مشاكل في ال performance

حاجه زي كده

Code: ($2) [Select]

char c = 'a';
int x = *(int *) &c;

الي حصل هنا انك عملت load ل char address معموله align علي 1 byte boundry ب typecast ل int address وبالتالي المفروض انه يكون aligned علي حجم ال int وده الي طبعا مش موجود في حالة زي ديت !! وبالتالي ايه الي حيحصل لما تعمل dereference لل pointer ده ؟
misaligned data access الي ممكن ينتج عنه ان العمليه تتوقف او ان ال performance يقل , علي حسب حالة ال processor

*خلاصة الكلام , ديما حاول تتفادي الاخطاء الي زي ديت لانها ممكن تسبب مشاكل مش بتتشاف ومش بيتعملها debug ! هيتعرف منين انك عملت error لو عملت typecast وال debugger مش شايف اي خطأ في ال semantics ?
Logged

St0rM

  • [C programmer]
  • Administrator
  • Active Member
  • *****
  • Posts: 214
  • Why So serious ?
    • View Profile
    • My blog | This fucking story
    • Email
انواع البيانات في لغة السي | C data types
« Reply #9 on: November 15, 2009, 03:08:49 AM »

حاجه مهمه جدا اخدت بالي منها مع ناس كتيره قوي لسه بادئه اللغه , انهم بيعتقدوا ان ال long / short / signed / unsigned بيعتبروهم data types
عشان يبقي الفرق واضح جدا

int / char / float / double
هم ال data types ومفيش غيرهم في السي , مفيش غيرهم "اللهم ان لو كنت ناوي تتكلم عن ال complex data types بس ده مش موضوعنا"

انما بالنسبه ل long / short / signed / unsigned فهم يسموا بال type modifiers يعني مثلا
في جهاز 32 bit ال int ممكن يكون 16bit بينما ال long int ممكن يكون 32 bit
ال int 16 bit وال short int يكون 8 bit
ال signed char يختلف تماما عن ال unsigned char في تعامله مع ال overflows ومع ال signed values
ال unsigned long int مختلف تماما بحيث انك لو خلصت ال 32bit بتوعك في ال int variable حيحصل truncation  مش overflow ومتقدرش تسجل signed values يعني سالبه
لازم تاخد بالك من الفروق ديت

علاوه علي كارثة تانيه , ان الناس بتعتبر struct بيعتبروه type !
عشان نوضح الفرق , كل الي فوق دوت type / type modifiers بينما ان ال struct مجرد language constructor بيمكنك انك تعمل انشاء ل data types تانيه called user data types بمعني اصح struct/union/enum دول keywords لتعريف data types تانيه aggregates
فرصه سعيده ! وقال certified قال !
Logged