?
Solved

C++ Pointers

Posted on 2011-02-26
6
Medium Priority
?
432 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 1000 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 1000 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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

801 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