Solved

C++ Pointers

Posted on 2011-02-26
6
400 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
>>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
Comment Utility
>> 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now