pointer addresses--help.

hello,
I'm exercising on to how manipulate with pointer addresses,
this piece of code work's fine and it show's the values when exiting function named "f", but I get an error at the end... that error line is commented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
void f(char**, int**);

int main() {
	char* pass = nullptr;
	int* net = nullptr;
	f(&pass, &net);
	cout << pass << endl << *net;
	delete[] pass; //error is here (no error if comment this line out!!)
	delete net;
	return 0;
}
void f(char** x, int** y) {
	char* a = new char[10];
	int* b  = new int(99);
	a = "test";
	*x = a;
	*y = b;
}


here is output:
debug assertion failed!
BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


as I said...
if line 10 is commented out no error at all,
but memory leak is a payload then lol.
please help.
delete pass;
This is not how you assign a char*

a = "test";

Try using the strcpy function defined in the <cstring> header. You may have access to strcpy simply by including iostream.
@ciphermagi
I've try that but no success :/

@shacktar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
void f(char**, int**);

int main() {
	char* pass = nullptr;
	int* net = nullptr;
	f(&pass, &net);
	cout << pass << endl << *net << endl;
	delete[] pass;
	delete net;
	system("pause");
	return 0;
}
void f(char** x, int** y) {
	char* a = new char[10];
	int* b  = new int(99);
	//a = "test";
	*x = a;
	*y = b;
}


now as it is here work's ...
what if I don't wanna use any of standard functions?
I mean how to initialize it just like that?
maybe using some for loop... I'll try that...
You can't just stuff chars into a, you have to put them into the address of a.
1
2
3
4
5
6
7
8
9
void f(char** x, int** y) {
	char* a = new char[10];
	int* b  = new int(99);
	cout << "input data: ";
	cin >> a;   //option 1
	strcpy(a, "test");  //option  2
	*x = a;
	*y = b;
}


I think you can't pass values into address of a pointer !?
or how do u mean into address of a?
a is a pointer. You can't put values into a pointer, it doesn't make sense. You have to put values into the memory that's pointed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
void f(char**, int**);

int main() {
	char* pass = nullptr;
	int* net = nullptr;
	f(&pass, &net); // see comments in f()
	cout << pass << endl << *net;
	delete[] pass; // you cannot delete pass since it now points to "test" which
                       // you did not allocate on the heap.
                       // the new char[10]; below is a memory leak
	delete net;
	return 0;
}
void f(char** x, int** y) { // x points to pass
	char* a = new char[10];
	int* b  = new int(99);
	a = "test";
	*x = a; // now pass points to "test"
	*y = b;
}



Edit: clarified (hopefully)
Last edited on
no I still think you've got issues here... when you say a = "test"; what do you think that is doing?
char* a = new char[10]; //a point to the memory that allocated by new.
a = "test"; //now,a is changed, a point to a string constant.
//and the memory that allocated by new was dropped.

delete[] pass; //because pass are equal to a,so you are trying to release a string constant memory.That's why the program fail.
Last edited on
Topic archived. No new replies allowed.