473,626 Members | 3,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with the function that takes some_object** as the argument

Hi!

I have a class:
some_class{
private:
char* seq;
public:
some_class(char * se);
char* ret_seq();
....
}
some_class::som e_class(char* se){
seq=new char[strlen(se)];
seq=se;
}

char* some_class:ret_ seq(){
static char* sequ;
sequ=se;
return sequ;
}
and than
I have the function witch should modify the array of the objectc of
the some_class:

int func(some_class ** obj){
....
char* tmp;
for(int i=0;i<foo;i++){
.....
obj[i]=new some_class(tmp) ;
cout<<(*obj[i]).ret_seq()<<en dl; //LINE A
}
return foo;
}

and when I want to get the seq values for all objects in the array:
main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j<i;j++)
{
cout<<(*objs[i]).ret_seq()<<en dl; //LINE B
}
....

and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings

PS: I have just started to programm in C++
Jul 19 '05 #1
4 1417

"Krzysztof" <kw*****@yahoo. com> wrote in message
news:c5******** *************** ***@posting.goo gle.com...

My first suggestion? Use the string class, not char* pointers. But read
on...
Hi!

I have a class:
some_class{
private:
char* seq;
public:
some_class(char * se);
char* ret_seq();
...
}
Need a semi-colon here.
some_class::som e_class(char* se){
seq=new char[strlen(se)];
This allocates an array as long as the string you want to hold. But I
assume you'll be using null-terminated strings here, so you need to add one
to that:

seq = new char[strlen(se)+1];
seq=se;
This does not copy the data in the array. It merely overwrites the seq
pointer with the value in se, which makes seq point to the same memory as
se.
}

char* some_class:ret_ seq(){
That shoud have two colons, not one! (The above should not have compiled!)
static char* sequ;
sequ=se;
Again, this just copies the pointer contents, not the array contents.
Except for one other serious problem: there IS NO se variable in this
function (or class)! Did you mean "seq"? If so, why are you returning a
copy of the pointer, and why use a static variable to hold that copy?
return sequ;
}
and than
I have the function witch should modify the array of the objectc of
the some_class:

int func(some_class ** obj){
...
char* tmp;
for(int i=0;i<foo;i++){
What's foo???
....
obj[i]=new some_class(tmp) ;
Just guessing, but is the "..." above where tmp gets allocated and filled
out?
cout<<(*obj[i]).ret_seq()<<en dl; //LINE A
}
return foo;
}

and when I want to get the seq values for all objects in the array:
main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j<i;j++)
{
cout<<(*objs[i]).ret_seq()<<en dl; //LINE B
}
Why use (*objs[i])? Why not use objs[i]-> instead?
...

and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings

PS: I have just started to programm in C++


This code won't compile as is. Can you post what you really have, or at
least a compilable sub-set of your original code?

My suggestion, again, would be to use the string class, and avoid all the
pointers. But it looks like your main problem is trying to use the
assignment operator (=) to copy array data, which does not work. You need
to use strcpy, or memcpy, or manually copy the data, not assign the pointers
as you've done.

-Howard

Jul 19 '05 #2
Krzysztof wrote:
Hi!

I have a class:
some_class{
private:
char* seq;
public:
some_class(char * se);
char* ret_seq();
...
}
some_class::som e_class(char* se){
seq=new char[strlen(se)];
In the above line, you've allocated almost enough memory for the
C-style string (hopefully) pointed to by `se' (you haven't allocated
enough to hold the trailing null char).
seq=se;


Now you immediately set `seq' to something else -- in this case
whatever the passed argument `se' happened to be. Also, since you've
lost the original pointer to the allocated buffer, a memory leak has
been introduced.

So whaddya expect? ;-)

[snip]

Fix the above -- and next time please post well formatted *minimal*
*compilable* code that exhibits whatever problem you've encountered.
That will greatly enhance your chances of getting whatever help you
need.

Cheers and HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #3
"Krzysztof" <kw*****@yahoo. com> wrote...
I have a class:
some_class{
private:
char* seq;
public:
some_class(char * se);
char* ret_seq();
...
}
some_class::som e_class(char* se){
seq=new char[strlen(se)];
seq=se;
Now, this is a HUGE error. You allocate 'strlen(se)' chars,
obtain the address of that newly allocated memory, and then
BAM! immediately override the value with the parameter. This
is known as a "memory leak".

The error is probably due to your missing the fact that arrays
cannot be copied using the assignment operator, and due to your
confusing arrays and pointers. No biggie, it's one of the most
difficult areas for beginners.

You made two mistakes (logical, not syntax) in this function.
First, if what you pass is a C-string, you need to (a) allocate
one more character than the length to store the null terminator,
and (b) use 'strcpy' to copy the array:

some_class::som e_class(char* se) {
seq = new char[strlen(se) + 1];
strcpy(seq, se);
}

Now, that said, you'd be MUCH BETTER OFF if you used 'string'
class from <string>.
}

char* some_class:ret_ seq(){
static char* sequ;
sequ=se;
return sequ;
What's that for? Can't you just do

return se;

?
}
and than
I have the function witch should modify the array of the objectc of
the some_class:

int func(some_class ** obj){
...
char* tmp;
for(int i=0;i<foo;i++){
....
obj[i]=new some_class(tmp) ;
cout<<(*obj[i]).ret_seq()<<en dl; //LINE A
}
return foo;
}

and when I want to get the seq values for all objects in the array:
main()
int main()
{
some_class** objs=new some_class*[100];
int i;
i=func(objs);
for(int j=0;j<i;j++)
{
cout<<(*objs[i]).ret_seq()<<en dl; //LINE B
}
...

and the problem is that the results that I obtain from LINE A and LINE
B are not the same.
If you have any idea why please help. I would be very thankful
Greetings

PS: I have just started to programm in C++


Visit alt.comp.lang.l earn.c-c++ newsgroup, it might be more
beneficial for you as a beginner.

Victor
Jul 19 '05 #4
Thankks a lot.
Now I have got what is up and now it works perfect... but will wisit
the newgroup that has been suggested
Krzysztof
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2971
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
13
1670
by: al | last post by:
1. If a function template is defined: template <typename A> A SQ(A& a) { return a*a; } How could prevent users from passing a type which makes no sense, like Employee class type?
4
2395
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
11
2798
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its derived class circle, rectangle and triangle. i have also written this method for each shape object called 'draw_all_seq_inside2' which takes two values. i want to call this method using for_each statement in another method but it doesnt seem to...
8
10079
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) When I asked why, I was told that it facilitated calling it as:
6
2338
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
39
19615
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
28
4302
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
1
2763
by: JohnCox | last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode). The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this: SIMPLELIB_API int StrFirst(LPTSTR str, long num); The second function is essentially the same thing, but with the order of the...
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.