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;
}
6 1077
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
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
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
I believe the answer is: -
#include <stdio.h>
-
-
void fun(char **p)
-
{
-
delete[] (*p);
-
(*p) = new char[strlen("Moumita") + 1];
-
strcpy((*p), "Moumita");
-
}
-
-
main()
-
{
-
char *p;
-
p = new char[strlen("Debasis") + 1];
-
strcpy(p, "Debasis");
-
-
printf("%s\n", p); // Here prints Debasis
-
fun(&p);
-
printf("%s\n", p); // Here now prints Moumita
-
}
-
- Sam
yes u r right ,,,,, u welcome
Sign in to post your reply or Sign up for a free account.
Similar topics
by: perseus |
last post by:
Well,
As you all have seen, I received a host of messages
that say that I should not post up here a question about BOOST.
BOOST is a FREE interface that allows you, THE STANDARD C++
USERS, to...
|
by: Mr.Tickle |
last post by:
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;
enum SomeFlags
{
|
by: Jim Hubbard |
last post by:
Yet another hotfix alert (http://www.kbalertz.com/Feedback_823535.aspx) that
states "To resolve this problem immediately, contact Microsoft Product
Support Services to obtain the hotfix."
...
|
by: DavidS |
last post by:
Have Visual Studio.NET installed on MS 2000 Professional OS laptop. No issue
ever with web development and SQL connections.
Purchased new laptop with XP Professional SP2!!!!!!!! & Visual...
|
by: MOOVBUFF |
last post by:
I've been looking (wasting my time) for simple solutions such as Updating a
database in VS 2005 and all I get is either no answer or another question.
Obviously the people in the newsgroup can't...
|
by: jerry.levan |
last post by:
Hi,
I have a Tktable object (self.table) and when I click on a row the
whole row is selected.
If I click of a button to get the row contents then
self.table.curselection() fails with a...
|
by: sherifffruitfly |
last post by:
Hi all,
The following function is the ValueChanged handler for two
DateTimePicker thingies - allowing the user to specify a date range. I
don't understand why the following bug exists: when the...
|
by: Dale Strickland-Clark |
last post by:
Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
<object object at 0xb7bbd438>
Traceback (most recent call last):
File...
|
by: DanWeaver |
last post by:
I must be doing some daft thing-
In Management Studio Express I have Northwind, Pubs and a db of my
own.
I can create SQL select queries and get results back fine but when I
try to do an insert,...
|
by: raylopez99 |
last post by:
They usually don't teach you in most textbooks I've seen that
delegates can be used to call class methods from classes that are
'unaware' of the delegate, so long as the class has the same...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |