473,395 Members | 1,532 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,395 software developers and data experts.

Macro help

Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
should work for strings of different length.
Is there any possibility to do this?
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.
thanks in advance and greetings

Axel Klein

Aug 7 '06 #1
7 1694
d2*****@gmx.net wrote:
Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
You forgor the null terminator there :-)

Anyway, what purpose would converting a string literal to a character
array initialization form serve for you ?
should work for strings of different length.
Is there any possibility to do this?
Using the preprocessor only, on a C string literal ? no.
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.
Aug 7 '06 #2
Nils O. Selåsdal schrieb:
d2*****@gmx.net wrote:
Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
You forgor the null terminator there :-)

Anyway, what purpose would converting a string literal to a character
array initialization form serve for you ?
The caracter array initialization form is used to allocate memory for
the string (it is a wide char array on hp-ux).
After the conversion i call a function to convert the char array to an
SAP-CHAR array (hpux uses 4byte wchar while SAP-CHAR is just 2 byte;
needed to communicate with a SAP system). After that the original char
array should be freed since memory usage would explode if not (every
literal would be in memory twice once as wchar und once as SAP-CHAR).
should work for strings of different length.
Is there any possibility to do this?
Using the preprocessor only, on a C string literal ? no.
bad news :(
Any other idea to do this?
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.
Aug 7 '06 #3
d2*****@gmx.net writes:
Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
should work for strings of different length.
Is there any possibility to do this?
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.
Not so much trouble with memory leaks as taking into account the need to
"escape" certain characters which alters the amount of chars needed for the final
destination string. But doable. Try it.

When I say the need to escape, I assume you want it compilable in C.
Aug 7 '06 #4

Richard schrieb:
d2*****@gmx.net writes:
Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
should work for strings of different length.
Is there any possibility to do this?
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.

Not so much trouble with memory leaks as taking into account the need to
"escape" certain characters which alters the amount of chars needed for the final
destination string. But doable. Try it.
I got around 5000 literals to convert. So memory is a quite big issue
isn't it ?
When I say the need to escape, I assume you want it compilable in C.
yeah plain old c nothing more allowed for some reasons

Aug 7 '06 #5


d2*****@gmx.net wrote On 08/07/06 08:43,:
Nils O. Selåsdal schrieb:

>>d2*****@gmx.net wrote:
>>>Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this

You forgor the null terminator there :-)

Anyway, what purpose would converting a string literal to a character
array initialization form serve for you ?
The caracter array initialization form is used to allocate memory for
the string (it is a wide char array on hp-ux).
Why not write

wchar_t string[] = L"string";

If the "string" part is an ordinary string literal (not a wide
string literal), generated by macro expansion or something, you
can convert to wide by concatenating with an empty wide literal:

wchar_t string[] = L"" "string";
After the conversion i call a function to convert the char array to an
SAP-CHAR array (hpux uses 4byte wchar while SAP-CHAR is just 2 byte;
needed to communicate with a SAP system). After that the original char
array should be freed since memory usage would explode if not (every
literal would be in memory twice once as wchar und once as SAP-CHAR).
Freeing the original character array will accomplish little.
The only way you can free() it is if you allocated it dynamically
with malloc() and friends. If you allocate it dynamically you
cannot use an initializer, so you will need to copy the initial
contents from somewhere else. You can then free the array, but
the "somewhere else" cannot be free()d and will still occupy
space.

--
Er*********@sun.com

Aug 7 '06 #6
d2*****@gmx.net writes:
Richard schrieb:
>d2*****@gmx.net writes:
Hi there,
i am looking for a macro which replaces a string "string" with its
character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
should work for strings of different length.
Is there any possibility to do this?
Or is the only solution to write a function which is quite hard since i
need to allocate memory for the char[] and using a functon makes some
problems with memory leaks then.

Not so much trouble with memory leaks as taking into account the need to
"escape" certain characters which alters the amount of chars needed for the final
destination string. But doable. Try it.
I got around 5000 literals to convert. So memory is a quite big issue
isn't it ?
Depends. If you have a machine made in the last 15 years I would say no
:)

The max length of your single string will strlen(s)*5+2. I think thats
right : work it out.

5000 literals would be about a quarter of a megabyte I suppose. Not
much. Why do you suppose that is an issue?
>
>When I say the need to escape, I assume you want it compilable in C.

yeah plain old c nothing more allowed for some reasons
Aug 7 '06 #7


d2*****@gmx.net wrote On 08/07/06 09:21,:
>
I got around 5000 literals to convert. So memory is a quite big issue
isn't it ?
Is it?

How long are these literals? 100 characters each (a couple
lines of source)? That's 500000 characters. At four bytes per
wchar_t (your system's value), that's two megabytes. Plus two
more bytes when each is converted to an SAP-CHAR (as you described
elsethread), which is another megabyte. There was a time when
three megabytes was A Lot Of Memory, and there are circumstances
when it still is -- but you say your program runs under HP-UX,
which sort of suggests it's not running in a cell phone or a
Palm Pilot or a toaster oven or something.

Why bend your program all out of shape to save something
less than three megabytes? (Or more likely, as I've explained
elsethread, to fail to save it?)

--
Er*********@sun.com

Aug 7 '06 #8

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

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
8
by: Nick M | last post by:
Hello All, Excellent info here Thanks! I am very new to using access in general and I am on a learning curve. I'm trying to import an excel workbook (with worksheets) into an access db via a...
10
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
4
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
2
by: Senthil | last post by:
Hi All I need to create an Excel report and create a command button and have to run a macro on the click event that will print all the pages in the Excel workbook. I am able to create the report...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
1
by: skennd | last post by:
Hello, All your help is appreciated in this problem. I am running a macro to execute certain queries and the macro is started to run by a windows task scheduler. However, after the macro runs...
2
by: Stratocaster | last post by:
Hello, and thank you for any help in advance. I need help determining if any commands exist in VB (Excel macro style) that can enable a user to select cells and run a macro which performs...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.