-2

I'm trying to allocate some memory using MACROS. But I get this errors:

error: expected expression before ‘)’ token

error: expected statement before ‘)’ token

This is my code:

#define ALLOC(p,n) (p*)malloc(sizeof(p)*n)

int main(){
  char *ponteiro;
  ponteiro=ALLOC(5,ponteiro);
  return 0;
}
Community
  • 1
  • 1

3 Answers3

1

Change:

#define ALLOC(p,n) (p*)malloc(sizeof(p)*n)

to:

#define ALLOC(p,n) malloc(sizeof(*(p))*(n))

and in the main() free the allocated memory before return

int main(){
    char *ponteiro;
    ponteiro = ALLOC(ponteiro,5);
    /* Do something */
    free(ponteiro);
    return 0;
}
Santosh A
  • 5,173
  • 27
  • 37
  • `#define ALLOC(p,n) malloc(sizeof(*(p))* (n) )` important to put **every** macro parameter in parenthesis, always! Imagine what happens if you use it with the very common expression: `ponteiro = ALLOC(ponteiro, 1+strlen(foo));`. Your macro would expand to: `malloc(sizeof(*(ponteiro))*1+strlen(foo))`, which is clearly a **BUG**. – Patrick Schlüter Jul 13 '15 at 16:33
0

Change

 #define ALLOC(p,n) (p*)malloc(sizeof(p)*n))

to

 #define ALLOC(n,p) malloc( sizeof(*(p)) * (n) )

because

  1. return of malloc() need not to be casted.
  2. n represents the number of elements, an p is the pointer, so the MACRO definition should change.
  3. sizeof(p) is wrong, as the p is supposed to be the pointer variable name itself. You need to have sizeof(*p).

That said,

  • The recommended signature of main() is int main(void)
  • You should free() the allocated memory after the usage is done.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    Please add some parentheses. – fuz Jul 13 '15 at 12:43
  • 1
    Use this: `#define ALLOC(n, p) malloc(sizeof (*(p)) * (n))` to prevent weird behaviour. – fuz Jul 13 '15 at 12:49
  • Some stylistic guidelines advise against leaving out the parentheses around the operand to `sizeof`. – fuz Jul 13 '15 at 12:50
  • basically , i need to allocate some memory to any array using some MACRO (programation test). And the code that i wrote have some errors – David Ferreira Jul 13 '15 at 12:54
  • 1
    @DavidFerreira **do not change your code in a way that makes the existing answer invalid.** It already earned me one downvote. :-) – Sourav Ghosh Jul 13 '15 at 13:12
  • 1
    ok, updates...two downvotes, now I'm really interested..... What is that obvious mistake which I (seems to be the only person) am missing here...Anybody, help please? – Sourav Ghosh Jul 13 '15 at 14:16
  • 1
    @SouravGhosh No, it's not fine. Please include all the parentheses I wrote. For instance, if you write `ALLOC(a + b, p)`, your code generates `malloc(sizeof(*p) * a + b)` which is incorrect due to operator precedence. – fuz Jul 13 '15 at 14:19
  • @FUZxxl Yes, you're right. I was thinking of the pointer only, the size should also be taken care in this regard. Thanks for correcting. – Sourav Ghosh Jul 13 '15 at 14:24
  • Another nitpick: It's both okay to not `free` memory before calling `exit` (why clean up the carpet when you're destroying the house?) and to not put `void` in the argument list of `main` (since C99, in a *definition* [not a declaration] of a function, an empty argument list denotes a function that takes no arguments) – fuz Jul 13 '15 at 14:33
  • @FUZxxl 1) agree, only if it's `main()` returning from other functions without both returning the allocated pointer and freeing will cause leak 2) see 5.1.2.2.1 of `C11` which says to use `(void)`. – Sourav Ghosh Jul 13 '15 at 14:39
  • @SouravGhosh The standard gives an example of what the definition could look like. `int main() { ... }` is a definition with the exact same effect, this changed with C99. – fuz Jul 13 '15 at 17:35
0

The macro needs some extra parentheses and you made a mistake: you must pass the type to the macro:

 ponteiro=ALLOC(char, 5);

Fix the macro this way:

 #define ALLOC(p,n)  ((p*)malloc(sizeof(p)*(n)))
chqrlie
  • 131,814
  • 10
  • 121
  • 189