-3
class A{
        char info[256];
        public:
        char* getInfo();
        A(char i[256]);

//A.cpp
#include "A.h"
char * A::getInfo(){
    return(&info[256]);
}
A::A(char i[256]){
    info[256]=i[256];
}

I'm struggling with the accessor. When I try to use getInfo(), I get a char*, and thus with

char test[256] = "test";
FractionException d(test);
for (int i = 0; i < 256; i++) {
    cout << d.getInfo()[i] ;
}

I get

╠╠╠╠╠╠╠╠test

I guess I'm doing things the wrong way, but I cant figure it out..

By the way, VScode also warn me on

info[256]=i[256]

by telling me that 257 octets bytes might be written (C6386) but I dont get it ...

Could you help me please ? Thanks !

178434577
  • 1
  • 3
  • Also, you cannot return an array from a function, in C or C++. It doesn't work this way. In C++ use `std::array` instead of a plain array, and you will be able to return that from a function. And you still have to fix the broken constructor. – Sam Varshavchik May 30 '21 at 16:55
  • You should have a default constructor, one without parameters. – Thomas Matthews May 30 '21 at 16:56
  • You should pass arrays by reference so that the compiler is not making a copy when passing to functions. – Thomas Matthews May 30 '21 at 16:57
  • @SamVarshavchik there is nothing wrong with the accessor shown. It is not returning an array, it is returning a pointer to the 1st element of the array, which is perfectly legal. – Remy Lebeau May 30 '21 at 16:57
  • @ThomasMatthews arrays can't be passed by value, so there is no copy being made.. In this code, the array is being passed to the constructor by an implicit pointer. – Remy Lebeau May 30 '21 at 16:57
  • The stated intent is to return a `char[256]`. That's just not possible in C++. There's a big difference between return an array, or a pointer to its first element, and the current version of the accessor doesn't even return a pointer to the first element in the array, anyway. – Sam Varshavchik May 30 '21 at 16:58
  • `info[256]=i[256]` won't copy an array into the other. Seems to me that `info` is a string, so why not using `std::string`? – MatG May 30 '21 at 17:00
  • `╠` is 0xCC in [CP437](https://en.wikipedia.org/wiki/Code_page_437), and 0xCC means you're accessing uninitialized memory. That's a special value in debug mode to help you identify problems: [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) – phuclv May 30 '21 at 17:09

2 Answers2

0

This declaration of the constructor

A(char i[256]);

does not make a great sense because the compiler will adjust the parameter declaration like

A(char *i);

Taking into account this code snippet

char test[256] = "test";
FractionException d(test);
for (int i = 0; i < 256; i++) {
    cout << d.getInfo()[i] ;
}

It seems you want that the constructor would accept a string. If so then it should be declared like

A( const char * );

and it can be defined like

#include <cstring>

//...

A::A( const char *i ){
    strncpy( info, i, sizeof( info ) );
    info[sizeof( info ) - 1] = '\0';
}

The member function getInfo should return the array instead of the address of the non-existent element info[256]

char * A::getInfo(){
    return info;
}

This method should be also overloaded

const char * getInfo() const;

And this loop

for (int i = 0; i < 256; i++) {
    cout << d.getInfo()[i] ;
}

should be substituted for this statement

std::cout << d.getInfo();
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The problem is, your constructor is not initializing the contents of the info array correctly, and your accessor is returning a bad pointer.

In the constructor, info[256]=i[256] does not do what you think it does. You are trying to copy the 257th element of i into the 257th element of info, which is Undefined Behavior since neither array has 257 elements. That is why the compiler is warning you about it.

Try this instead:

A::A(char i[256]){
    for(int x = 0; x < 256; ++x){
        info[x] = i[x];
   }
}

Alternatively:

#include <algorithm>

A::A(char i[256]){
    std::copy_n(i, 256, info);
}

As for the accessor, it is returning a pointer to the non-existent 257th element. You need to return a pointer to the 1st element instead:

char * A::getInfo(){
    return(&info[0]);
}

Or simply:

char * A::getInfo(){
    return info;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770