I so confused about SEGFAULT in my program. I am trying to implement scanf. So I am passing const char as the first parameter, than parse it for % entries and than use variables arguments to store value in it.
I am so confused about this.
I am not an expert in C an C++ programming. But I understand memory allocation and usage. Here is my code snippet. I am using getline passing to it NULL pointer to char and 0 as size. According to manual it should malloc memory by itself.
I think that is the bad idea that some func allocating memory. But let consider it is ok.
As this func has malloc memory , so I have to free it, but when I am trying to do this , I am getting segmentation fault , but If I leave it as it is , there is no memory leaks in program. I am using valgrind to check memory leaks.
It always crashes on printf ,but in debug mode is seems that variable is correct and consists string.
Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
s = va_arg(argp, char *);
if (lengthIsSet) {
size_t newLen ;
size_t readLen;
char *strInput = NULL;
length = atoi(lengthStr.c_str());
readLen = getline (&strInput,&newLen,inputSteam);
if (!strInput) {
break;
}
int sizeToCopy = (length<readLen)?length:readLen;
memcpy(s,strInput,sizeToCopy+1);
s[sizeToCopy] = '\0';
}
|
I have tried to initialize
size_t newLen = 0;
it seems to work know, but I don't thinks it causes the problem. Please help to solve this. I would be very grateful for any help and explanation.
Here is full function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
void scanfCustom(const char* input, FILE *inputSteam, va_list argp) {
const char *p;
int * i;
bool formFound = false;
std::string lengthStr("");
int length;
bool lengthIsSet = false;
unsigned u;
char *s;
p = input;
for (; *p != '\0'; p++) {
if ((formFound) && ( *p >= '0' && *p <= '9' ) ) {
lengthStr+=(*p);
lengthIsSet = true;
continue;
}
if (*p == '%') {
formFound = true;
continue;
}
formFound = false;
switch (*p) {
case 's':
s = va_arg(argp, char *);
if (lengthIsSet) {
size_t newLen = 0;
size_t readLen;
char *strInput = NULL;
length = atoi(lengthStr.c_str());
readLen = getline (&strInput,&newLen,inputSteam);
if (!strInput) {
break;
}
int sizeToCopy = (length<readLen)?length:readLen;
memcpy(s,strInput,sizeToCopy+1);
s[sizeToCopy] = '\0';
printf("%s",s); // It crashes here
// free(strInput);
//fgets(s,length,inputSteam);
}
//TODO READ FULL STR
}
}
}
|
Please suggest how to read user input correctly using C++, maybe there is more easier and convenient way to do this . Without using explicitly scanf, vscanf and so on. Please help, because I have spent hours dealing with this problem. I have found only this functions to read user input from stream.
Of course I've debug my app using gdb, but it seems quite correct, memory copies and have exactly what I need,
I am not pro in C++ and C so don't know a lot of features.