-1

Getting the Segmentation Error whenever I am trying to perform the string copy. but the same code will work when I will do direct assignment. In the if statement, I am performing the strcpy operation, getting the memory corruption issue. Help me to understand "strcpy" functionality with respective this code.

 int getEnvVariable()
{
   char mPath[20];
   char mHome[20];
   char *AddressHolder[2];
   char *VariableName[2];

   VariableName[0]="PATH";
   VariableName[1]="HOME";

   memset(&mPath,sizeof(mPath),0);
   memset(&mHome,sizeof(mHome),0);

   AddressHolder[0]=reinterpret_cast<char*>(&mPath);
   AddressHolder[1]=reinterpret_cast<char*>(&mHome);

  char** namePtr = VariableName;
  char** vrbPtr = AddressHolder;
  char* tmp;
  while(*namePtr != NULL)
  {
    tmp = getenv(*namePtr);
    cout<<"\n tmp - "<<tmp<<"\n";
    if(tmp)
    {
      strcpy(*vrbPtr,tmp);
      //*vrbPtr=tmp;
      cout<<"\n"<<*namePtr<<": "<<*vrbPtr;
      ++namePtr;
      ++vrbPtr;
    }
    else
    {
       cout<<"\n Error: Environment veribale name are not set\n";
       return -1;
    }

  }

   cout<<"\n"<<VariableName[0]<<" : "<<mPath;
   cout<<"\n"<<VariableName[1]<<" : "<<mHome;

   return 0;
}
Shrikant B
  • 25
  • 4
  • You are memsetting`0` bytes in `mPath` and `mHome`: `memset( void* dest, int ch, std::size_t count );`. Also, please, just use `std::string` and `std::vector` instead of the weird mix of C and C++. You'll avoid countless bugs that way. – Dan M. Apr 26 '18 at 12:40
  • 1
    Could annyone tell me how the above is C++ leaving the casting aside. As @DanM. is correct. The problem is only starting with writing C instead of C++ – Kaveh Vahedipour Apr 26 '18 at 12:40
  • VariableName is an array of two pointers to char-array. Why do you expect that beyoind that array is a NULL value that you try to detected here ` while(*namePtr != NULL)`? – harper Apr 26 '18 at 12:41
  • 1
    @KavehVahedipour it has `cout` ;) – Dan M. Apr 26 '18 at 12:41
  • So when you enable warnings, what messages to you see? – UKMonkey Apr 26 '18 at 12:43
  • Much simpler: `AddressHolder[0] = mPath;`. Why did you feel a need to `&` and cast there, when you've written `VariableName[0]="PATH";` above? – molbdnilo Apr 26 '18 at 12:47
  • 3
    I am in a lot of pain. I looked at this code. What happened to std::string? – Kaveh Vahedipour Apr 26 '18 at 12:47
  • 1
    You can also initialise `char mPath[20] = {0};` instead of reaching for `memset`, which is very easy to get wrong (and you do). – molbdnilo Apr 26 '18 at 12:49
  • 2
    As a rule of thumb, whenever you need a `reinterpret_cast` to make the code compile, you are doing something bad. – Bo Persson Apr 26 '18 at 12:51
  • you might also want to check `tmp` for `NULL` after the getenv call and not after trying to print it... – FloIsAwsm Apr 26 '18 at 12:53
  • It's a legacy code issue. So before refactoring to complete cpp. I just wanted to understand issue. – Shrikant B Apr 27 '18 at 14:27
  • can any one tell me, why memset() function throw an segmentation fault. char filename[50]; memset(filename,'\0',sizeof(filename)); This memset operation is failing with memset operation. I have checked in stackoverflow for similar problem but I not exactly what happening in memset. please help me. – Shrikant B May 08 '18 at 09:11

1 Answers1

0

In the strcpy, *vrbPtr points to the address of AddressHolder. You want to point to AddressHolder[0]

nicomp
  • 4,344
  • 4
  • 27
  • 60