Solved

C++ Pointers

Posted on 2011-02-26
6
408 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 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