• C

Address voilation

#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();
}
nagaharikolaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
You need to allocate some space for the destination string, change the line

char *dst;

to

char dst[256];
0
 
nagaharikolaAuthor Commented:
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
 
user_nCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
nagaharikolaAuthor Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
nagaharikolaAuthor Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
nagaharikolaAuthor Commented:
yes i understood then in my original question i am using const char *src
then why src++ should not be used
0
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>> 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
 
Subrat (C++ windows/Linux)Connect With a Mentor Software EngineerCommented:
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
 
Subrat (C++ windows/Linux)Software EngineerCommented:
Indirectly already said by evilrix.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I believe I was the first to state the correct answer and I get nothing?!!
0
 
nagaharikolaAuthor Commented:
I am sorry for the mistake!!!
0
 
nagaharikolaAuthor Commented:
Thanks
0
 
evilrixSenior Software Engineer (Avast)Commented:
Thank you nagaharikola.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.