I read this sample code in a book. I can't figure out why this part of the following sample code's function declaration is necessary:
while (i <= n)
p[i++] = '\0'; // set rest of string to '\0'
Here is the whole code:
#include <iostream>
const int ArSize = 80;
char * left(const char * str, int n = 1);
int main()
{
using namespace std;
char sample[ArSize];
cout << "Enter a string:\n";
cin.get(sample,ArSize);
char *ps = left(sample, 4);
cout << ps << endl;
delete [] ps; // free old string
ps = left(sample);
cout << ps << endl;
delete [] ps; // free new string
return 0;
}
// This function returns a pointer to a new string
// consisting of the first n characters in the str string.
char * left(const char * str, int n)
{
if(n < 0)
n = 0;
char * p = new char[n+1];
int i;
for (i = 0; i < n && str[i]; i++)
p[i] = str[i]; // copy characters
while (i <= n)
p[i++] = '\0'; // set rest of string to '\0'
return p;
}
I ran the code after I erased it and there was no problem.
The loop is unnecessary. Null-terminated strings end at the first null byte. If more memory was allocated than the actual string needs, it does not matter what’s in those extra bytes. All non-broken C-string handling code stops at the first null terminator. All that’s required is a single
p[i] = '\0';
after the for loop. However, that one null byte is mandatory. C-string functions depend on it and will happily overrun the allocated memory if it’s missing. Essentially they’ll (try to) keep going until they stumble upon the next null byte in memory. If that is past the allocated memory it causes undefined behaviour, resulting in a crash if you’re lucky; or corrupted data if you’re less lucky.
That said: Throw away that book yesterday. The code is a catastrophe from the first to the last line. It barely qualifies as C++. Most of it is plain C. And even as C code it’s highly questionable.
Why to avoid using namespace std. #vol7ron pointed out in the comments that the major complaint is against using namespace std in headers. Here it’s used inside a function in a .cpp file, which lessens the impact significantly. Although in my opinion it is still worth avoiding. If you don’t know the implementation of your standard library in depth, you don’t really have an idea about all the symbols you pull into your scope. If you need it for readability, pulling in specific symbols (e.g. using std::cout;) is a better choice. Also, I’m confident I’m not alone in kind of expecting the std:: prefix. For example, std::string is what I expect to see. string looks slightly off. There’s always a lingering doubt that it might not be the std library string, but a custom string type. So, including the prefix can benefit readability as well.
Why all the C-string pain? We’ve had std::string for a while now …
Copying characters in a loop? Seriously? That’s what std::strcpy() is for.
Raw new and delete everywhere: error prone because you have to keep track of the new/delete pairs manually to avoid memory leaks.
Even worse: asymmetric owning raw pointers. left() allocates and returns a pointer; and it’s the caller’s responsibility to delete it. It doesn’t get more error prone than that.
… And these are only the problems that stick out on first glance.
What that piece of code should look like:
#include <iostream>
#include <string>
std::string left(const std::string& str, std::size_t len = 1);
int main()
{
// getline can fail. If that happens we get an empty string.
std::string sample;
std::getline(std::cin, sample);
auto ps = left(sample, 4);
std::cout << ps << '\n';
ps = left(sample);
std::cout << ps << '\n';
return 0;
}
// `len` may be longer than the string. In that case a copy
// of the complete input string is returned.
std::string left(const std::string& str, std::size_t len)
{
return str.substr(0, len);
}
Related
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'm a newbie, at both coding and English. This is my code:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n = 1;
char *a = new char[n], c = getchar();
while ((c != EOF) || (c != '\n'))
{
a[n-1] = c;
c = getchar();
n++;
}
cout << a;
delete[] a;
return 0;
}
I'm learning about dynamic memory allocation. The problem is to input a string whose length is unknown. My idea is to read the string character by character and stop when it reaches EOF or \n. Could you please point out the error?
And another question: I was told that new selects a memory block of the specified size. So what happens if there wasn't a large enough block?
Thanks for helping!
[I know adhering to best practices and methods available is the "good"
thing to do, but the OP should know why the current code doesn't work
and the other answers here do not seem to be answering that]
First, you should use C++ string class for this.
Second, if you are wondering why your current code is not working, it is because:
The condition inside while is wrong. It says, "Execute this block if the character is not \n or it is not EOF". So even if you press enter (c is '\n'), this block will still execute because "c is not EOF", and vice-versa.
You are allocating only 1 byte worth of memory to your char*, which is clearly not enough.
This should fairly replicate what you want, but the memory allocated is static and the string has to be limited.
int main()
{
int n=1;
char *a = new char[100],c=getchar();
while(true)
{
if(c == '\n' || c == EOF){
break;
}
a[n-1]=c;
c=getchar();
n++;
}
cout << a;
delete[] a;
return 0;
}
First of all, there is no need to use char* and new char[n]. You can use std::string.
Then you have to ask yourself:
Can the string contain whitespace characters?
Can the string span multiple lines?
If it can span multiple lines, how many lines does it span?
If the answer to the first question is "No", you can use:
std::string s;
cin >> s;
If the answer to the first question is "Yes" and the answer to the second question is "No", then you can use:
std::string s;
getline(cin, s);
If the answer to the second question is "Yes", the answer gets more complicated.
Then, you need to find answers to more questions?
Is the number of lines hard coded?
If it is not hard coded, how does the program get that number from the user?
Based on the answers to those questions, your code will vary.
#include <iostream>
#include <string>
int main() {
std::string line;
// first argument is the stream from whence the line comes.
// will read to newline or EOF
std::getline(std::cin, line);
}
Considering the restrictions of your task (no std::string, no std::vector, dynamic memory allocation), I'll try to give you a modified but working version of your code.
My idea is read the string word my word and stop when it reach EOF or
\n. Could you please point out the error?
As molbdnilo pointed out, (c!=EOF) || (c!='\n') is always true, so your loop will never end.
As mah noticed, your a buffer is only 1 char long and you don't check for the overflow, besides, You forgot to add the null terminator at the end of it.
Your second question is about what happens when new can't allocate enough memory. It throws an exception which your program should catch to manage the situation, but the best thing (not the only one actually, maybe the easiest) you can do is to terminate your program.
This is an example of how to accomplish your task given the above mentioned limitations:
#include <iostream>
using namespace std;
int main()
{
const int INITIAL_SIZE = 8;
// The following block of code could rise an exception.
try
{
int n = 0;
char c;
// Allocate some memory to store the null terminated array of chars.
char *a = new char[INITIAL_SIZE];
// what happens if new fails? It throws an exception of type std::bad_alloc
// So you better catch it.
int allocated = INITIAL_SIZE;
// read a charachter from stdin. If EOF exit loop
while( cin.get(c) )
{
// If it's a newline or a carriage return stop.
if( '\n' == c || '\r' == c )
//^ Note that ^^^ putting the literals first helps avoiding common
// error like using "=" instead of "==" in conditions.
break;
// If the array is full, it's time to reallocate it.
if ( n == allocated )
{
// There are better alternatives, of course, but I don't know which library
// you are allowed to use, so I have to assume none.
// Allocate a bigger array. The growing strategy may be different.
allocated += 2 + allocated / 2;
char *b = new char[allocated];
// Copy the old one in the new one (again, you could use std::copy).
for ( int i = 0; i < n; ++i )
{
b[i] = a[i];
}
// Release the memory handled by the old one...
delete[] a;
// but keep using the same pointer. Just remember not to delete 'b'
// so that 'a' always points to allocated memory.
a = b;
}
a[n] = c;
// A new character has been succesfuly added.
++n;
}
// Now, before using a, we have to add the null terminator.
a[n] = '\0';
// Note that a doesn't contain the '\n'.
cout << a << '\n';
// Clean up.
delete[] a;
// Normal program termination.
return 0;
}
// If 'new' fails to allocate memory a std::bad_alloc exception is thrown.
catch ( const exception &e )
{
cout << "Exception caught: " << e.what() << "\nProgram terminated.\n";
return -1;
}
}
I'm making a virtual machine in C++. I have loaded in the contents of a file as a string. I pass this string to a function of type int*, but the problem is the string variable containing the contents of the file seems to be empty because when I try to use cout << file << endl; I get nothing.
Here is the file in question:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class reedoovm {
private:
string filedata;
string instruction;
string file;
int instr;
int instructionCount;
int instructionPointer;
public:
int load_program(string filename) {
ifstream rdfile(filename);
while(rdfile >> instruction) { /* Get each instruction */
filedata += instruction; /* Append the instruction to filedata */
filedata += ","; /* Append a comma to separate each instruction */
instructionCount++;
}
rdfile.close(); /* Close the file */
return instructionCount; /* Return the filedata */
}
int *instrToArr(string file) {
//file = "02,0022,00E1,0022,00,04,73";
cout << file << endl;
stringstream hextoint;
unsigned int value;
string s = file; /* store fconv in a variable "s" */
string delimiter = ","; /* The delimiter */
size_t pos = 0;
string token;
int i = 0;
int inst;
static int* instarray;
instarray = (int*) calloc(instructionCount,sizeof(int));
while ((pos = s.find(delimiter)) != string::npos) { /* Convert hex instructions to decimal */
token = s.substr(0, pos);
stringstream hextoint(token);
hextoint >> hex >> value;
if (i < instructionCount) {
instarray[i] = value;
cout << instarray[i] << endl;
i++;
}
s.erase(0, pos + delimiter.length());
}
return instarray;
}
int getNextIntruction(string s) {
int *instruction = instrToArr(s);
cout << *instruction << endl;
return 0;
}
void run_program(string s) {
int loop = 1;
while (loop) {
instr = getNextIntruction(s);
loop = 0;
}
}
void execute_program(string s) {
file = load_program(s);
int * arr = instrToArr(file);
//cout << arr << endl;
//run_program(s);
}
};
int main(int argc, char* argv[]) {
reedoovm rd;
rd.execute_program(argv[1]);
return 0;
}
The function causing the problem is int *instrToArr(string file) {. I don't know why all of a sudden the file variable is empty.
Your code has many issues, but the one that is bugging you is probably
file = loadProgram(s);
because loadProgram has been defined as returning an integer (the number of instructions) and not a string, but you're assigning it to a string.
For what I'd call a design bug of C++ assigning an integer to a string is a perfectly legal instruction and means that the string will have one character with the value of the integer.
Officially the reason for accepting assignment from an integers is that it was thought that it could be useful to write
str += chr; // Adds the char at the end
where str is a string and chr a char. By extension if += was legal then it was thought that also assignment should be legal too (a logical jump I don't agree with in this specific case).
chars however in C++ are numbers and integers (or even doubles) can be converted implicitly to a char without any warning or any error. So it's for example also legal:
std::string s;
s = 3.141592654;
Other issues I can see in your code are:
1. instructionCount is not initialized
In C++ you must always initialize native type members (e.g. integers, doubles) in class instances in the constructor. The default constructor won't do it for you. The result is that when allocating the class instance those members will have random values and you don't want that. Official explanation for this rule is that initializing members that won't be access may penalize performance, if the programmer wants to pay for initialization then it has to write the initialization.
2. instrToArr returns a pointer to a local static variable
That variable that is however allocated each time the function is called thus leaking memory at each call if the caller doesn't take care of deallocation.
Note that in C++ writing:
static int * instarray = (int *)calloc(...);
is not the same as writing:
static int * instarray;
instarray = (int *)calloc(...);
because in the first case the allocation is done only once (the first time the code reaches that instruction) while in the second case the allocation is done every time.
3. You are using calloc
Your code is allocation a variable-sized array using calloc and this, while not a bad idea in absolute, requires very careful handling to avoid leaks or other errors (for example memory allocated with calloc must be freed with free and not with delete[] but the compiler cannot help the programmer remembering what was allocated with one or with the other method (new[]).
MUCH better unless there are very specific reasons to play with naked pointers and implicit sizes is to use std::vector for variable-sized arrays.
4. You seem to want hex -> int conversion
... but your code does nothing to do it. Unfortunately input parsing is a sad story in C++ and I, as one, prefer to use old c <stdio.h> functions for input and especially for output (where formatting in C++ is just too painful).
5. your getNextInstruction always returns 0
Nothing remains of the processing of instrToArr and also the array returned is just dropped on the floor after sending the address on output.
This means just leaking memory at every iteration.
6. your run_program just loops once
... thus at least the naming is confusing (there are no real loops).
7. your program doesn't do any kind of checking in main
If someone calls the program passing no arguments (a quite common case) then something bad is going to happen.
I think in load_program() instead of:
return instructionCount;
you meant:
return filedata;
And change the return type of load_program() to string
I suppose you have a typo
int * arr = instrToArr(file)
instead of
int * arr = instrToArr(filedata)
After trying for about 1 hour, my code didn't work because of this:
void s_s(string const& s, char data[10])
{
for (int i = 0; i < 10; i++)
data[i] = s[i];
}
int main()
{
string ss = "1234567890";
char data[10];
s_s("1234567890", data);
cout << data << endl;//why junk
}
I simply don't understand why the cout displays junk after the char array. Can someone please explain why and how to solve it?
You need to null terminate your char array.
std::cout.operator<<(char*) uses \0 to know where to stop.
Your char[] decays to char* by the way.
Look here.
As already mentioned you want to NUL terminate your array, but here's something else to consider:
If s is your source string, then you want to loop to s.size(), so that you don't loop past the size of your source string.
void s_s(std::string const& s, char data[20])
{
for (unsigned int i = 0; i < s.size(); i++)
data[i] = s[i];
data[s.size()] = '\0';
}
Alternatively, you can try this:
std::copy(ss.begin(), ss.begin()+ss.size(),
data);
data[ss.size()] = '\0';
std::cout << data << std::endl;
You have ONLY allocated 10 bytes for data
The string is actually 11 bytes since there is an implied '\0' at the end
At a minimum you should increase the size of data to 11, and change your loop to copy the '\0' as well
The function std::ostream::operator<< that you are trying to use in the last line of the main will take your char array as a pointer and will print every char until the null sentinel character is found (the character is \0).
This sentinel character is generally generated for you in statements where a C-string literal is defined:
char s[] = "123";
In the above example sizeof(s) is 4 because the actual characters stored are:
'1', '2', '3', '\0'
The last character is fundamental in tasks that require to loop on every char of a const char* string, because the condition for the loop to terminate, is that the \0 must be read.
In your example the "junk" that you see are the bytes following the 0 char byte in the memory (interpreted as char). This behavior is clearly undefined and can potentially lead the program to crash.
One solution is to obviously add the \0 char at the end of the char array (of course fixing the size).
The best solution, though, is to never use const char* for strings at all. You are correctly using std::string in your example, which will prevent this kind of problems and many others.
If you ever need a const char* (for C APIs for example) you can always use std::string::c_str and retrieve the C string version of the std::string.
Your example could be rewritten to:
int main(int, char*[]) {
std::string ss = "1234567890";
const char* data = ss.c_str();
std::cout << data << std::endl;
}
(in this particular instance, a version of std::ostream::operator<< that takes a std::string is already defined, so you don't even need data at all)
EDIT: I don't want to delete the post because I have learned a lot very quickly from it and it might do someone else good, but there is no need for anyone else to spend time answering or viewing this question. The problems were in my programming fundamentals, and that is something that just can't be fixed in a quick response. To all who posted, thanks for the help, quite humbling!
Hey all, I'm working on building my own string class with very basic functionality. I am having difficulty understand what is going on with the basic class that I have define, and believe there is some sort of error dealing with the scope occurring. When I try to view the objects I created, all the fields are described as (obviously bad pointer). Also, if I make the data fields public or build an accessor method, the program crashes. For some reason the pointer for the object is 0xccccccccc which points to no where.
How can a I fix this? Any help/comments are much appreciated.
//This is a custom string class, so far the only functions are
//constructing and appending
#include<iostream>
using namespace std;
class MyString1
{
public:
MyString1()
{
//no arg constructor
char *string;
string = new char[0];
string[0] ='\0';
std::cout << string;
size = 1;
}
//constructor receives pointer to character array
MyString1(char* chars)
{
int index = 0;
//Determine the length of the array
while (chars[index] != NULL)
index++;
//Allocate dynamic memory on the heap
char *string;
string = new char[index+1];
//Copy the contents of the array pointed by chars into string, the char array of the object
for (int ii = 0; ii < index; ii++)
string[ii] = chars[ii];
string[index+1] = '\0';
size = index+1;
}
MyString1 append(MyString1 s)
{
//determine new size of the appended array and allocate memory
int newsize = s.size + size;
MyString1 MyString2;
char *newstring;
newstring = new char[newsize+1];
int index = 0;
//load the first string into the array
for (int ii = 0; ii < size; ii++)
{
newstring[ii] = string[ii];
index++;
}
for(int jj = 0; jj < s.size; jj++, ii++)
{
newstring[ii] = s.string[jj++];
index++;
}
//null terminate
newstring[newsize+1] = '\0';
delete string;
//generate the object for return
MyString2.string=newstring;
MyString2.size=newsize;
return MyString2;
}
private:
char *string;
int size;
};
int main()
{
MyString1 string1;
MyString1 string2("Hello There");
MyString1 string3("Buddy");
string2.append(string3);
return 0;
}
EDIT:
Thank you everyone so far who has responded and dealing with my massive lack of understanding of this topic. I'll begin to work with all of the answers, but thanks again for the good responses, sorry my question is vague, but there isn't really a specific error, but more of a lack of understanding of arrays and classes.
Here's just the mistakes from the first constructor.
MyString1()
{
//no arg constructor
char *string; //defines local variable that hides the member by that name
string = new char[0]; //sort of meaningless
string[0] ='\0'; //not enough room for that (out-of-bounds)
std::cout << string;
size = 1; //I don't think you should count null as part of the string
}
Similar mistakes elsewhere.
Also you should pass parameters in a more careful way.
MyString1(const char* source); //note const
MyString1 append(const MyString1& what); //note const and reference
If the latter is correct, also depends on what it is supposed to do. Based on std::string the expected result would be:
MyString1 a("Hello "), b("world");
a.append(b);
assert(a == "Hello world");
Some comments on your code:
MyString1()
{
//no arg constructor
Perhaps your instruction requires it, but in general this is the kind of comment that's worse than useless. Comments should tell the reader things that aren't obvious from the first glance at the code.
char *string;
string = new char[0];
string[0] ='\0';
This invokes undefined behavior. Calling new with zero elements is allowed, but you can't dereference what it returns (it may return a null pointer, or it may return a non-null pointer that doesn't refer to any storage). In most cases, you're better off just setting the pointer to NULL.
std::cout << string;
What's the point of writing out an empty string?
size = 1;
The string is empty so by normal figuring, the size is zero.
//constructor receives pointer to character array
Still useless.
MyString1(char* chars)
Since you aren't (or shouldn't be anyway) planning to modify the input data, this parameter should be char const *.
{
int index = 0;
//Determine the length of the array
while (chars[index] != NULL)
index++;
While this works, "NULL" should really be reserved for use as a pointer, at least IMO. I'd write it something like:
while (chars[index] != '\0')
++index;
Unless you're using the previous value, prefer pre-increment to post-increment.
//Allocate dynamic memory on the heap
As opposed to allocating static memory on the heap?
MyString1 MyString2;
Using the same naming convention for types and variables is confusing.
while (string[index] != NULL)
Same comment about NULL as previously applies here.
MyString1 append(MyString1 s)
IMO, the whole idea of this function is just plain wrong -- if you have a string, and ask this to append something to your string, it destroys your original string, and (worse) leaves it in an unusable state -- when you get around to adding a destructor that frees the memory owned by the string, it's going to cause double-deletion of the storage of a string that was the subject (victim?) of having append called on it.
I'd consider writing a private "copy" function, and using that in the implementations of some (most?) of what you've shown here.
As a bit of more general advice, I'd consider a couple more possibilities: first of all, instead of always allocating exactly the amount of space necessary for a string, I'd consider rounding the allocation to (say) a power of two. Second, if you want your string class to work well, you might consider implementing the "short string optimization". This consists of allocating space for a short string (e.g. 20 characters) in the body of the string object itself. Since many strings tend to be relatively short, this can improve speed (and reduce heap fragmentation and such) considerably by avoiding doing a heap allocation if the string is short.
index doesn't start at 0 in the second while loop in your append function. You should probably be using for loops. Oh, and you're using the wrong form of delete. You should be using delete[] string because string is an array.
There are numerous other style issues, and outright errors, but the thing I mentioned first is the basic error you were encountering.
I would write the append function in this way:
void append(MyString1 s)
{
//determine new size of the appended array and allocate memory
int newsize = s.size + size;
char *newstring = new char[newsize+1];
int destindex = 0;
for (int index = 0; index < size; ++index) {
newstring[destindex++] = string[index];
}
for (int index = 0; index < s.size; ++index) {
newstring[destindex++] = s.string[index];
}
newstring[destindex] = '\0';
delete[] string;
string = newstring;
}