Solved

C++ Pointers

Posted on 2011-02-26
6
413 Views
Last Modified: 2012-05-11
Hello,
Trying to understand the pointer concepts. Just tried the attached two programs.
I dont understand why the TestB.cpp doesn't work, it is crashing (Says process returned <0xC0000005>).
But TestA.cpp works fine as expected.  
Can somebody explain me why is this happening?

FYI I am using code::blocks in windows 7.

TestA.cpp
TestB.cpp
0
Comment
Question by:v_eman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 34988445
>>Can somebody explain me why is this happening?

The line

*j = 123;

(with 'j' being an 'int*') assings an 4-byte integer to a memory address on the stack that holds a 'char'. This address was set with the line

j = (int *)&c;

So, since the integer value is way too big for the 'char' memory location, you get an access violation.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 34988449
In the one that crashes for you :

>>     j = (int *)&c;

This in itself can cause problems due to memory alignment and when accessing the memory through this pointer, there is the potential to access unallocated memory (or even out-of-bounds or invalid memory).

This is one such access :

>>     cout<<*j<<endl;

This is another, which is actually trying to write data :

>>     *j = 123;

The latter is probably the cause of the crash, because it likely overwrites memory that it's not supposed to (for example, it might overwrite part of the c1 variable).


The one that doesn't crash for you has the same issues (apart from trying to write using the j pointer). The fact that it doesn't crash, doesn't make it any more valid.
0
 

Author Comment

by:v_eman
ID: 34988517
Thank you for immediate responses Infinity08 and jkr, so, If I understood correctly both your comments, then the following code works fine without any potential problems. Am I right?
  int main()
{
    int i = 123;
    char *c1=0;
    int *j =0;
    j = &i;
    c1 = (char *)&i;
    //print the values
    cout<<*c1<<endl;
    cout<<*j<<endl;
    *c1 = 'b'; //or *j = 98;
    cout<<*c1<<endl;
    cout<<*j<<endl;
    return 0;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 34988555
Indeed, it does - yet you will get the ASCII representation of '123' as the first line of output. On the other hand, the code will not crash ;o)

0
 
LVL 86

Expert Comment

by:jkr
ID: 34988561
>>In the one that crashes for you :
>>>>     j = (int *)&c;

Well, it is the dark prelude, yet the actual crash is the assignment

*j = 123;

;o)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34988606
>> Well, it is the dark prelude, yet the actual crash is the assignment

with "the one" in "In the one that crashes for you", I meant the TestB.cpp file, not the line that followed immediately after in my post. Could have made that a bit more clear.

Although, technically, that line could also cause a crash (if there are issues with memory alignment).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 121
c++ substatte a varabe for a string in a LPCTSTR statment 8 100
How to split this in C++ 4 127
max float value 3 68
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question