Problem when using struct pointer as class constructor parameters

Hi, I got a Segmentation Fault(core dump) error when running the following code. Seems like the struct pointer does not work when used as the constructor parameter. I am using ubuntu-12.10-64bit system. Any one has any solution for that? Thanks very much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
typedef struct{
    char code[11];
    char period[6];
    char state[6];
} config;

class star1 {
    protected:
        int sats;
    public:
        star1(config *conf) {
          cout<<conf->code<<endl;
          cout<<conf->period<<endl;
        }
}

int main() {
    config temp;
    
    strncpy(temp.code,"0",sizeof(config.code));
    strncpy(temp.period,"0",sizeof(config.period));
    strncpy(temp.state,"0",sizeof(config.state));

    cout<<"Debug 1"<<endl;
    star1 item(&temp);
    cout<<"Debug 2"<<endl;

    return 0;

}

The output is:
Debug 1
Segmentation fault (core dumped)
I believe there's a syntax error in your declaration of star1 (no ; at the end).

Also I wonder if your declaration of struct is C-style.. Oh well, this seems to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstring>

struct config{
    char code[11];
    char period[6];
    char state[6];
};

class star1 {
    protected:
        int sats;
    public:
        star1(config *conf) {
          std::cout<<conf->code<<std::endl;
          std::cout<<conf->period<<std::endl;
        }
};

int main() {
    config temp;

    strncpy(temp.code,"0",sizeof(temp.code));
    strncpy(temp.period,"0",sizeof(temp.period));
    strncpy(temp.state,"0",sizeof(temp.state));

    std::cout<<"Debug 1"<<std::endl;
    star1 item(&temp);
    std::cout<<"Debug 2"<<std::endl;

    return 0;

}
I will recommend not using strncpy when copying from one string to another because in the case of source string being longer than destination string, something weird starts to happen with the destination string in that it it copies more than it's specified capacity and reallocates itself. Example, replace this main with the one posted by Bourgond Aries and see the length of the strings after it has run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
    config temp;
    
//    printf("%lu %lu %lu\n", sizeof(temp.code), sizeof(temp.period), sizeof(temp.state));
    strncpy(temp.code,"To be or not to be",sizeof(temp.code));
    strncpy(temp.period,"To be or not to be",sizeof(temp.period));
    strncpy(temp.state,"To be or not to be",sizeof(temp.state));

    std::cout<<"Debug 1\n";
    star1 item(&temp);
    printf("%lu %lu\n", strlen(temp.code), strlen(temp.period));
    std::cout<<"Debug 2\n";
    return 0;
}
I will recommend not using strncpy when copying from one string to another because in the case of source string being longer than destination string,


That why one uses strncpy as opposed to strcpy. Of course, one needs to make an effort to ensure the resultant string is nul terminated, which the OP isn't doing (but which isn't a problem in his code.)


something weird starts to happen with the destination string in that it it copies more than it's specified capacity and reallocates itself.

arrays of type char do not reallocate themselves.

It's hard to say what is causing the problem the OP has since the code provided obviously isn't the code that is producing the problem as it will not compile for multiple reasons.


1
2
3
4
void strncpy_t(char* dest, const char * source, unsigned long Size) {
	*dest = '\0';
	strncat(dest, source, Size-1);
}


This works, however it cuts off at the last character
I believe there's a syntax error in your declaration of star1 (no ; at the end).

Your belief is incorrect. If you put the method definition inside the class definition, you don't need a semicolon after the closing brace.

@MikeyBoy

It was not about the method, but the entire class definition.
Oh, sorry! Yes, you're right.
@Bourgond Aries @MikeyBoy

There is a typo in the codes. I forgot to put ; after class definition. But in my simulation, I did put ; so there is no compilation error but failed at run time.
There is a typo in the codes. I forgot to put ; after class definition. But in my simulation, I did put ; so there is no compilation error but failed at run time.

That is not the only thing keeping the code you posted from compiling. Please test code to make sure the problem is reproduced by it before posting it if you've simplified or changed it for posting.
Topic archived. No new replies allowed.