Macros Help

Hi,

I am trying to get this to work

#define malloc( (x) * (sizeof(y))) { int size = (x) * (sizeof(y)); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}

I keep getting ( unexpected in macro formal parameter list

#define malloc( (x) * sizeof(y)) { int size = (x) * sizeof(y); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);} this way doesnt help either,

I would like to know what is that I am messing up here, rather than just the solution.

thanks,
rover
blinker82Asked:
Who is Participating?
 
bpmurrayCommented:
Thanks for the info: now i understand what you're doing. What I suggest is the following:

1. Do not change your C source, so you keep:
             char *ptr = (char *)malloc(MAX_PATH * sizeof(char));

2. You create a macro to map to the appropriate call:
           #define   malloc(x)             HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,(x))

This means that you don't have to change anything, and the call above maps to
            char *ptr = (char *)HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,(MAX_PATH * sizeof(char)))

which looks like what you're looking for.

BTW, why do you say that malloc isn't safe? It works just fine in Windows. If you use HeapCreate to create a local heap, HeapAlloc does allow you to isolate memory problems and ito free an entire blob of memory by simply dropping pages, rather than walking through your allocated memory. In this, Windows memory management is better, but that does NOT mean that malloc is bad: it's just less functional.
0
 
bpmurrayCommented:
A define has a left and a right hand side, but your macro is a single item. Think about it for a second, and you'll realize that what you want to say is that "malloc(foo, bar)" should do some action on foo and bar. So, your left hand side is:
    malloc(x, y)

and this should be equal to
    { int size = (x) * (sizeof(y)); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}

Putting it all together:

#define   malloc(x, y)             { int size = (x) * (sizeof(y)); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}
0
 
bpmurrayCommented:
Actually, do you really want to define a variable size? It's only used in the block, so you can't reuse it anywhere. So you can replace size with the calculation (x) * (sizeof(y)), so we get:
    { int size = (x) * (sizeof(y)); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}
becomes
                                              HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,(x) * (sizeof(y)))
and you can remove a pair of parentheses, giving:

#define malloc(x, y)        HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, (x)*sizeof(y))

which can be used like any other function call:

     char * foo =  malloc(200, int);
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
blinker82Author Commented:
thanks for the answers, I understand the usage of malloc in the macro as malloc(x,y),  is there a way I can use it in the macro as

malloc( x * sizeof(y)) instead of malloc(x,y), the reason is I have huge chunks of code written in C with the standard C library malloc usage, I do not want to go about doing a manual text replacement with malloc(x,y) , I have to port it to win 32.

thanks for your time
rover
0
 
bpmurrayCommented:
No, that's not how it works. What do you currently have? I bet you have something like malloc(x * sizeof(y)) today - you can still do that since you're allocating a number of bytes, which is the param to malloc. In fact, if you have malloc(x * sizeof(y)), this is the same as malloc(foo), where foo = x * sizeof(y), and you can do this:

#define malloc(foo)        HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, foo)

Anyway, malloc works fine in Win32 - can you use that? Malloc always allocates the specified number of bytes, while you're trying to make it function like calloc.
0
 
blinker82Author Commented:
let me get this right

#define foo (x * sizeof(y))
#define malloc(foo)        HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, foo)
0
 
bpmurrayCommented:
No. I think I confused you. What I'm trying to do is guess what you really want to do. So I'll ask a few questions instead :-)
1. Why do you need to redefine malloc? Why can't you use the existing malloc?
2. What do you currently have in your source code? Give me an example of your usage of malloc.

From these I can be much more precise and correct.
0
 
blinker82Author Commented:
1. Why do you need to redefine malloc? Why can't you use the existing malloc?
 - because the compiler we have does not support unsafe standard C Library functions, I had already developed the code in unix which had support for standard C library functions, now I have to port it to win 32.

2.What do you currently have in your source code? Give me an example of your usage of malloc.
- now I had used malloc at several places in my code, I did not want to replace the entire code with HeapAlloc( a win32 contemporary), so I wanted to use a simple macro to achieve it,

for example I had used malloc at some places in my code like this

char *ptr = (char *)malloc(MAX_PATH * sizeof(char));

so I wanted to use a macro to make it simpler and I used this

#define malloc( (x) * sizeof(y)) { int size = (x) * sizeof(y); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}
0
 
blinker82Author Commented:
thanks a ton, I am sorry I didnt mean malloc as unsafe, most of the standard C library functions (unfortunately the classic malloc) have been disabled and we are encouraged to use win32 functions only here.

thanks.
Raghu
0
 
blinker82Author Commented:
the sol works thats gr8, btb Mr Murray can you pls explain what's wrong with this #define malloc( (x) * sizeof(y)) { int size = (x) * sizeof(y); HeapAlloc(GetProcessHeap(),HEAP_NO_SERIALIZE,size);}, why doesnt the compiler interpret this correctly and keeps throwing an error.

thanks once again,

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.