Here is an example of what I am trying to do:
#include <stdio.h>
FILE* f;
const char* getstring()
{
f = fopen ("hello.txt", "r");
char x[200];
for (int i = 0; i < 200; i++) x[i] = 0;
for (int c = getc(f), i = 0; (c != EOF) && (i < 200); c = getc(f), i++)
x[i] = c;
fclose(f);
printf ("getstring(): x = %s", x);
const char* y = x;
printf ("getstring(): y = %s", y);
return y;
}
void printstring (const char* string)
{
printf ("%s", string);
}
int main()
{
printstring(getstring());
printf ("\nprintf: %s", getstring());
return 0;
}
and the output is:
getstring(): x = Hello World
getstring(): y = Hello World
���getstring(): x = Hello World
getstring(): y = Hello World
printf: ��������
I don't know why the printstring() function is outputting nothing and printf is outputting random data or why there is a bit of random data at the end of the string when I use the printstring() function.
Is there any way to fix this and what am I doing wrong?
The problem
The problem is that getstring() returns a pointer to a local array. This array gets destructed when the function returns, so you have an dangling pointer. Using this pointer is then undefined behavior. Anything can happen: for example you can get garbage random value, you can get the old unchanged value, or the system could crash.
The solution
Since this question is labelled c++, just use std::string instead of char*, and this kind of nightmare will vanish for good.
Note that Using std::string in prinf() would require you get a pointer with .c_str().
If for an obscure reason, you are required to use char* you'd have to to use strdup() or llocate some memory for the c string and return a pointner to that memory. But the caller must then delete this pointer if you don't want memory to leak.
The C string is stored in a function local char array. This array is destroyed when the function is left. Since the question is tagged as C++ use std::string
#include <iostream>
#include <string>
#include <fstream>
std::string getstring()
{
std::ifstream f("hello.txt");
std::string x;
x.resize(200);
for (int i = 0; i < 200; i++) x[i] = 0;
for (int c = f.get(), i = 0; (c != EOF) && (i < 200); c = f.get(), i++)
x[i] = c;
std::cout << "getstring(): x = " << x;
const std::string& y = x;
std::cout << "getstring(): y = " << y;
return x;
}
void printstring (const std::string& string)
{
std::cout << string;
}
int main()
{
printstring(getstring());
std::cout << "\nprintf: " << getstring();
return 0;
}
Since others have explained how to go about resolving the issue, I'll expand the other answers a bit and explain why you had an issue in the first place.
To put it simply, whenever a function is called, it is given a stack frame (also called activation record) of its own. That is to say: it is given an area of memory where it can place its local variables. When the function returns, the stack frame is destroyed. If you then call another function, the stack frame of that function overwrites the stack frame of the previous function.
In this specific case, when getstring returns and printstring and subsequently printf are called, the stack frames of the latter two along with that of main overwrite the data that was previously located within the stack frame of getstring. The most likely result is that printf will output complete junk. In the worst case, it can make the entire program crash if the null terminator of the string was overwritten.
It is also interesting to note that in your case it seems that a binary value corresponding with that of the null terminator \0 was inserted somewhere, because only a bit of junk is printed before printf returns. This would indicate that it did not stop at the original null terminator of the character array x but rather encountered a value it interpreted as the null terminator and returned.
If you wish, you can read more about the call stack on Wikipedia.
This is the answer for C code (not C++). In the code in NPE's answer on the similar question that I found at this link, which I didn't find before I posted this question, I found the malloc(bytes) function does the trick but if you compile with gcc, you have to have to convert the malloc function as a char* manually by using (char*) malloc (bytes). I also found that because it's using pointers you will have to treat the allocated string as a global variable when you free() it.
Here is an example of the working code based on the code in my question:
#include <stdio.h>
#include <stdlib.h>
FILE* f;
char* getstring()
{
f = fopen ("hello.txt", "r");
char* x = (char*) malloc (200);
int length = 0;
for (int i = 0; i < 200; i++) x[i] = 0;
for (int c = getc(f), i = 0; (c != EOF) && (i < 200); c = getc(f), i++)
{ x[i] = c; length++; }
fclose(f);
return x;
}
void printstring (char* string)
{
printf ("%s", string);
free (string);
}
int main()
{
printstring(getstring());
return 0;
}
And this outputs the exact first 200 bytes of "hello.txt".
Related
This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 3 years ago.
So, my task is to make and return a copy of a given string, and in case it has failed (for example, hit enter) to return NULL. However, when I hit Enter, the program continues to work and to print out some garbage values. The fun fact though, is that the program works just fine when I run it through the Debugger (which chalenges me the most). I don't find a simple explanation for what might've gone wrong. Or is it the problem with my compiler?
#include <stdio.h>
#include <stdlib.h>
// listing functions
char *ft_strdup(char *src);
int main(void)
{
// input of a string
char c[200];
scanf("%[^\n]%*c", c);
// calling a function
char *f = ft_strdup(c);
printf("\n%s\n", f);
free(f);
return 0;
}
char *ft_strdup(char *src)
{
int i = 0;
// itearting to get the 'length' of string src
while (src[i] != '\0')
++i;
// if user has inputted nothing - return NULL and break the function
if (i == 0)
{
return NULL;
}
// otherwise, make a copy of the string
char *x = malloc(i+1);
int j = 0;
while (j != i)
{
x[j] = src[j];
++j;
}
x[i+1] = '\0';
// print out and return
printf("%s\n%s\n%i", x, src, i);
return x;
}
It prints garbage value because char c[200]; is not initialized that mean It has garbage value inside.
char c[200] = {0};
try this and probably get some crash due to NULL pointer access.
add this to avoid crash
if (f != NULL) {
printf("\n%s\n", f);
free(f);
}
The program has undefined behavior by two reasons.
The first one is that the array cwas not initialized
char c[200];
So it has indeterminate values and if the user just press the Enter key the array will not be changed.
You could initialize it in C++ like
char c[200] = {};
or in C like
char c[200] = { '\0' };
or just like
char c[200] = "";
;
The second one is you access memory beyond the the allocated array in the function ft_strdup.
x[i+1] = '\0';
you have to write
x[j] = '\0';
And you have to check the return value of the function.
Pay attention to that in C++ you should use the operators new and delete instead of the C functions malloc and free. And instead of dynamically created character arrays to store strings you should use the standard C++ class std::string.
So I have this header given to me
int mystrcpy(char *c, const char* s);
And I have to implement strcpy function myself. But when I do it and run the program, the console stops working, which I believe is a sign of memory address violation.
I tried this:
int mystrcpy(char *c, const char* s)
{
int i=0;
for(i=0;i<strlen(s);i++)
c[i]=s[i];
c[++i]=NULL;
cout<<c;
return 0;
}
Full code:
#include<iostream>
#include<cstring>
using namespace std;
class Persoana
{
char *nume;
int an;
float inaltime;
public:
int my_strcpy(char *c, const char*s);
};
int Persoana::my_strcpy(char *c, const char* s)
{
//code I need to insert
return 0;
}
int main()
{
Persoana a;
cout << endl;
cout<<a.my_strcpy("maria","george");
return 0;
}
Other posters have posted implementations of strcpy - Why you are using this in C++ code? That is an interesting question as C++ very rarely uses C style strings
The other problem is with its usage:
int main()
{
Persoana a;
cout << endl;
cout<<a.my_strcpy("maria","george");
return 0;
}
The strings "maria" and "george" are read only. Instead create an empty read/ write string as shown below that is long enough - i.e. 7 characters (do not forget the null character)
So the code should be
int main()
{
Persoana a;
char copy_of_string[7];
cout << endl;
cout<<a.my_strcpy(copy_of_string,"george");
return 0;
}
Your loop itself is OK (but inefficient, since you call strlen() on every iteration). The real problem is when you go to insert the null terminator at the end of the copied string. You are incrementing i again before inserting the terminator. DON'T do that. i is already at the correct index after the loop ends, so just use it as-is:
int mystrcpy(char *c, const char* s);
{
int i, len = strlen(s);
for(i = 0; i < len; ++i)
c[i] = s[i];
c[i] = '\0'; // <-- NO ++ HERE!
cout << c;
return 0;
}
That being said, the simplest way to implement strcpy without using i at all is as follows:
int mystrcpy(char *c, const char* s)
{
char *p = c;
while (*p++ = *s++);
cout << c;
return 0;
}
If you remove the cout then it becomes simpler:
int mystrcpy(char *c, const char* s)
{
while (*c++ = *s++);
return 0;
}
No matter what, make sure that when you call mystrcpy(), c is pointing to a char[] buffer that is allocated to at least strlen(s)+1 chars in size, otherwise the code will have undefined behavior.
The for loop
for(i=0; i < strlen(s); i++)
aborts when i < strlen(s) becomes false, which is the case when i is equal to strlen(s). So that will be the value of i when the loop ends.
C strings are NULL-terminated as you know, so you need strlen(s) + 1 bytes reserved for c. Since you increment i again before writing the '\0' character, you're using strlen(s) + 2 bytes starting at c.
If c is exactly the size that's needed (strlen(s) + 1), that may lead to an access violation, since you're writing past the end of the allocated memory.
So instead of
c[++i]=NULL;
write
c[i]='\0';
Hope this makes sense!
I have been given a task, where I need to create the string_copy function Note that the function body and prototypes have been given by the source and that needs to be maintained. The portions written by me are after the comment write your code here.
#include <iostream>
using namespace std;
int string_length(const char* string_c);
char* string_copy(const char* string_c);
int main()
{
const char* string_c = "This is a string and is a long one so that we can create memory leaks when it is copied and not deleted";
// write your code here
int length = string_length(string_c);
cout << "Copied String: " << string_copy(string_c) << endl;
return 0;
}
int string_length(const char* string) {
int length = 0;
for (const char* ptr = string; *ptr != '\0'; ++ptr) {
++length;
}
return length;
}
char* string_copy(const char* string) {
// we need to add 1 because of ’\0’
char* result = new char[string_length(string) + 1];
// write your code here (remember zero-termination !)
int i;
for (i = 0; string[i] != '\0'; ++i)
{
result[i] = string[i];
}
result[i] = '\0';
return result;
}
Now task tells me
that it is very important that any memory allocated with e=new TYPE is
released later with delete e (and a=new TYPE[size] with delete [] a)
else this will lead to an error.
It is not exactly clear if error means compile/runtime error or error as in my task did not meet the requirement error.
My question is, in this code how do I delete the intermediate dynamically created result array? If I delete result, won't it fail the purpose of the task? Then how am I to respect the quotation above or maybe simulate memory leak as given in the long string constant?
Thanks.
EDIT: Why the negative votes? Please at least explain the reason! I am not asking any solution or something, but mere suggestion if I am missing some point or not!
The caller of string_copy would be responsible for releasing the memory when it's done with it by calling delete[] on it.
This is, by the way, a terrible way to write C++ code. You should be using std::string or std::vector<char> or something like that.
Here's why:
int length = string_length(string_c);
char* copy = string_copy(string_c);
cout << "Copied String: " << copy << endl;
delete[] copy;
return 0;
Yuck.
In fact the ideal solution is to use std::string and not char *. There is no real need of using char * instead of std::string in your example.
With std::string:
You don't need to new anything
You don't need to delete anything
You can do everything with std::string, that you do with char *.
I need to pass a char pointer to function, then change the value that it points to inside the function and print values outside the function.
The problem I have is that I'm losing it when I leave function and try to print it outside. What can I do to avoid this?
This is an code example:
char array[] = "Bada boom";
char *pText = array;
reverseText(pText);
cout << (pText);
cout should print
moob adaB
When I print inside the function, everything is fine(it prints reversed).
My task is to print It out outside the function (as you can see in a 4th line of code)
This is the full of code which have the bug (printing inside func works, outside didn't work)
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
char reverseText(char *text);
int main(){
char array[] = "Bada boom";
char *pTekst = array;
reverseText(pTekst);
cout << (pTekst); //in here it doesn't work
}
char reverseText(char *text){
char befRev[100]; int lenght=-1;
/*until *text doesn't meet '\0' */
for(int i=0;*text!='\0';i++){
befRev[i]=(*text);
text++;
lenght++;
}
/*reversing*/
int j=0;
for(int i=lenght;i>=0;i--){
*(text+j)=befRev[i];
j++;
}
for(int i=0;i<=lenght;i++) //in here it does print the right value
cout << text[i];
};
Just re-arrange the array in-place. The pointer itself doesn't need to change:
#include <cstring>
#include <algorithm>
void reverseText(char* array)
{
auto len = std::strlen(array);
std::reverse(array, array+len);
}
int main()
{
char array[] = "Bada boom";
char *pText = array;
reverseText(pText);
std::cout << pText << std::endl;
}
Output:
moob adaB
If you really wanted to provide a pointer that points to a different address to the caller, you could simply return it:
char* foo(char* stuff)
{
char* tmp = ....;
...
// do some stuff
...
return tmp;
}
Alternatively, you could pass the pointer by reference, but the intent is less clear than in the previous version:
void foo(char*& stuff)
{
stuff = something_else;
}
But in both cases, you must make absolutely sure the thing the new pointer points to is valid outside of the function. This might require some dynamic memory allocation. For your case, it seems the best and simplest option is to re-arrange the array in place.
To answer your question, you have an error in logic. Notice that in your first loop in reverseText you increment the local pointer text. In your second loop you did not reset text to it's original value so beforeRev is being copied over starting at location text+offset.
If you were to look at pText on return from call to reverseText you would find it contains:
"Bada boom\0moob adaB"
Your reverseText should be renamed palindrome :)
This is pretty straightforward. Some points to note:
An array decays to a pointer when you pass it to a function.
You are passing in a null terminated string. So the length of the char array you are passing in is the length of the string (including white space) +1.
Because you are using a pointer there is no need to assign a temp variable to hold everything.
Here is some code in C that is easy to translate to C++. Working out the actual reverse algorithm is left for you as an exercise.
#include<stdio.h>
void reverseText(char* text)
{
// Hint: It can be done in one loop!
int i;
for(i = 0; i < 9; i++)
{
// Your algorithm to reverse the text. I'm not doing it for you! ;)
*(text + i) = 'r';
}
}
int main()
{
char array[] = "Bada boom";
reverseText(array);
printf("The text reversed: %s\n", array);
return 0;
}
My final code:
#include <iostream>
void reverseText(char* text){
int length=-1; char tmp;
/*Length = sign from 0 to 8 without counting explicit NUL terminator*/
for(int i=0;*(text+i)!='\0';i++){
length++;
}
int j=0; int i=length;
while(j<i){
tmp=*(text+j); //tmp=first
*(text+j)=*(text+i); //first=last
*(text+i)=tmp; //last=tmp
j++;
i--;
}
}
int main(){
char array[] = "Bada boom";
char *pText = array;
reverseText(pText);
std::cout << pText;
}
I should have read more about pointers before I started this exercise.
You can either return a pointer or pass a pointer to pointer as a function argument.
//pointer to pointer
void reverseText(char** textPtr) {
char* newText = ...; //initialize;
...
*textPtr = newText; //assign newText
}
//return pointer
char* reverseText(char* text) {
char* newText = ...; //initialize
return newText;
}
Remember that if you allocate memory in this function you must do it dynamically (with new or malloc) and you have to free it afterwards (with delete or free respectively). Memory allocation in a function like this is probably a bad practice and should be avoided.
Just start learning C++ and trying to make a simple function that do substring with following code:
char* substring(int start, int end, const char* target)
{
size_t length = strlen(target);
char result[(length + 1)];
int iterator = 0;
for(int i = start; i < length; i++)
{
if(i <= end)
{
result[iterator] = target[i];
iterator++;
}
}
result[iterator] = '\0';
cout << result << endl; // This give correct result "aya";
return result;
}
When I use these function on main method:
int main(int argc, char** argv) {
char some_text[] = "Saya Makan";
char* sub = substring(1, 3, some_text); // No string returned
cout << "Substring Result is: " << sub;
return 0;
}
It give output is like this:
aya
Substring Result is:
RUN SUCCESSFUL (total time: 44ms)
As seen on the result, my function isn't returning anything but empty string although a line of code before return, the "result" variable echoed result correctly. Would you please explain to me what is happening inside my function, am I missing something?
You are returning pointer to a local array which is not required to live beyond the function.
You need to make the buffer persist beyond the scope of the function by allocating it dynamically using malloc. And ofcourse remember to free it later.
Oh just noticed its C++.
So drop the idea of malloc and free simply use std::string and not char *.