473,405 Members | 2,373 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,405 software developers and data experts.

Why is this working?

Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];
p = (char(*)[]) new (char[3]);
(*p)[0] = 'c';
cout << (*p) << endl;
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).

Jun 1 '06 #1
3 1501
je*****@gmail.com wrote:
Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];
OK, so 'p' is a pointer to a single-dimensioned array of chars.
p = (char(*)[]) new (char[3]);
So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.
(*p)[0] = 'c';
You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.
cout << (*p) << endl;
You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).


It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '06 #2
Thank you. I was not familiar with the concept of an array decaying to
a pointer.
Victor Bazarov wrote:
je*****@gmail.com wrote:
Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];


OK, so 'p' is a pointer to a single-dimensioned array of chars.
p = (char(*)[]) new (char[3]);


So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.
(*p)[0] = 'c';


You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.
cout << (*p) << endl;


You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.
}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).


It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 1 '06 #3
je*****@gmail.com wrote:
Thank you. I was not familiar with the concept of an array decaying to
a pointer.
I'm sure V would say "You're welcome", but he won't (check his signature).

Joe
Victor Bazarov wrote:
je*****@gmail.com wrote:
Can anyone explain why the following code works?

#include <iostream>

int main ()
{
char (*p)[];


OK, so 'p' is a pointer to a single-dimensioned array of chars.

p = (char(*)[]) new (char[3]);


So, you're allocating an array of three chars and casting the
pointer to its first element to a pointer to an array of chars.
I am not sure this should work without undefined behaviour.

(*p)[0] = 'c';


You're dereferencing the pointer to the array (and get the actual
array, or a reference to it), then you index within it. You're
getting a reference to the first element of that array. Then you
assign 'c' to it. Should be no problem.

cout << (*p) << endl;


You dereference the pointer to the array, get the array, it is
converted to a pointer to 'char', and printed. Since the rest of
the array is not initialised, it can cause undefined behaviour.
If the second or the third elements of the array just happen to
be zero, you get lucky.

}

The above code will print out 'c'. Why is it that the line
(*p)[0]='c' does not cause a segfault? Isn't it the equivalent of
*(*p).


It is. But (*p) is an array that decays to a pointer if you try to
apply indexing to it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 2 '06 #4

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

Similar topics

4
by: zalekbloom | last post by:
I noticed on my PC with win98/IE 6.028 applets are not working. Applets are working when I use Netscape 7.1. When I open the DOS win and I check for a Java version I am getting: C:\WINDOWS>java...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
3
by: S. van Beek | last post by:
Dear reader, To work with linked tables is a professional method of working with a frond end and back end mdb. Working with linked tables on a local pc gives an acceptable performance but...
6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
0
by: Mark B | last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a scheduled hard-boot occurred during a ms-security patch install overnight. They couldn't get the server working again so they...
1
by: Larry Bud | last post by:
Wondering what I should be looking at when looking for a memory leak.. Using Process Explorer by SysInternals, and they have a Working Set and a Virtual Size memory column. Which one should I be...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
0
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.