I am passing an string or a char array to a function and swapping them but losing the first char array's value for some reason. Here is my code:
void foo(char* a, char* b){
char* temp;
temp = new char[strlen(a)+1];
strcpy(temp, a);
strcpy(a, b);
strcpy(b, temp);
delete[] temp;
}
So in foo the function gets passed two pointers and the are attempted to be swapped.
Here is the main function. There may be a problem with the passing of the variable, but the compiler did not give me an issue.
int main(){
char a[] = "First";
char b[] = "Last";
std::cout << "A Before: "<< a << "\n";
std::cout << "B Before: " << b << "\n\n";
foo(a, b);
std::cout << "A After: "<< a << "\n";
std::cout << "B After: "<< b << "\n\n";
return 0;
}
The output I am getting is as follows:
A Before: first
B Before: last
A After:
B After: first
Now I have tested the values of the strings while in the function during the strcpy's and turns empty after the final strcpy, which means, or at lest I think, that the problem lies within the pointers to the original variables. It could be a chain reaction type of thing where all of the pointers are pointing to the "a" and it confuses the program.
Any help would be appreciated, also why this is happening would be very useful as well.
Because your string a is longer than b.So strcpy does not work as you expect in line:
strcpy(b, temp);
Tips:
Use strncpy instead of strcpy
Use c++ Strings instead of the c style string.Then you can swap
them with a.swap(b);
The problem is your array sizes happen to be such that you are clobbering your stack; fortunately, for you, the effect is simply to place a null byte in the first character of a, making it an empty string.
#include <iostream>
#include <string.h>
void foo(char* a, char* b){
char* temp = new char[strlen(a)+1];
strcpy(temp, a);
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
strcpy(a, b);
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
strcpy(b, temp); // this copies 6 bytes to b, which puts a 0 in the first byte of a.
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
delete[] temp;
}
int main() {
char a[] = "First";
char b[] = "Last";
std::cout << "a size is " << sizeof(a) << std::endl;
std::cout << "b size is " << sizeof(b) << std::endl;
std::cout << "address of a[0] is " << (void*)&a[0] << std::endl;
std::cout << "address of b[0] is " << (void*)&b[0] << std::endl;
foo(a, b);
std::cout << "A After: "<< a << "\n";
std::cout << "B After: "<< b << "\n\n";
}
http://ideone.com/fDvnnH
a size is 6
b size is 5
address of a[0] is 0xbfec5caa
address of b[0] is 0xbfec5ca5
temp = First a = First b = Last
temp = First a = Last b = Last
temp = First a = b = First
A After:
B After: First
You might want to investigate std::string or look at using std::strncpy
Related
I wrote this simple c++ program and I got some strange results that I don't understand (results are described in the line comments)
int arr[3] {1, 2, 3};
int* p{ nullptr };
p = arr;
std::cout << p[0] << " " << p[1] << " " << p[2]; // prints 1 2 3, OK
p = arr;
std::cout << *(p++) << " " << *(p++) << " " << *(p); // prints 2 1 3 ??
p = arr;
std::cout << *p << " " << *(++p) << " " << *(++p); // prints 3 3 3 ??
p = arr;
std::cout << *p << " "; ++p;
std::cout << *p << " "; ++p;
std::cout << *p; // prints 1 2 3, OK
it seems that the pointer increments along a std::cout concatenation don't work.
What's wrong in my idea?
I supposed it should have worked.
best
final edit: I was using c++14, I switched to c++20 and now it works properly
thank you everybody!
int* p{ nullptr };
std::cout << p[0] << " " << p[1] << " " << p[2];
This is Undefined Behavior, as you are dereferencing nullptr, p does not point at valid memory yet.
p = arr;
std::cout << p[0] << " " << p[1] << " " << p[2];
This is well-defined behavior. p points at valid memory, is always incremented before dereferenced, and is incremented in a deterministic and valid manner. This is the same as if you had written the following instead:
std::cout << *(p+0) << " " << *(p+1) << " " << *(p+2);
p = arr;
std::cout << *(p++) << " " << *(p++) << " " << *(p);
p = arr;
std::cout << *p << " " << *(++p) << " " << *(++p);
Both of these are Undefined Behavior prior to C++17, because the order in which chained operator<< calls are evaluated is not guaranteed in earlier versions, the compiler is free to evaluate them in whatever order it wants. This is no longer the case in C++17 onward.
p = arr;
std::cout << *p << " "; ++p;
std::cout << *p << " "; ++p;
std::cout << *p;
This is well-defined behavior. p points at valid memory, is always dereferenced before incremented, and is incremented in a deterministic and valid manner.
I'm working with C++ pointers and I've encountered something curious.
If I reset a pointer to itself using "b = (int*)&b;", I expected the deferenced output to be the memory address of itself -- since it was pointing to itself.
So I thought *b would be "0x7ffea00819b0", but it's some strange numeric value.
But this isn't the case. The alternate value I get is confusing.
Here is my output:
Value of a = 10
Address of a = 0x7ffea00819ac
Value of b = 0x7ffea00819b0
Address of b = 0x7ffea00819b0
Dereference of b = -1610081872
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a = 10;
int *b = &a;
b = (int*)&b;
cout << "Value of a = " << a << endl;
cout << "Address of a = " << &a << endl << endl;
cout << "Value of b = " << b << endl;
cout << "Address of b = " << &b << endl;
cout << "Dereference of b = " << *b << endl;
}
Changing the type to unsigned long int fixed the issue.
Thank you all!
#include <iostream>
#include <type_traits>
using namespace std;
int main(int argc, char** argv)
{
unsigned long int a = 10;
unsigned long int *b = &a;
b = (unsigned long int*)&b;
cout << "Value of a = " << a << endl;
cout << "Address of a = " << &a << endl << endl;
cout << sizeof b << endl;
cout << sizeof *b << endl;
//static_assert(sizeof b == sizeof *b);
cout << "Value of b = " << b << endl;
cout << "Address of b = " << &b << endl;
cout << "Dereference of b = " << hex << "0x" << *b << endl;
}
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{move(a)};
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = move(b);
b = move(tmp);
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
void swap_no_move(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{ a };
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = b;
b = tmp;
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
int main() {
int a = 10;
int b = 5;
swap(a, b);
cout << endl;
int c = 10;
int d = 5;
swap_no_move(c, d);
cin.get();
return 0;
}
I have two swap functions: swap and swap_no_move. According to what I read from the book, there should be no "copy" in function swap which means the address of tmp should be the same for tmp and an in function swap. However, the output I got shows there is no difference between these two functions, did I do something wrong?
The definition
int tmp{move(a)};
doesn't move the reference or the variable a itself. It creates a brand new variable tmp which the compiler allocates space for. Then the value of a is moved into tmp.
And since moving int values can't really be done, it's exactly the same as
int tmp = a;
char arr[2][6] = { "hello", "foo" };
cout << arr[0] << " or " << *(arr) << endl;// prints "hello"
cout << arr[1] << " or " << *(arr + 1) << endl; // prints "foo"
cout << arr << endl; // prints an address of "hello" (and 'h')
cout << arr + 1 << endl; //prints an address of "foo" (and 'f')
cout << arr[0][1] << endl; // prints 'e'
cout << &arr[0][1] << endl; // prints "ello"
So, I'd like to print an address of, say, 'e' in "hello". How do I do that?
I know if I was dealing with an array of any other type, &arr[0][1] would do the job, however with all of this cout char(array) overloads I'm not sure if it's even possible?
There is a overload for operator <<(std::ostream&, const char*) which prints c-string (nul-terminated string). You have to cast to void* to print address in such case:
std::cout << static_cast<const void*>(&arr[0][1]) << std::endl;
Yes, it's possible, but only by bypassing the IOstream special handling for char* (which assumes a C-string and formats its output accordingly):
cout << (void*)&arr[0][1] << endl;
// ^^^^^^^
This really has nothing to do with 2D arrays. Here's a much simpler example:
#include <iostream>
int main()
{
const char* str = "hi";
std::cout << &str[0] << '\n'; // "hi"
std::cout << (void*)&str[0] << '\n'; // some address
}
(live demo)
The address of 'ello' and 'e' in 'hello' are the same. An array is just a sequential chunk of memory and its address is the address of its first value, that's why the array 'ello' has the same address as 'e'.
I'm trying to do a C-style string copy but something is not working right. What am I doing wrong?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char string_a[20]="Good day!";
char string_b[30]="Hi!";
int i=0;
cout << "string a: " << string_a << endl;
cout << "string b: " << string_b << endl;
while (*string_a++ = *string_b++) {
cout << ++i << endl;
}
cout << "string a: " << string_a << endl;
cout << "string b: " << string_b << endl;
return 0;
}
You cannot do:
string_a++
if string_a is defined as an array. That only works for pointers and arrays decay to pointers only in specific circumstances.
If you change:
while (*string_a++ = *string_b++) {
cout << ++i << endl;
}
into:
char *pa = string_a, *pb = string_b; // a "specific circumstance" :-)
while (*pa++ = *pb++) {
cout << ++i << endl;
}
then it will work just fine, outputting:
string a: Good day!
string b: Hi!
1
2
3
string a: Hi!
string b: Hi!