Solved

Macros Help

Posted on 2006-07-20
10
321 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:blinker82
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 17148381
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
 
LVL 15

Expert Comment

by:bpmurray
ID: 17148409
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
 

Author Comment

by:blinker82
ID: 17148599
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
 
LVL 15

Expert Comment

by:bpmurray
ID: 17148850
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
 

Author Comment

by:blinker82
ID: 17149015
let me get this right

#define foo (x * sizeof(y))
#define malloc(foo)        HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, foo)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 15

Expert Comment

by:bpmurray
ID: 17149249
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
 

Author Comment

by:blinker82
ID: 17149574
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
 
LVL 15

Accepted Solution

by:
bpmurray earned 75 total points
ID: 17152488
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
 

Author Comment

by:blinker82
ID: 17155815
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
 

Author Comment

by:blinker82
ID: 17155852
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now