smithss 18 Newbie Poster
int** a = new int*[col]; // works
int** b = new int[][col]; //error cannot convert from 'int (*)[1]' to 'int **
int* c[] = new int[][col]; /error cannot convert from 'int (*)[1]' to 'int *[]'

The first one works, but i get these errors when i use the other 2 forms. Can someone explain these to me. What is wrong in 2, 3 and why is 1 ok !!!

VernonDozier commented: Great question. +18
smithss 18 Newbie Poster

So how do i fix this problem? I obviously don't want uninitialized data instead i wan't the pointer to the properly initialized memory being passed and used and not de-allocated till the thread completes. How do i achieve that?

To tell you more, i basically have to call a class 'A' fn from a thread. There is a 'thread' class which has a static fn, that is the fn being passed in beginthread and i want to pass the 'this' pointer of class 'A' as an argument so that i can call class A functions. This idea i got from c++ code on net.

smithss 18 Newbie Poster

Hi Guys,

i wrote a program that starts another thread. In one case i thought that i have introduced a memory leak and i should get a segmentation fault but i didn't !! That's kind of puzzled me so i decided to post it here.

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;

void  silly( void * );

int main()
{
    cout << "Now in the main() function.\n" << endl;

    int *i = new int(10);
    _beginthread( silly, 0, i );

    delete i;
	cout << "deleted" << endl;
	Sleep( 1000 );
	cout << "didn't crash?" << endl;
}

void  silly( void *arg )
{
	Sleep(100);
	cout << "The silly() function was passed %d\n" << (int*)arg << endl;
}

Output:
Now in the main() function.

deleted
The silly() function was passed %d
00316990
didn't crash?

as you guys can see the silly function is being pointer to a dynamically allocated int and i delete that memory before the silly function prints it. But it still doesn't crash? I was expecting it to give me a dump.

smithss 18 Newbie Poster

Hey,

I was trying to write something to help you and got confused myself by this output. May be someone can take a look at this too. Its all related so I'm not starting a new thread

#include <iostream>
#include <iomanip>

int main()
{
	std::cout << std::setprecision(2) << std::fixed << 12.525 << std::endl;
	std::cout << std::setprecision(2) << std::fixed << 16.525 << std::endl;
}

output:
12.53
16.52

How is it possible that setprecision is rouding 12.525 to 12.53 but 16.525 to 16.52 :-O !!!

smithss 18 Newbie Poster

Hi guys,

I wrote a program which used an external library. i was asked to include a .h file for compiling and link with the '.lib' while linking. I don't understand the reasons behind all this? Can someone explain?

1>What happens when i include the .h and link with the library?
2>What is the cause of the symbol referencing error?
3>and most important what difference does it make to the linking process when i use a .dll instead of a .a?

smithss 18 Newbie Poster

Hi,

Is it possible to find out what all object files were put into an exe? Does it sound like a weird question? Well i have a c++ executable which was a simple program created by linking 2 .obj files. Can i now get this information from the exe which .obj files they were? I have used 'ldd' command to find which '.so's were linked but can't get anything for object files.

smithss 18 Newbie Poster

Hi Narue,

That was a very nice explanation, Thank you. i realize that i can also create a reference to an object on the heap like this..

int main()
{
	a *obj = new a();
	a& b = *obj;	
};
smithss 18 Newbie Poster

Hi Guys...

I have some basic doubts with references and pointers and need some understanding on the behavior of this code

#include <iostream>

using namespace std;

class a
{
	public:
		int getAmount(a&);
	private:
		int amount;
};

int
a::getAmount(a& p)
{
	cout << this << endl;
	cout << p << endl;
	//cout << &p << endl;
	return amount;
}

int main()
{
	a *obj = new a();
	obj->getAmount(*obj);
	//a obj1;
	//obj1.getAmount(obj1);
};

When i'm passing *obj to getAmount, i realize i'm passing the object to it, and the parameter is a&, so my understanding is that 'p' now should be my reference to the object and cout << p, should work. But i get compilation error that operator << is not overloaded for type 'a'(which means that 'p' is the actual object itself). instead cout << &p works and gives me the address. Why is that so?

If from main i use the commented code then both cout << p and cout << &p work the same and give me the address of the object in memory. Thus here 'p' works as a reference to the object.

smithss 18 Newbie Poster

Ok i'm marking it solved. The original problem was solved i guess i'll clear the other doubts as i go along. Thanks a lot for your help, i could write all other programs without any issues, atleast one assignment is done !!! On to #2 .. :)

smithss 18 Newbie Poster

Use of std::strings etc? No?

No, not yet. The assignment is primarily to help us understand char arrays/pointers and their manipulation. I'm sure everyone takes this dusty road before enjoying the comfort of std::strings class :)

smithss 18 Newbie Poster

so the way i am calling 'padRight', where there's no destination pointer there can be issues because 'a' is not big enough to hold the padded string. when i say "a = temp".

The first point is clear now. I think i can differentiate between changing the contents and changing the pointer now. thanks a lot.

one final qs. if the main function of 'padRight' is written as

int main()
{
	char *p = "smiths";
	cout << "before padding " << strlen(p) << " " << p << endl;
	//char *a = p;
	padRight(p,10);
	cout << "after padding " << strlen(p) << " " << p << endl;
};

I'm using 'char *p', this is a read-only memory but it still allows it to be changed. How does that happen?

smithss 18 Newbie Poster

I'm right now in that state of confusion that comes right before the moment of clarity. Please bear with me.

when i wrote mystrcpy i did

void mystrcpy(char *a,char *b)
{
	while (*b++ = *a++);
}
int main()
{
	char *s = "rajats";
	char d[8] = "rajatch";
	mystrcpy(s,d);
	cout << d << endl;
};

this also seems pass by value to me now. so 'd' is being passed by value then how is that when i copy 'a'-'b' the effect is visible in main? shouldn't i be passing the reference to the pointer here as well?

2> in my padRight program, 'a' is a pointer to a static array, then when i assign 'temp' to this pointer does the array size change? at 'a = temp;' step.

smithss 18 Newbie Poster

Did you attempt to walk through what your code does in your head?

Here's your code with my comments included:

// Assign a to temp
char *temp = a;
// temp and a both point to the original buffer

// Assign a to point to a new buffer n characters long
a = (char *)malloc(n);
// a points to the new buffer
// ? did you allocate space for the '\0' at the end of the string?

// Initialize the new space pointed to by a to all spaces
memset(a,' ',n);
// a points to the new buffer of spaces

// This one line does a bunch...
// each iteration copies one character from the original string to the new buffer
// both pointers are advanced  to point to the next character
// The loop continues until the '\0' is copied
while (*a++ = *temp++);
//cout << a << endl;

// Now the loop is done:
// temp points to one character beyond the '\0' in the original buffer
// a points to one character beyond the '\0' in the new buffer

// as a is a reference parameter, the pointer back in the main loop also points one beyond the '\0' in the new buffer.

Does that help with 1 & 2?

well i did and your comments tell me that i was thinking right till the last step of copying. That was bang-on target, both the pointers are pointing to the end of strings and i will have to …

smithss 18 Newbie Poster

ok i got the 3rd point. its because strlen doesn't count the null terminator hence we're getting a length of 9. The output is perfectly alright then :). Awaiting answers to 1 & 2.

smithss 18 Newbie Poster

Hi,

Couple of doubts

1> what was the error in my code? Why was in not copying the temp string back to a?
2> i realize that i'm allocating memory and it should be freed, where can i do that? In Main?

editing:

3> the output is slightly off

>
before padding 6 smiths
after padding 9 smiths

so even though you are setting temp[n-1] = 0, which seems correct because arrays start from 0, why is the strlen giving length as '9'?

Thanks

smithss 18 Newbie Poster

Hi,

so i wrote a few of the programs on my own after all the help previosuly but i've got stuck in this one here. The point is the make the string alteast 'n' characters long by padding spaces to its right.

#include <iostream>

using namespace std;

void padRight(char *&a, int n)
{
	int diff = strlen(a) - n;

	if(diff >= 0)
	{
		return;
	}
	else
	{
		char *temp = a;
		//cout << temp << endl;
		a = (char *)malloc(n);
		memset(a,' ',n);
		while (*a++ = *temp++);
		//cout << a << endl;
	}
}

int main()
{
	char p[] = "smiths";
	cout << "before padding " << strlen(p) << " " << p << endl;
	char *a = p;
	padRight(a,10);
	cout << "after padding " << strlen(a) << " " << a << endl;
};

output:
>
before padding 6 smiths
after padding 10 _MONETm

Please point me to the errors with explanations.

smithss 18 Newbie Poster

yes you are right, thats one important point and i had read about that, thanks for pointing it out, my example above was not apt I realize that.

smithss 18 Newbie Poster

Thanks for all those tips. So basically what i was doing was something like

char *p = "  smith";
char *a = p;

so even though i changed 'a' to point to the new location, 'p' never changed nor did the string.

Thanks everyone for being so patient. I have to write another 14 programs dealing with char *'s, arrays and I'm sure to pester you guys again :).

smithss 18 Newbie Poster

>>Because you did not pass the address of the pointer -- all you did was pass the address of the start of the character array, which is not the same thing. Look closely at the & symbol in the trimLeft() function declaration -- that is a reference to a pointer.

ok i understand the point of passing the 'reference to a pointer', so when I'm passing the address of the start of the array and increment it its just shifting the pointer but not actually changing the start address and hence nothing is happening. Is my understanding right?

this is my code using memmove, this is working fine

void trimLeft(char *a)
{
	char *p = a;
	while (*p++ == ' ');
	*p--;
	size_t len = strlen(p);
	memmove((void *)a,(void *)p,len);
	*(a+len) = '\0';
}
smithss 18 Newbie Poster

Thanks for your replies...

"You must realize that no data has been moved at all, however ptr is just pointing to a different part of the array you created to begin with. If you want to actually alter the array of characters, you will have to do something similar to what Ancient Dragon mentioned."

I understand the part that no data has been moved. But what i fail to understand is that when i'm passing the address of array and incrementing the pointer why is it not having as the same effect as the code that you have given. Am i missing a level of indirection? Will it be possible for you to elaborate a little more.

I don't want to google it right away as it might give me the whole code which i don't want. Till then I'll writing the code using memmove to actually move the data.

-Smith. S

smithss 18 Newbie Poster

while you are at it, can you also tell me why this is not working. i think i'm passing the address of the array and hence both the outputs should be same. but it seems to be getting passed by value. its supposed to trim any blanks at the starting of the string.

#include <iostream>

using namespace std;

void trimLeft(char *a)
{
	while (*a++ == '  ');

	*a--;

	cout << a << endl;

}

int main()
{
	char a[11] = "  smith";

	trimLeft(a);

	cout << a << endl;

};

output:
>
smith
smith (there's a space before this output line but here its getting aligned to the left border for some reason)

-Smith.S

smithss 18 Newbie Poster

Hi i have written a standard library function 'strcat' on my own, even though the output seems ok, can someone please verify if what i'm doing is right? i get confused using pointers.

void mystrcat(char *a,char *b)
{
	while (*a++ != '\0');

	*a--;

	while (*a++ = *b++);

}

i'm assuming 'a' to be big enough to hold the concatenated value.

-Smith.S