Hi All,
I have an integer pointer with value 10, but it returns a different
return value after the preincrement and post increment.
I will be very thankful if somebody can give an explanation for this
behavior.
Listed below is the program and output - executed in vc++6.
int main(int argc, char* argv[])
{
int *i = (int*) calloc(sizeof(i nt),1);
*i = 10;
cout << " *i++ " << *i++ << endl;
cout << " *i " << *i << endl;
*i = 10;
cout << " ++*i " << ++*i << endl;
cout << " *i " << *i << endl;
return 0;
}
*i++ 10
*i -33686019
++*i 11
*i 11
Press any key to continue 14 2023
Review operator precedence. operator++ has higher precedence than
unary operator*, so
*i++
is equivalent to
*(i++)
and not equivalent to
(*i)++
which is what your presumably wanted. When you dereference a pointer
and find an unexpected, large-magnitude value, it generally means that
the pointer isn't pointing where you thought it was.
Luke
Robben wrote: Hi All,
I have an integer pointer with value 10, but it returns a different return value after the preincrement and post increment.
I will be very thankful if somebody can give an explanation for this behavior.
Listed below is the program and output - executed in vc++6.
int main(int argc, char* argv[]) { int *i = (int*) calloc(sizeof(i nt),1);
Don't:
int* i = new int;
*i = 10;
cout << " *i++ " << *i++ << endl;
This is *(i++) : increment the pointer, return the old address and
dereference it.
cout << " *i " << *i << endl;
*i = 10;
illegal, i does not point to valid memory anymore.
cout << " ++*i " << ++*i << endl;
and this is ++(*i) : dereference the pointer and increment the value.
cout << " *i " << *i << endl;
Don't forget to delete the int:
delete i;
return 0; }
*i++ 10 *i -33686019 ++*i 11 *i 11
Jonathan
> Don't forget to delete the int: delete i;
Remembering, of course, not to mix "new" or "delete" in isolation with
C-style (de)allocation. But since I agree with your recommendation to
use new rather than calloc, no problem.
Luke
when you delete a pointer,
delete i;
i = null;
it is a good habit, especially in very complex environment.
Regulus wrote: when you delete a pointer,
delete i; i = null;
it is a good habit, especially in very complex environment.
Is it? Not having dangling pointers* at all is a better habit, IMHO. Are
you going to be checking that "i != NULL" each time you use it?
* Okay, it's "NULL" rather than dangling, which would be fine if C++
were a language where null has a special meaning rather than just being 0.
W Marsh wrote: Regulus wrote: when you delete a pointer,
delete i; i = null;
it is a good habit, especially in very complex environment. Is it? Not having dangling pointers* at all is a better habit, IMHO.
Not having pointers at all is still better, but some things are
inevitable.
Are you going to be checking that "i != NULL" each time you use it?
The point of setting a pointer to 0 is to prevent double deletion.
* Okay, it's "NULL" rather than dangling, which would be fine if C++ were a language where null has a special meaning rather than just being 0.
Well a valid address cannot be 0 so yes, 0 has a special meaning for
pointers in C++.
Jonathan
"W Marsh" <wayneDOTmarshA TgmailDOTcom@de cipher> wrote in message
news:43******** *************** @news.zen.co.uk ... Regulus wrote: when you delete a pointer,
delete i; i = null;
it is a good habit, especially in very complex environment.
Is it? Not having dangling pointers* at all is a better habit, IMHO. Are you going to be checking that "i != NULL" each time you use it?
* Okay, it's "NULL" rather than dangling, which would be fine if C++ were a language where null has a special meaning rather than just being 0.
Actually, for pointers it is the one case it does have a special meaning.
It is legal to delete a null pointer.
int* i = new int;
delete i;
delete i; // ERROR
int* i = new int;
delete i;
i = NULL;
delete i; // NO ERROR
Jim Langston wrote: "W Marsh" <wayneDOTmarshA TgmailDOTcom@de cipher> wrote in message news:43******** *************** @news.zen.co.uk ...
Regulus wrote:
when you delete a pointer,
delete i; i = null;
it is a good habit, especially in very complex environment. Is it? Not having dangling pointers* at all is a better habit, IMHO. Are you going to be checking that "i != NULL" each time you use it?
* Okay, it's "NULL" rather than dangling, which would be fine if C++ were a language where null has a special meaning rather than just being 0.
Actually, for pointers it is the one case it does have a special meaning.
It is legal to delete a null pointer.
Sure, but you could just as well say "it is legal to delete a pointer
with the value 0". It's different to having a null pointer (or
reference) in a language such as C#, where deferencing it would throw an
exception rather than causing some nasty undefined behaviour. int* i = new int; delete i; delete i; // ERROR
int* i = new int; delete i; i = NULL; delete i; // NO ERROR
It's just my personal philosophy to design around potential errors like
this so that they don't have the chance to occur, rather than just
making them harmless if they do. I'm sure there are plenty of
programmers far more skilled than me who would disagree, though.
Luke Meyers wrote: Don't forget to delete the int:
delete i;
Remembering, of course, not to mix "new" or "delete" in isolation with C-style (de)allocation. But since I agree with your recommendation to use new rather than calloc, no problem.
Luke
Thank you Luke and Jonathan for your feedback. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sean Dettrick |
last post by:
I am wondering if it is possible to increment an integer at compile
time using template<int N> (or by any other method, e.g. compiler
directives etc), across multiple source files.
This may sound crazy, but I wish to build a string array (which
requires no user maintenance) of source file RCS version numbers!
The idea is, I would like to be print the beggers out or operate on
them at run time if I feel like it, by using a (maintenance...
|
by: Gus Tabares |
last post by:
Hello all,
I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:
int ndigit;
for (i = 0; i < 10; i++)
ndigit = 0;
int c;
|
by: trint |
last post by:
for (int i = 0; i < listBox2.Items.Count; i+=1)<<something like this?
{
thisString = (string)listBox10.Items;
}
Any help is appreciated.
Thanks,
Trint
|
by: anonymous |
last post by:
I have couple of questions related to array addresses. As they belong
to the same block, I
am putting them here in one single post. I hope nobody minds:
char array;
int address;
Questions 1:
Why cannot I do the following:
|
by: Stefan Wallentowitz |
last post by:
Hello together!
I'm searching for a statistic about the value ranges of integers in a set
of "standard" c-programs.
I'm specifically interested in the avarage ratio of assignments etc. of
integers where the value is either of (un)signed 16 bit or 32 bit size.
Has anybody ever been in contact with something similar?
Bye,
Stefan
| |
by: Anders Würtz |
last post by:
i have an assignment to iterate through a collection containing
different types of numeric values (float, double, int, byte, short etc.)
and to add 1 to all of them. I tried with array and arraylist so far,
but i either get a cast exception or the value doesn't seem to get
updated at all. What should i be doing to make it work?
Thanks in advance
Anders
|
by: Nonoize |
last post by:
Hi all,
Really dumb question but I don't know how to resolve it. Looked in help and
evry book I have.
I have a table where the primary key was set as an Integer and its reached
over 140K worth of records and the numbering has restarted from 1.
I realize now that I should have set it to double. Can someone please advise
how I can save my existing records and restart the numbering from say
|
by: robert maas, see http://tinyurl.com/uh3t |
last post by:
I'm working on examples of programming in several languages, all
(except PHP) running under CGI so that I can show both the source
files and the actually running of the examples online. The first
set of examples, after decoding the HTML FORM contents, merely
verifies the text within a field to make sure it is a valid
representation of an integer, without any junk thrown in, i.e. it
must satisfy the regular expression: ^ *?+ *$
If the...
|
by: haelly |
last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return statement).If eithere of the value is negative, the program prints "negative input" and terminates.Otherwise it prints the value the user entered.
After that,the program compares he first integer and the second integer and prints either, "the first...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |