Solved

Address voilation

Posted on 2011-09-12
18
298 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
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 139
Why this code doesn't work? 8 97
Problem with form project 66 317
Beginner Probleme with my IDE Code::Blocks C++ 5 233
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

895 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