Solved

Address voilation

Posted on 2011-09-12
18
297 Views
Last Modified: 2012-05-12
#include <stdio.h>
#include <conio.h>
 char *ptr;
char *mystrcpy(char *dst, const char *src)
{
ptr = dst;       
      while(*src!='\0')
      {
            *dst=*src;
            dst++;
            src++;
      }

*dst='\0';
      return(ptr);
}
int main()
{
      char *src="hari";
      char *dst;
      printf("the string is %s \n",mystrcpy(dst,src));
      getch();
}
0
Comment
Question by:nagaharikola
  • 6
  • 5
  • 2
  • +2
18 Comments
 

Author Comment

by:nagaharikola
ID: 36527438
The code is ececuted and o/p is hari.
But when enter is pressed to close the console getting exception for memory violation
I am not getting the mistake.
0
 
LVL 9

Expert Comment

by:user_n
ID: 36527444
http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967
you can't increase const pointer src. you cand declare another char *temp =src and increase temp but not src
0
 

Author Comment

by:nagaharikola
ID: 36527474
i have changed the code to this but same problem
char *mystrcpy(char *dst, const char *src)
{
 char *temp=src;
      ptr=dst;
while(*temp!='\0')
      {
            *dst=*temp;
            dst++;
            temp++;
      }

*dst='\0';
      return(ptr);
}
0
 
LVL 35

Accepted Solution

by:
mccarl earned 75 total points
ID: 36527487
You need to allocate some space for the destination string, change the line

char *dst;

to

char dst[256];
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36528132
>> you can't increase const pointer src

I think you're getting your const's mixed up :)

If the pointer is defined as either of these...

const char *
char const *

it is what the pointer points to that is const not the pointer itself.

If the pointer is defined as either of these

char * const
char const * const
const char * const

then the pointer is const so it cannot be changed. Not only are there no const pointers in the code even if there were it wouldn't cause a runtime access violation error -- you'd get a compile time error for trying to modify a non-mutable type.

mccarl appears to have identified the probably cause of the issue here -- you are trying to write to memory you have not allocated.

For what it's worth -- strcpy (or, rather your own version) can be implemented a lot simpler than that (1 line in the function) -- but I'll let you figure that out :)
0
 

Author Comment

by:nagaharikola
ID: 36529262
char *str="hari"; // line1
char str[]=hari; // line 2
what is the difference between these lines
in line one do we need to use malloc for assigning memory to the pointer
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36529372
From my article "C++ Q & A / Interview Practice Questions"

http://e-e.com/A_1415.html



How do these 2 lines of code differ?

char const * p = "foobar";
char const s[] = "foobar";

Open in new window


Answer: Two answers related to character strings...

a) Since a literal string decays to a pointer type of char const * and NOT char * this is not strictly speaking legal C++; however, to maintain backwards compatibility with C, compilers will allow this but they (should) produce a warning since this construct is now deprecated in C++ and will be removed from future versions of the standard.

b) Line one is a pointer to a literal string, which is immutable and attempting to modify the string will result in undefined behaviour. Line two is an array that is initialised with a string value, which means you are allowed to remove the const specifier and modify the array if you so wish (since you own the memory that represents the array).

         
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36529381
>> in line one do we need to use malloc for assigning memory to the pointer
Not if you are just assigning it a literal string.
0
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.

 

Author Comment

by:nagaharikola
ID: 36529477
yes i understood then in my original question i am using const char *src
then why src++ should not be used
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 25 total points
ID: 36529481
>> then why src++ should not be used

That's not the problem -- the problem is dst is pointing to nothing sensible (ie, like some valid memory).
0
 
LVL 8

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 25 total points
ID: 36529744
you can use malloc/calloc to reserve memory for destination string(dst) depending upon the size of the source string(src). After that you call your copy function.
This will solve ur problem.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 36529752
Indirectly already said by evilrix.
0
 
LVL 9

Expert Comment

by:user_n
ID: 36530183
0
 
LVL 35

Expert Comment

by:mccarl
ID: 36533496
I believe I was the first to state the correct answer and I get nothing?!!
0
 

Author Comment

by:nagaharikola
ID: 36548602
I am sorry for the mistake!!!
0
 

Author Closing Comment

by:nagaharikola
ID: 36548617
Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36550384
Thank you nagaharikola.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

13 Experts available now in Live!

Get 1:1 Help Now