Putting a C-string into a struct

Hi. I have some code to get the vendor string from a CPU, but I don't want the function (see below) to print anything. I want to put the string in my struct (also below), so I tried using strcpy but it caused a segfault, as did memmov.

How can I get vstring into the struct?

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
void getVID(void) {
    cpu_id_t* cpu;
    char manufacturer[24];
    char vstring[12];
    void* vptr = (void*)&(vstring[0]);
    
    __asm__ __volatile__
    (
        "movl $0, %%eax\n\t"
        "cpuid\n\t"
        "movq %0, %%rsi\n\t"
        "movl %%ebx, (%%rsi)\n\t"
        "movl %%edx, 4(%%rsi)\n\t"
        "movl %%ecx, 8(%%rsi)\n\t"
       :"=m"(vptr)
    );
    
    strcpy(manufacturer, getManufacturer(vstring));
    
    if (!strcmp(manufacturer,"Unknown Manufacturer")) {
        printf("Manufacturer:\t%s\n"
               "Vendor String:\t%s\n",
               manufacturer, vstring);
    } else {
        printf("Manufacturer:\t%s\n",
               manufacturer);
    }
    
}


1
2
3
4
struct cpu_id {
    char vstring[13];
    char manufacturer[24];
};


Thanks.
Last edited on
What does getManufacturer() do, and what is this?
(void*)&(vstring[0])
Now, that's what I call being roundabout.
(void*)vstring
What does getManufacturer() do

It's a bunch of strcmps against
1
2
3
4
5
6
7
8
9
10
11
12
13
#define AMD_OLD       "AMDisbetter!"
#define AMD_NEW       "AuthenticAMD"
#define CENTAUR       "CentaurHauls"
#define CYRIX         "CyrixInstead"
#define INTEL         "GenuineIntel"
#define NAT_SEMICON   "Geode by NSC"
#define NEXGEN        "NexGenDriven"
#define RISE          "RiseRiseRise"
#define SIS           "SiS SiS SiS "
#define TRANSMETA_NEW "GenuineTMx86"
#define TRANSMETA_OLD "TransmetaCPU"
#define UMC           "UMC UMC UMC "
#define VIA           "VIA VIA VIA " 

it returns "AMD", "Intel", [...] "VIA" or "Unknown Manufacturer" based on that.
Last edited on
Did you check that it's returning something meaningful? Functions with a return type return garbage when an execution path doesn't have a return statement.
The getManufacturer function returned 'Intel' on my CPU.

It's when I try to memmove/strcpy vstring or manufacturer into the struct that I get a segfault.

I don't want that function doing output because its meant to do a job and return a value in the same way strcmp doesn't do output, it just works away silently.
Can you post a code which causes this segfault?

Are you aware of the fact that "cpu" is not initialized? If you try to copy something into an area referenced by "cpu" then there's no suprise that you get the error.
Last edited on
cpu_t is a typedef -- typedef struct cpu_id cpu_t; which means that *cpu should point to cpu, right?

That code does cause a segfault.
Last edited on
cpu_t is a typedef -- typedef struct cpu_id cpu_t; which means that *cpu should point to cpu, right?

No when cpu is not initialized.

That code does cause a segfault.

You said earlier:

It's when I try to memmove/strcpy vstring or manufacturer into the struct that I get a segfault.

I don't see any attempt to copy vstring or manufacturer into the structure (cpu_id, I assume) in the code you posted at the beginning.
Oh, wait no it wasn't that code; my mistake.
Topic archived. No new replies allowed.