By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Here is a not so obvious problem

P: 4
I'm just trying to create a checked strcpy wrapper inside a base class that can be used by it's children. IF you run this code you will knowtice that the values change and reallocate inside the Set_String function but there is no change outside the scope of the function. If you can let me know how to do this, you are a better man than I.

- Sam



#include "stdio.h"
#include <string.h>





///////////////////// Immulated Base class h and cpp file
class Baser
{
public:
Baser();
~Baser() {};

char* Btype;

static int Set_String(char* Destination, char* Source);
};

Baser::Baser()
{
Btype = NULL;

Set_String(Btype, "Init");
};

int Baser::Set_String(char *Destination, char *Source)
{
if (Source == NULL) return -1; // Nothing to copy

if (strlen(Source) > 99) return -1; // Don't copy large strings

// delete, reallocate, and populate destination pointer
delete(Destination);
Destination = new char[strlen(Source) + 1];
strcpy(Destination, Source);

return 1;
};





///////////////////// Immulated child class h and cpp file
class Childer: public Baser
{
public:
Childer();
~Childer() {};

char* Ctype;
};

Childer::Childer()
{
Ctype = NULL;

Set_String(Ctype, "Init");
};





///////////////////// Main
int main(int argc, char* argv[])
{
Childer* myChild = new Childer();

char* Error_Message = NULL;

myChild->Set_String(Error_Message, "Okay");

if (Error_Message == NULL) printf("Why is Error_Message this still NULL???\n");

delete [] Error_Message;

return 0;
}
Mar 20 '07 #1
Share this Question
Share on Google+
6 Replies


dmjpro
100+
P: 2,476
Destination = new char[strlen(Source) + 1];

this line causes the error .... u r setting the address in side the function ....
if u write this then u have to pass pointer to pointer ......


if u setting the address in caller function and then call the function .... i thing it will work fine ...


welcome again
Mar 20 '07 #2

P: 4
Thanks for the welcome. This is a great forum.

Can you clarify:

-- "if u write this then u have to pass pointer to pointer ......"
Does this mean that the I can't reallocate a pointers memory inside a function because the block it's called from will retain the address of the old allocated memory? That's how it's acting to me.

-- "if u setting the address in caller function and then call the function"
Can you think of a way to do this then?

- Sam
Mar 20 '07 #3

dmjpro
100+
P: 2,476
okkk .... look at this code ....


void fun(int *p)
{
p="Moumita";
}

main()
{
int *p = "Debasis";
puts(p); //Here print Debasis
fun()
puts(p); //Here also print Debasis
}

plz try this code ....

now here is the explain .....

u r passing address and u change the address ........

how is it be possible ..... have u any idea

welcome again
Mar 20 '07 #4

P: 4
I believe the answer is:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void fun(char **p)
  4. {
  5.    delete[] (*p);
  6.    (*p) = new char[strlen("Moumita") + 1];
  7.    strcpy((*p), "Moumita");
  8. }
  9.  
  10. main()
  11. {
  12.    char *p;
  13.    p = new char[strlen("Debasis") + 1];
  14.    strcpy(p, "Debasis"); 
  15.  
  16.    printf("%s\n", p);   // Here prints Debasis
  17.    fun(&p); 
  18.    printf("%s\n", p);   // Here now prints Moumita
  19. }
  20.  
- Sam
Mar 20 '07 #5

P: 4
Thanks Yall.

- Sam
Mar 21 '07 #6

dmjpro
100+
P: 2,476
yes u r right ,,,,, u welcome
Mar 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.