473,545 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string and gcc problem ?

Hi Guys,
i've problem with my small C++ programm. I've just small template
class which represetns a array, everything works fine up to
combination with std::string. I did tried it with M$ VC++ and with GCC
(Cygwin and Linux) and my problem is when i try do this

int main(int argc, char **argv) {
array<std::stri nga(10);

a[0] = "Huhuhu"; <--- with gcc i got a crash !

std::string = a[0];
return 0;
}
the program crashes with segmentation fault on std::string::as sign(),
but just with gcc ?!
Can somebode help me solve this problem ?

peter

.... and here is my code

#ifndef _array_h
#define _array_h
#include <stdexcept>
#include <sstream>
#include <stdlib.h>
#include <memory.h>
#ifdef _DEBUG
#include <iostream>
#endif

template <typename T>
class array {
public:
array(size_t ) throw(std::bad_ alloc &);
array(const array<T& ) throw(std::bad_ alloc &);
~array();

T& operator[](size_t ) throw(std::out_ of_range &);
const T&operator[](size_t ) const throw(std::out_ of_range &);

size_t length() const { return size; }

int compare(const array<T& ) const;

bool operator==(cons t array<T&arg ) { return compare(arg) == 0; }
bool operator!=(cons t array<T&arg ) { return compare(arg) != 0; }
bool operator<(const array<T&arg ) { return comapre(arg) < 0; }
bool operator>(const array<T&arg ) { return compare(arg) 0; }
bool operator!() const { return length 0; }

private:
T *data;
size_t size;
};

#endif

template <typename T>
array<T>::array (size_t e) throw(std::bad_ alloc & )
{
data = NULL; size = 0;
data = new T[e];
size = e;
}

template <typename T>
array<T>::~arra y()
{
if(data != NULL)
delete [] data;
data = NULL;
size = 0;
}

template <typename T>
const T& array<T>::opera tor[](size_t idx) const
throw(std::out_ of_range &)
{
if(idx size) {
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.c_str() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}
template <typename T>
T& array<T>::opera tor[](size_t idx) throw(std::out_ of_range &)
{
if(idx size) {
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.str().c_s tr() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}

Mar 7 '07 #1
4 11214
da*****@gmx.net wrote:
i've problem with my small C++ programm. I've just small template
class which represetns a array, everything works fine up to
combination with std::string. I did tried it with M$ VC++ and with GCC
(Cygwin and Linux) and my problem is when i try do this

int main(int argc, char **argv) {
Why do you need 'argc' and 'argv'? You never use them!
array<std::stri nga(10);
'array' is undeclared at this point. But presuming you have included
your template definition from below somehow... Supposedly here you
have create an array that internally allocates 10 strings and allows
you the use of all of them, right?
>
a[0] = "Huhuhu"; <--- with gcc i got a crash !
And with VC++ you don't, correct?

So, 'a[0]' returns a reference to the zeroth element in your array in
the 'a' object. Having looked at the operator[], I don't see any
problem with it. Did you try using a debugger to see what values you
get into the 'std::string::a ssign'?
>
std::string = a[0];
This shouldn't even compile! How did you get a crash? Are you
sure you posted your _real_ code?
return 0;
}
the program crashes with segmentation fault on std::string::as sign(),
but just with gcc ?!
Can somebode help me solve this problem ?
If it's a GNU-specific problem, GNU people should be able to help.
The code looks fine but only if one assumes that the stuff that you
omitted is correct.
>
peter

... and here is my code

#ifndef _array_h
#define _array_h
Avoid leading underscores like the plague. Any identifier that begins
with an underscore is reserved in the global namespace.
#include <stdexcept>
No such standard header.
#include <sstream>
#include <stdlib.h>
#include <memory.h>
#ifdef _DEBUG
#include <iostream>
#endif

template <typename T>
class array {
public:
array(size_t ) throw(std::bad_ alloc &);
array(const array<T& ) throw(std::bad_ alloc &);
~array();

T& operator[](size_t ) throw(std::out_ of_range &);
const T&operator[](size_t ) const throw(std::out_ of_range &);

size_t length() const { return size; }

int compare(const array<T& ) const;

bool operator==(cons t array<T&arg ) { return compare(arg) == 0; }
bool operator!=(cons t array<T&arg ) { return compare(arg) != 0; }
bool operator<(const array<T&arg ) { return comapre(arg) < 0; }
bool operator>(const array<T&arg ) { return compare(arg) 0; }
Why aren't those operators 'const'?
bool operator!() const { return length 0; }

private:
T *data;
size_t size;
};

#endif

template <typename T>
array<T>::array (size_t e) throw(std::bad_ alloc & )
{
data = NULL; size = 0;
What is that for?
data = new T[e];
size = e;
}

template <typename T>
array<T>::~arra y()
{
if(data != NULL)
delete [] data;
data = NULL;
size = 0;
}

template <typename T>
const T& array<T>::opera tor[](size_t idx) const
throw(std::out_ of_range &)
{
if(idx size) {
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.c_str() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}
template <typename T>
T& array<T>::opera tor[](size_t idx) throw(std::out_ of_range &)
{
if(idx size) {
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.str().c_s tr() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #2
On 7 Mar, 13:59, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
daro...@gmx.net wrote:
#include <stdexcept>

No such standard header.
Where does your implementation declare std::logic_erro r and the like
then?

Gavin Deane

Mar 7 '07 #3
Gavin Deane wrote:
On 7 Mar, 13:59, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>daro...@gmx.ne t wrote:
>>#include <stdexcept>

No such standard header.

Where does your implementation declare std::logic_erro r and the like
then?
My mistake.
Mar 7 '07 #4

da*****@gmx.net wrote:
>
int main(int argc, char **argv)
argc, argv are never used
{
array<std::stri nga(10);

a[0] = "Huhuhu"; <--- with gcc i got a crash !
comment sign // lost
>
std::string = a[0];
error assignment to type

std::string tmp= a[0];
return 0;
}

template <typename T>
class array {
public:
array(size_t ) throw(std::bad_ alloc &);
array(const array<T& ) throw(std::bad_ alloc &);
extra <Tfor "array<T&"
array(const array& ) throw(std::bad_ alloc &);

assignment operator lost
array& operator=(const array& ) throw(std::bad_ alloc &);
>
template <typename T>
array<T>::array (size_t e) throw(std::bad_ alloc & )
{
data = NULL; size = 0;
data = new T[e];
size = e;
}
assignment instead of initialization,
extra assignment to data, size

template <typename T>
array<T>::array (size_t e) throw(std::bad_ alloc & )
:data( e? new T[e]: 0),
size(e)
{}
>
template <typename T>
array<T>::~arra y()
{
if(data != NULL)
rare needed, here extra comparsion with "data"
delete [] data;
data = NULL;
rare needed, here extra assignment to "data"
size = 0;
more rare needed, here extra assignment to "size"
}

template <typename T>
const T& array<T>::opera tor[](size_t idx) const
throw(std::out_ of_range &)
{
if(idx size) {
if(idx >= size) {

assuming idx, size unsigned
size is 1..N or empty
idx is 0..size-1 or overflow
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.c_str() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}
template <typename T>
T& array<T>::opera tor[](size_t idx) throw(std::out_ of_range &)
{
if(idx size) {
if(idx >= size) {
std::stringstre am error;
error << "idx size " << __FILE__ << "(" << __FUNCTION__ << ":" <<
__LINE__ << ")";
#ifdef _DEBUG
std::cerr << error.str().c_s tr() << std::endl;
#endif
throw std::out_of_ran ge(error.str()) ;
}

return data[idx];
}
***********
a[0] = "Huhuhu"; //<--- with gcc i got a crash !
std::string::as sign(...
/**
* The data is copied,

so must not be errors here

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 8 '07 #5

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

Similar topics

10
8149
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That...
11
3626
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
5
8725
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
14
2657
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling pthread_mutex_lock and pthread_mutex_unlock from different std::string methods. Since the strings I'm using are never used in another thread I was wondering...
22
13217
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
19
6120
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible...
8
9168
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be a namespace and another typedef to basic_string: namespace my_string {
16
16394
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
84
15788
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
2880
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
0
7464
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...
0
7396
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...
0
7805
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...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5968
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...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.