Solved

Macros Help

Posted on 2006-07-20
10
342 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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