Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Pointers

Posted on 2011-02-26
6
Medium Priority
?
442 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
Technology Partners: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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