Solved

Address voilation

Posted on 2011-09-12
18
306 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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
 

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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

860 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