Write access in char array - c++

everyone!
I'm writing a simple char* string reverse. I get an error with writing access in line src[k] = src[n - k]; when I initialize my input array as a pointer : char * input= "string".
And it is fixed, when I put: char input[] = "string";
Why doesn't it work with a pointer? Thanks a lot!
Here is my code:
void reverse(char* src) {
if (src == NULL) {
return;
}
size_t n = strlen(src) - 1;
char swap;
for (int k = 0; k <= n/2; k++) {
swap = src[k];
src[k] = src[n - k];
src[n - k] = swap;
}
src[n + 1] = '\0';
}

It doesn't work with the pointer because the pointer points to the literal "string". String literals are not modifiable. When you use an array, the literal is copied into the array, which is modifiable.

Related

c++ Split char array without use of any library

I've been running into this weird issue where the split code returns correctly when I printf output inside the function, but will incorrectly return output upon calling it as an instance.
Question: How do I get the correct ouput when calling it as an instance?(see useage bellow)
Here is the code:
typedef struct SplitText
{
int splitLen;
char* splitTxt[100];
char* subTxt(char* text, int index, int len)
{
char subTxt_[1000];
int count = 0;
for (int i = 0; i < 1000; i++)
subTxt_[i] = '\0';
for (int i = index; i < index + len; i++)
subTxt_[count++] = text[i];
return subTxt_;
}
void split(char* text, char sep)
{
char separator[3] = { '<', sep, '>' };
int textLen = strlen(text);
int splitIndex = 0;
int splitCount = 0;
for (int t = 0; t < textLen; t++)
{
if (text[t] == separator[0] && text[t + 1] == separator[1] && text[t + 2] == separator[2])
{
if (splitIndex != 0)
splitIndex += 3;
splitTxt[splitCount] = subTxt(text, splitIndex, t - splitIndex);
splitIndex = t;
//correct output
printf(splitTxt[splitCount]);
printf("\n");
splitCount++;
}
}
splitLen = splitCount;
}
}SplitText;
Useage:
SplitText st;
st.split("testing<=>split<=>function<=>", '=');
for (int i = 0; i < st.splitLen; i++)
{
//incorrect output
printf(st.splitTxt[i]);
printf("\n");
}
printf("--------\n");
This:
char* subTxt(char* text, int index, int len)
{
char subTxt_[1000];
...
return subTxt_;
}
Is undefined behavior. Returning a pointer to a local stack variable (or local array var) is going to result in weird stuff like this happening.
The typical thing that corrupts the contens of that returned pointer is when another function is invoked, the memory occupied by subTxt_ is going to get overwritten with the stack variables of the next function invoked.
Better:
char* subTxt(char* text, int index, int len)
{
char *subTxt = new char[1000];
...
return subTxt_;
}
And then make sure whoever invokes subTxt remembers to delete [] on the returned pointer.
Or just use std::string and be done with it (unless this is an academic exercise).
Also, this is undefined behavior:
for (int t = 0; t < textLen; t++)
{
if (text[t] == separator[0] && text[t + 1] == separator[1] && text[t + 2] == separator[2])
when t == textLen-1, then referencing text[t+2] and text[t+1] is an out of bounds access. Change it to be:
for (int t = 2; t < textLen; t++)
{
if (text[t-2] == separator[0] && text[t -1] == separator[1] && text[t] == separator[2])
And do similar fixups with t within the block as well.
Well you can create a splitstring function instead of a struct/class.
Anyway your code still looks quite "C" like with its fixed size char arrays. This will limit the usability and stability (out-of-bound array bugs).
Strings in C++ are usually of type std::string.
and then C++ has string_view to make views on that string (so no data gets copied, but it also means your string_view is only valid for as long as the string it is viewing lives).
If you don't know the number of substrings in a string up-front, you should not use a fixed size array, but a std::vector (which can resize internally if needed)
This is what a split_string function would look like in current C++, note that the code also shows better what it is doing compared to "C" style programming that show more what you are doing.
std::vector<std::string_view> split_string(std::string_view string, std::string_view delimiters)
{
std::vector<std::string_view> substrings;
if(delimiters.size() == 0ul)
{
substrings.emplace_back(string);
return substrings;
}
auto start_pos = string.find_first_not_of(delimiters);
auto end_pos = start_pos;
auto max_length = string.length();
while(start_pos < max_length)
{
end_pos = std::min(max_length, string.find_first_of(delimiters, start_pos));
if(end_pos != start_pos)
{
substrings.emplace_back(&string[start_pos], end_pos - start_pos);
start_pos = string.find_first_not_of(delimiters, end_pos);
}
}
return substrings;
}
Take a look at std::string_view.
You can avoid allocating memory and it has a built-in substring function.
Just be careful when using printf for printing to console as "%s" will
print the whole string.
See printf documentation.
for(auto view : container_with_string_views)
printf("%.*s, (int)view.size(), view.data());

dynamic allocating memory for char array

having some understanding issues with the next block of code.
#include<iostream>
using namespace std;
int main() {
char *str = "hi";
char *p = new char[strlen(str) + 1];
for (int i = 0; *(str + i); i++)
*(p + i) = *(str + i);
cout << p << endl;
return 0;
}
Here's the result:
hi═¤¤¤¤
When i'm using debugger, i can see that my p points to an array of like 10 or 15 or some other amount of symbols (depends on compilation), so i'm getting extra symbols after "hi". BUT, when i'm using strcpy():
#include<iostream>
using namespace std;
int main() {
char *str = "hi";
char *p = new char[strlen(str) + 1];
strcpy(p, str);
cout << p << endl;
return 0;
}
i'm getting the result:
hi
So, can someone, please, explain to me, why am i getting such a result with the first example of a program and how to rework it to get the result like in the second example.
Thanks in advance.
The answer is in the stopping condition of the loop, i.e. *(str + i):
for (int i = 0 ; *(str + i) ; i++)
*(p + i) = *(str + i);
Note that there is no comparison operator in the expression. When an expression like this is used in a context where a logical condition is required, there is an implicit comparison to zero, i.e. *(str + i) means the same thing as *(str + i) != 0.
Now it should be clear why the string remains unterminated: loop stops when it discovers null terminator, and does not copy it into the destination string.
A slightly more "cryptic" way of doing the same thing would be coupling the comparison with the assignment, the way K&R book did:
for (int i = 0 ; *(p + i) = *(str + i) ; i++)
;
Now the null test happens after the assignment, ensuring that the destination is null-terminated.
You are not adding the terminating null character to p.
Add the line
*(p + i) = '\0';
after the for loop. However, to do that, you have to declare i before the for loop.
int i = 0;
for (i = 0; *(str + i); i++)
*(p + i) = *(str + i);
*(p + i) = '\0';
cout << p << endl;
You forgot to terminate the string in your first exaple with a zero:
#include <cstddef>
#include <iostream>
int main()
{
char const *str = "hi";
std::size_t length = std::strlen(str);
char *p = new char[length + 1];
for (std::size_t i = 0; i < length; ++i)
p[i] = str[i];
str[length] = '\0';
std::cout << p << '\n';
delete[] p;
}
Please mind: String literals are immutable so they should be pointed to by char const*s. The correct type to hold sizes of objects in memory or indexes into them is std::size_t, not int. If you do manual memory management you have to make sure that you free the allocated memory by passing pointers obtained using new to delete and pointers from new[] to delete[].
You shouldn't do memory management manually though. Use containers like std::string or std::vector or at least smart pointers like std::shared_ptr<> or std::unique_ptr<>.

C++ delete[] crash

Program crashes when I'm trying to delete[] an array that was allocated with new[]. I don't see any reasons why this shouldn't work. The array is allocated in method and deleted in the end of it, it's local, everything should be fine, but it's not.
I don't see any exception name that was thrown in debug, that's why I don't know where to dig.
Through debug I see that crash is happening at first delete[] operator.
Without those two deletes no crashes happening, but I don't want something to float in memory.
void Atbash::EncryptString(string t)
{
char *cOriginalString;
char *cEncryptedString;
unsigned int originalStringLength;
unsigned int i;
unsigned short j = 0;
originalString = t;
originalStringLength = originalString.length();
cOriginalString = new char(originalStringLength + 1);
cEncryptedString = new char(originalStringLength + 1);
strcpy_s(cOriginalString, originalStringLength + 1, originalString.c_str());
cOriginalString[originalStringLength] = '\0';
for (i = 0; i < originalStringLength; i++)
{
while (cOriginalString[i] != alphabet[j])
{
j++;
}
cEncryptedString[i] = alphabet[N - j - 2];
j = 0;
}
cEncryptedString[originalStringLength] = '\0';
encryptedString = cEncryptedString;
delete[] cOriginalString;
delete[] cEncryptedString;
}
originalString and encryptedString are of "string" type.
You aren't allocating a char[], just a plain old char. Note that you should be using square braces ([]) and not parenthethis (()):
cOriginalString = new char[originalStringLength + 1];
// Here ------------------^------------------------^
cEncryptedString = new char[originalStringLength + 1];
// And here ---------------^------------------------^
You can process individual characters in a std::string and it will both simplify your code and make it more robust. Here is one possible implementation.
void Atbash::EncryptString(string originalString)
{
encryptedString.clear();
encryptedString.reserve(originalString.size());
for (auto ch:originalString)
{
auto index= alphabet.find(ch);
if (index != npos)
encryptedString += alphabet[N - index - 2];
}
}

meet exc_bad_access errror when operating on c-style string

I'm solving a problem that "replace all spaces in a string with ‘%20’." and I want to operate on the original string instead of create a new string. Here is my code:
void replaceSpaces(char* s, int len) {
int spaceCnt = 0;
for(int i = 0; i < len; ++i) {
if(s[i] == ' '){
++spaceCnt;
}
}
int newlen = len + 2 * spaceCnt;
s[newlen] = '\0';
for(int i = len - 1; i >= 0; --i) {
if(s[i] == ' ') {
s[newlen - 1] = '0';
s[newlen - 2] = '2';
s[newlen - 3] = '%';
newlen -= 3;
} else {
s[newlen - 1] = s[i];
--newlen;
}
}
}
And I have an "thread: exc_bad_access" error in the line s[newlen] = '\0';. I know it's dangerous to operate c-stye string in this way but I don't know how to modify it...
Any explanations or suggestions will be appreciated!
Well, if the original buffer is not long enough to contain the string with replacements, you are accessing memory out of bounds. When you determine the new size, you could do a realloc call to reallocate a suffiecient buffer with the size newlen like *s = realloc(*s, newsize). The only issue is that you need to change the paramter from char* s to char** s so that if realloc moves the memory to another block you update the pointer. And of course, this will work with strings allocated on heap, not local stack strings, since you can't reallocate that.

Deleting pointers from an array

I am fairly new to C++, and this problem I am having has had me stumped for like the last 2 hours. What I am trying to do is create an array of pointers to my class Word objects, so a 2 dimensional array, which is **wordPtrList. Then I need to delete the memory of certain objects in the array and set their pointers to null. I have written a smaller version of what I am trying to accomplish below.
int main()
{
char *cArray;
cArray = new char[4];
int i;
for (i = 0; i < 3; i++)
cArray[i] = 'a';
cArray[i + 1] = '\0'; //cArray is a null terminated string
Word **wordPtrList;
wordPtrList = new Word* [3];
for (i = 0; i < 3; i++)
{
wordPtrList[i] = new Word(cArray);
}
wordPtrList[1]->del();
delete wordPtrList[1];
wordPtrList[1] = '\0';
return 0;
}
class Word
{
private:
char* ptr_;
int len_;
public:
Word(const char* word)
{
len_ = strlen(word);
ptr_ = new char[len_];
strcpy(ptr_, word);
}
~Word()
{
delete [] ptr_;
ptr_ = 0;
}
void del()
{
delete [] ptr_;
ptr_ = 0;
return;
}
};
When I do this however, I get:
Debug Error Heap Corruption Detected after normal block
This is in VS 2010 on Windows 7.
So what I am asking is, how do I delete the memory of my object so that I can set wordPtrList[1] to Null?
You are allocating 4 bytes to cArray (meaning you have access to write to bytes 0 through 3), and then writing to cArray[4]. You also allocate one byte too small in the Word constructor.
char *cArray;
cArray = new char[4];
int i;
for (i = 0; i < 3; i++)
cArray[i] = 'a';
cArray[i] = '\0'; //cArray is a null terminated string - i is now 3
and
Word(const char* word)
{
len_ = strlen(word);
ptr_ = new char[len_ + 1];
strcpy(ptr_, word);
}
should do the trick.
Look at this code:
for (i = 0; i < 3; i++)
cArray[i] = 'a';
cArray[i + 1] = '\0'; //cArray is a null terminated string
The problem is at the last line which is using i+1 as index, which is going out of range, as by the time the loop exits, the value of i is already 3; that means i+1 will become 4 which cannot be a valid index when you've allocated cArray as:
cArray = new char[4]; //taken from your code
The solution is this:
cArray[i] = '\0'; //Now, it is correct. Here i is equal to 3
That is, use i instead of i+1; Or simply use 3.
In C++, you could std::fill instead of manual loop, as:
std::fill(cArray, cArray + 4, 'a'); //done
Even better avoid using char* with new as much as possible, and prefer using std::string instead.
There are two obvious off-by-one errors in the code (when you get out of the first loop what is the value of the variable i? when calling strlen did you remember to consider the space needed by the null terminator?).
Also please note that c strings are not "null terminated", they are "NUL terminated" with uppercase letters and only one "L". NUL is the name of the ASCII control character with all bits set to zero and is represented in C++ with '\0'.
In one place you used NUL character as a null pointer, and while this is technically correct (because of a design bug of the C++ language) it's better to understand that the two concepts are quite different.
As others have said, you're basically accessing an array index which is out of bounds of the array.
I would go with Nathan Wiebe's solution.
In the future, when you have the option to do this, it's recommended to use std::vector<T> as that will allow you to store any type you wish in a dynamically resizeble array. In other words, providing you don't access an index which is outside of the vector's bounds, you could do something like this:
std::vector< char* > str;
for( size_t i = 0; i < str.size(); ++i )
{
str.push_back( 'a pointer to a block of memory consisting of characters' );
}
class Word
{
public:
Word( const char* str )
{
mStrs.push_back( str );
}
~Word( void )
{
for( size_t i = 0; i < mStrs.size(); ++i )
{
if( mStrs[ i ] )
{
delete mStrs[ i ];
mStrs[ i ] = NULL;
}
}
mStrs.clear();
}
private:
void del( size_t index )
{
if( index > mStrs.size() )
{
//error - throw new exception or something
}
delete mStrs[ index ];
}
std::vector< const char* > mStrs;
};