Solved

Address voilation

Posted on 2011-09-12
18
313 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
[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
  • 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
Independent Software Vendors: 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 36

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
 

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 9

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 9

Expert Comment

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

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

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!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 opening and reading files in the C programming language.

688 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