A
A
Alifar2015-06-01 00:52:35
Programming
Alifar, 2015-06-01 00:52:35

Why is it incorrect to work with variables and pointers in C++?

Good day. Tell me what to do - there is an incomprehensible work with variables and pointers in VS C++ 15
In the main part of the program in the DO loop, the content of the _data variable changes to some kind of dregs after its first use.
prntscr.com/7bnfmk - everything is OK here. We go further, I press "continue".
prntscr.com/7bng9h - some heresy returns to the screen.
In theory, the string that I entered in the console should be returned to me. Implementation in _setString method
PS I'm a first day in C++ so don't kick

#include <conio.h> 
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <iostream>

using namespace std;

/* Перегрузка функция копирования */
char *strcat_s(char *_string, char _buffer[100]) {
  _string = _buffer;
  return _string;
}

/* Функция ввода строки */
char *setString(char *_string = (char *)malloc(1)) {

  char _buffer[100];
  int i, _lenght = 0;
  _string = '\0';

  cout << "Введите строку:\n";

  do
  {
    i = scanf_s("%99[^\n]", _buffer, 100);

    if (i < 0) {
      free(_string);
      _string = NULL;
      continue;
    }

    if (i == 0)
      scanf_s("%*c");
    else {
      _lenght += strlen(_buffer);
      _string = (char *)realloc(_string, _lenght + 1);
      _string = strcat_s (_string, _buffer);
    }

  } while (i > 0);

  return _string;

}

char *removeSpaces(char *_string) {
  /* Функция удаления пробелов из строки */
  /* Функция удаления лишних пробелов после знаков */
  /* Функция удаления лишних пробелов перед знаками */
  return _string;
}


int main() {

  char *_data, *_result;
  
  // Не забудем поменять настройки консоли при запуске программы - шрифт Lucida Console
  SetConsoleCP(1251);
  SetConsoleOutputCP(1251);
  
  do {
    _data = setString();
    //if (strlen(_data) < 1)
      cout << _data << "\n";
      //continue;
    _result = removeSpaces(_data);
    cout << _data << "\n";
    cout << _result << "\n";

    cout << "Преобразованная строка:\n";

    //for (int i = 0; _result[i] != '\0'; i++) {
      //printf("%c", _result[i]);
    //}
    cout << "\n\n===========\n\n";

  } while (_data[0] != '\0');

  system("pause");
  return 0;

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2015-06-01
@alifar

As I wrote above, I compiled this case under gcc.
UPD:
The problem was solved by replacing the incomprehensible strcat_s with the standard strncpy(_string, _buffer, _lenght);
UPD2:
The whole essence of the error can be understood here valera.asf.ru/cpp/book/c08.shtml
Specifically, the item " 8.3.1. Automatic objects "

After returning to main(), mainResult points to a region of memory not allocated to any object. ( In this example, this area may still contain the correct value, since we did not call any other functions after trouble() and its activation record is probably not overwritten yet .)

V
Vladimir Martyanov, 2015-06-01
@vilgeforce

Why is it incorrect to work with variables and pointers in C++? - because the one who writes on it does not know how to use them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question