to understand

hi everyone

people always advices beginners to change hello world program for better understanding.

so i thought i should change other peoples works to understand better.

however i couldnt even compile it :D

here is the code
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
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

#define fge_BailOut(format, args...) do{\
        fprintf(stderr, (format), args);\
        exit(-1);\
    }while(0)


#define fge_Malloc(target, type, amount) do{\
        (target) = (type *)malloc((amount + 1)*sizeof(type));\
        if(!target){\
            fge_BailOut("could not allocate %d bytes of ram\n", (int)((amount)*sizeof(type)));\
        }\
        else{fge_BailOut("success");}\
        memset(target, 0, ((amount)*sizeof(type)));\
    }while(0)

int main()
{
    int * obj = NULL;

    fge_Malloc(obj, int, 1);
    return 0;
}


i got the error
C:\Users\Ceset\Desktop\test2\main.cpp||In function 'int main()':|
C:\Users\Ceset\Desktop\test2\main.cpp|24|error: expected primary-expression before ')' token|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


i couldnt fix it.
I dare to recommend you not trying to work with other's code until you become acquainted with syntax yourself. :)

what do you want of this program and why here are these defines instead of functions? some messy horror.

I mean that the idea of the code looks understandable, but this approach to writing programs should be definitely considered a sin. It is not debuggable.
Last edited on
haha funny comments, sin haha. yes it might be but it also should be learned by programmers right.

so can you help me
Last edited on
Simply cut code from defines and insert where it is used, throwing away these "do... while(0)" which are only brackets for macros:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

int main()
{
    int * obj = NULL;
    obj = (int*) malloc((1 + 1)*sizeof(int));
    if(!obj){
        fprintf(stderr, "could not allocate %d bytes of ram\n", (int)((1)*sizeof(int)));
    } else {
    	fprintf(stderr, "success");
    }
    memset(obj, 0, ((1)*sizeof(int)));
    return 0;
}


Rather stupid code.

it also should be learned by programmers right

Believe me, it should not.
Last edited on
hmm ok i will not if you say so.

thx for your helps
Topic archived. No new replies allowed.