1

I have the following code, I wonder what is the difference between the implementation of str2 and str3, they both give the same results, which one is more prone to errors? EDIT: when I was testing the representation of str2, I have found that one time my code crashed because str2 was a bad pointer!

/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[] = "Sample string";
  char str0[] = "Sample String and more";
  char* str2;
  str2 = new char[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,str1);
  strcpy (str2,str0);// crash happened here str2 is bad pointer!!!
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  delete str2;
  return 0;
}
Samer
  • 1,923
  • 3
  • 34
  • 54

1 Answers1

3

Aside from the fact that str2 is a pointer whereas str3 is an array (and a pointer is a bit more trickier to use, since you may forget to delete it etc), there is another issue: the memory allocated for str2 is on the free space, via operator new. This is a slow operation. In contrast, str3 has automatic storage duration, and most often its memory is allocated on the stack, which is much faster. So in performance critical code this may make a difference.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • i understand what you just explained........but i am not able to get through my head, whether a "Bad Pointer" is an unallowed pointer or will it give output but with a warning?, meaning, is a BAD pointer same as a SLOW pointer or is slow and bad mean to something different technically. (i hope u understand what i mean is that a SLOW pointer will still be legal and be allowed to execute safely with output and mostly without warnings and errors, but a BAD pointer is simply an error and cant execute) right??, i mean this is how i had an image of it. – Code Man Nov 12 '15 at 03:25
  • 1
    A bad pointer is your worst nightmare. Using it is undefined behaviour, and the program may decide to crash after seemingly working fine for 1 year. – vsoftco Nov 12 '15 at 03:26
  • oh so that means, BAD pointer will execute and work whether properly or weirdly, but it will work, did i get that right?. and a SLOW pointer ?? would be what in this case/context. plz excuse my silly inquisition but its been a long time i have a similar query dangling in my grey area......i guess today i might just end it as this question targets rightly what i have been struggling with for months. – Code Man Nov 12 '15 at 03:27
  • 1
    @CodeMan No, it won't work. It will compile though, but it won't work properly. It may seem to be working, but you should not rely on it. Undefined behaviour should be avoided at all costs. A slow pointer is a pointer into "cold" memory (that's not cached), so accessing it is slower compared to accessing e.g. the stack memory. Although I never saw the term "slow pointer" used before. – vsoftco Nov 12 '15 at 03:29
  • yes i understand it now, then that means, as a general idea, a C++ compiler should never be able to accept and run a code with BAD pointers and should never be able to execute even with a garbage value output right?....generally it should always be giving it out an error...and that slow is just meaning the performance and would not be erroneous... (just this last more confirmation and i guess i am getting it clear). – Code Man Nov 12 '15 at 03:34
  • 1
    @CodeMan The issue is that C++ compilers cannot verify at compile time whether you use a pointer incorrectly, except in a very limited set of cases. And because in C++ performance matters, there is no run-time verification (which will introduce additional checking costs). So pointers are NOT verified, and the burden is on the programmer. That's why it's better to use standard containers instead, such as `std::string` or `std::vector`. They are blazingly fast and you don't have to deal with all those issues related to pointers. So use pointers only when you really need them. – vsoftco Nov 12 '15 at 03:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94882/discussion-between-codeman-and-vsoftco). – Code Man Nov 12 '15 at 03:38
  • OOOHHHHH NOW ITS CLEAR.........i kept on going back to the fact that it is erroneous and non erroneous......now i get it that it may give some random output but a wrong one or mostly undefined behaviour/garbage etc etc.......but compiler will not classify it (atleast not the pointer, may be some other thing due to bad pointer) as an error or warning..Gr8. its solved – Code Man Nov 12 '15 at 03:43