473,473 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])

what's this?
Jun 27 '08 #1
11 2585
fuzhen said:
what's this?
Checking the subject line, I guess you are referring to this:

p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])

to which the answer is that it's a badly written call to malloc. But, given
suitable furniture (a function wrapped around it, <stdlib.h>, etc - and a
semicolon wouldn't go amiss at the end there), it's perfectly legal, provided
sizeof(float) doesn't exceed 6 on the target system. On systems where it
does, the behaviour is undefined.

Let's start off by assuming sizeof(float) is 3 - which is a perfectly legal
value for sizeof(float), and has the merit of being a little unlikely, to say
the least.

Thus, given that assumption, the code reduces to:

p=(char *)malloc((3)["\000\006\010\013\015\100"])

and (3) is just 3, so that gives us:

p=(char *)malloc(3["\000\006\010\013\015\100"])

Now, a[i] and *(a + i) are guaranteed to be equivalent, so let's do some
substituting:

p=(char *)malloc(*(3+"\000\006\010\013\015\100"))

Okay, x+y is the same as y+x, so:

p=(char *)malloc(*("\000\006\010\013\015\100"+3))

and *(a + i) is the same as a[i], so:

p=(char *)malloc("\000\006\010\013\015\100"[3])

Now, "\000\006\010\013\015\100" is an array with values { 0, 6, 8, 11, 13, 64,
0 }, so "\000\006\010\013\015\100"[3] is element 3 of that array: element 0
is 0, element 1 is 6, element 2 is 8, element 3 is 11. So we can substitute
that back in again:

p=(char *)malloc(11)

and finally we can lose the spurious and utterly pointless cast, which gives:

p = malloc(11)

Is that sufficiently simple for you? (Remember to replace 11 with a different
value from the array if sizeof(float) doesn't happen to be 3 on your system.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2

Is that sufficiently simple for you? (Remember to replace 11 with a
different
value from the array if sizeof(float) doesn't happen to be 3 on your
system.)
Does something like this have a legitimate use at all?
Jun 27 '08 #3
MisterE said:
>
>Is that sufficiently simple for you? (Remember to replace 11 with a
different
value from the array if sizeof(float) doesn't happen to be 3 on your
system.)

Does something like this have a legitimate use at all?
No - confusing the newbies is about its limit, really.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #4
MisterE wrote:
>
>Is that sufficiently simple for you? (Remember to replace 11 with a
different
value from the array if sizeof(float) doesn't happen to be 3 on your
system.)

Does something like this have a legitimate use at all?
/Something/ like this, yes, depending on how close a similarity
you insist on. `malloc` is, after all, a useful function.

--
"Never ask that question!" /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 27 '08 #5
"MisterE" <Mi*****@nimga.comwrites:
>Is that sufficiently simple for you? (Remember to replace 11 with a
different
value from the array if sizeof(float) doesn't happen to be 3 on your
system.)

Does something like this have a legitimate use at all?
This may save you a couple of bytes.

Making it more readable by placing the table as a static before the
call would probably not cost you any bytes, this would, however only
work if this is used where you can have declarations, otherwise eg. in
a macro, this may be a solution.

The numbers look odd though, appearently only size 1 to 4 produces
sensible values, what's the context here?

--
... __o Řyvind
... _`\(, http://www.darkside.no/olr/
... (_)/(_) ... biciclare necesse est ...
Jun 27 '08 #6
fuzhen wrote:
what's this?
Doodles from a sick programmer.
Jun 27 '08 #7

"fuzhen" <fu****@gmail.comschreef in bericht
news:b4**********************************@w34g2000 prm.googlegroups.com...
what's this?
in what context was this used?

Jun 27 '08 #8
Sjouke Burry wrote:
fuzhen wrote:
>what's this?
Doodles from a sick programmer.
Best answer I've seen.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #9
On Jun 11, 4:30 pm, Richard Heathfield <r...@see.sig.invalidwrote:
fuzhen said:
what's this?

Checking the subject line, I guess you are referring to this:

p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])

to which the answer is that it's a badly written call to malloc. But, given
suitable furniture (a function wrapped around it, <stdlib.h>, etc - and a
semicolon wouldn't go amiss at the end there), it's perfectly legal, provided
sizeof(float) doesn't exceed 6 on the target system. On systems where it
does, the behaviour is undefined.

Let's start off by assuming sizeof(float) is 3 - which is a perfectly legal
value for sizeof(float), and has the merit of being a little unlikely, to say
the least.

Thus, given that assumption, the code reduces to:

p=(char *)malloc((3)["\000\006\010\013\015\100"])

and (3) is just 3, so that gives us:

p=(char *)malloc(3["\000\006\010\013\015\100"])

Now, a[i] and *(a + i) are guaranteed to be equivalent, so let's do some
substituting:

p=(char *)malloc(*(3+"\000\006\010\013\015\100"))

Okay, x+y is the same as y+x, so:

p=(char *)malloc(*("\000\006\010\013\015\100"+3))

and *(a + i) is the same as a[i], so:

p=(char *)malloc("\000\006\010\013\015\100"[3])

Now, "\000\006\010\013\015\100" is an array with values { 0, 6, 8, 11, 13, 64,
0 }, so "\000\006\010\013\015\100"[3] is element 3 of that array: element 0
is 0, element 1 is 6, element 2 is 8, element 3 is 11. So we can substitute
that back in again:

p=(char *)malloc(11)

and finally we can lose the spurious and utterly pointless cast, which gives:

p = malloc(11)

Is that sufficiently simple for you? (Remember to replace 11 with a different
value from the array if sizeof(float) doesn't happen to be 3 on your system.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
This is the first time I am seeing array literals declared in this
manner. This is a new addition in C99, isn't it? And this cryptic
thing is supposed to pass array literals ( just to quote one of the
possible scenarios).
Jun 27 '08 #10
rahul wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
Checking the subject line, I guess you are referring to this:

p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])
<snip>
>
This is the first time I am seeing array literals declared in this
manner. This is a new addition in C99, isn't it?
No. Neither string literals nor a[i] == i[a] is new.

--
Peter
Jun 27 '08 #11
On Jun 12, 10:07 am, Peter Nilsson <ai...@acay.com.auwrote:
rahul wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
Checking the subject line, I guess you are referring to this:
p=(char *)malloc((sizeof(float))["\000\006\010\013\015\100"])
<snip>
This is the first time I am seeing array literals declared in this
manner. This is a new addition in C99, isn't it?

No. Neither string literals nor a[i] == i[a] is new.

--
Peter
Thanks Peter,
I missed that part in the FAQ. I got my answer.
char *a = "hello";
printf ("%c", 2[a]);
The above snippet directly does
printf ("%c", 2["hello"]);

a[i] == i[a]. Hey...I knew that
Jun 27 '08 #12

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

Similar topics

6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
11
by: Meghavvarnam | last post by:
Hi all, I just joined this group and am new to VC++. I wrote the code following the next para in C++ and used VC++ 6.0 Enterprise Edition to build and test. I had the following error message...
3
by: Adolfo López Escribano | last post by:
I'm try to launch db2Backup from Borland Delphi, but I obtain an undocumented SQLCODE -1152. Any idea? My code is the next: Type Tsqlca = record sqlcaid: Array of Char; sqlcabc: LongInt;...
4
by: Schley Andrew Kutz | last post by:
I am iterating through 454 rows of a table and with each row I use the ID field to form a URI for a contact in a public folder on my exchange server. My memory keeps growing though. I think it...
4
by: Craig Kenisston | last post by:
Hi, I have a dll that must return a string to a C# .Net application and also needs to server a Delphi application. I have defined my delphi function like this : function GetString(var strPtr...
7
by: Michael Cornelison | last post by:
I believe I have found a serious performance problem with heap management in C++ .Net. I have an application does the following thousands of times: vint = new int; vchar = new char; The...
23
by: grubertm | last post by:
When accessing pointer member variables these are not treated as const even though the instance is const. This came as a complete surprise to me and I would appreciate some feedback why that is the...
7
by: PSN | last post by:
Hello everyone, int main() { char *pChar; pChar = (char *)malloc(16); memcpy(pChar, "AAAAAAAAAAAAAAA", 16); printf ("***%s***\n", pChar); realloc(pChar, 28);
1
by: Jure Bogataj | last post by:
Hello! Does anybody knows how to handle this issue: I have an Delphi DLL with following two function declaration: function DeallocateString(lpszString : PChar) : DWORD; stdcall; function...
0
by: ManUtdFans | last post by:
I have a Delphi DLL function: function GetMyNickName(aMyName: PChar): PChar; stdcall; This will return my nickname. In ASP.NET, C#: static extern string GetMyNickName(string aMyName);
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.