473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Preprocessor errors?

Hi!

I'm getting some weird results from a (probably) valid C expression
with some #define... I'm really puzzled.

A sample code is this (just the variables and defines, not actual code):

#define themacro(p, i) (p)[(i)]

int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);

Where memory_allocation is a function which mallocs and reallocs
memory. I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;

solves the problem. Am I doing something out of standard?

--
Sensei <se******@mac.com>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)

Dec 14 '05 #1
3 1138
Sensei wrote:
Hi!

I'm getting some weird results from a (probably) valid C expression with
some #define... I'm really puzzled.

A sample code is this (just the variables and defines, not actual code):

#define themacro(p, i) (p)[(i)]

int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);

Where memory_allocation is a function which mallocs and reallocs memory.
I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr;

solves the problem. Am I doing something out of standard?

Show a compilable snippet that exhibits the problem. *Real code*. Cut
and paste.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 14 '05 #2
Sensei <se******@mac.com> wrote:
themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...); anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr; solves the problem. Am I doing something out of standard?


Possibly; I'm not much of a guru to tell you. If you haven't
already, however, I would investigate your platform's documentation
for how to invoke only the preprocessor on your code (gcc -E will do
it, for example) to make sure the preprocessed line looks the way you
expect it to.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 14 '05 #3
On Wed, 14 Dec 2005 21:02:42 +0100, in comp.lang.c , Sensei
<se******@mac.com> wrote:
#define themacro(p, i) (p)[(i)]
by convention, user-defined macros are UPPERCASE so they can be
identified easier.
int *ptr, *anotherptr, counter;

themacro(ptr, counter)=anotherptr=MEMORY_ALLOCATION(...);
themacro() will create an object (*ptr)[(counter)] which has type int.
This isn't compatible with anotherptr which is of type int*.
Where memory_allocation is a function which mallocs and reallocs
memory. I get a weird null pointer somewhere, and just writing:

anotherptr=MEMORY_ALLOCATION(...);
themacro(ptr, counter)=anotherptr; solves the problem.
this is just as bad - if you don't get a warning here, you should turn
up warninglevels in your compiler.
Am I doing something out of standard?


Remember how macro substitution works - its literal text replacement.
So you can write down what themacro() will do.
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
3
by: Charlie Zender | last post by:
Hi, I want to have a CPP macro that tests the value of a token and returns the string "No" if the token is undefined (or 0) and returns "Yes" if the token is defined (non-zero). Then I can...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
1
by: Theon Greyjoy | last post by:
Hi all, I have a code behind file which I would like to conditionally compile parts of. The code is in C#. here is what I want to achieve, in pseudo C++ style: ...
6
by: ludovicd | last post by:
Hi to all, There is my question, Let's say I got a fonction which takes a numeric argument long enough so that there has to be spaces in it to represent it correctly. (Ex. 0xf63a002c5ff0338ec...
13
by: Zhou Yan | last post by:
I am a novice, I want to know what is CPP, as showed in title. I heared about it but the book I read do not mentioned it. Maybe this book is too "introductory". Any one can give me a concept about...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.