Page 1 of 1

Global Structure not completley initializing.....

Posted: Sun Apr 22, 2007 2:39 pm
by Alboin
I've got an array of structs:

Code: Select all

job instruction_list[INSTRUCTION_COUNT] = {
	{ 5, 1, 0, NULL, "load_bin_file\0" },
	{ 5, 1, 0, NULL, "load_com_file" },
	{ 5, 1, 0, NULL, "cpu" }, 
	{ 5, 1, 0, NULL, "mem" },
	{ 7, 1, 0, NULL, "load_device" },
	{ 2, 1, 0, NULL, "randomize_memory" }
};
Where job is

Code: Select all

struct job {
	int priority;
	int arg_count;
	ll args;
	char *name;
	int flags;
};
The integer priority initializes and so do the rest, but not the char *name. (I get "(null)") I've checked this out else where and I think it's supposed to work. The array is global, and does not change. I've also checked initialization in K+R's book, and it says a character array can be initialized by a string literal. So, I'm at a loss.

Why wouldn't the char* be initializing, but everything else is?

Posted: Sun Apr 22, 2007 3:09 pm
by urxae
Could that possibly be because the fourth elements of the initializers are all NULL, and the name is the fourth member of the struct? :)

Posted: Sun Apr 22, 2007 3:11 pm
by Alboin
:lol: - Wow. Okay...... :oops:

I switched them, but I still get the same results...

Posted: Sun Apr 22, 2007 3:25 pm
by urxae
(EDIT: Saw in another topic that you use "ll" for linked list)

Code: Select all

#include <stdio.h>

struct linked_list;

typedef struct linked_list* ll;

struct job {
   int priority;
   int arg_count;
   ll args;
   char *name;
   int flags;
};
typedef struct job job;

#define INSTRUCTION_COUNT 6

job instruction_list[INSTRUCTION_COUNT] = {
   { 5, 1, NULL, "load_bin_file\0", 0 },
   { 5, 1, NULL, "load_com_file", 0 },
   { 5, 1, NULL, "cpu", 0 },
   { 5, 1, NULL, "mem", 0 },
   { 7, 1, NULL, "load_device", 0 },
   { 2, 1, NULL, "randomize_memory", 0 }
};

int main() {
	for (size_t i = 0; i < INSTRUCTION_COUNT; i++) {
		job j = instruction_list[i];
		printf("%d %d %p %s %x\n",
			j.priority, j.arg_count, j.args, j.name, j.flags);
	}
}
Output:

Code: Select all

5 1 (nil) load_bin_file 0
5 1 (nil) load_com_file 0
5 1 (nil) cpu 0
5 1 (nil) mem 0
7 1 (nil) load_device 0
2 1 (nil) randomize_memory 0
That looks like it works...

Posted: Sun Apr 22, 2007 10:49 pm
by mystran
Is this when you are running on a normal OS, or when you are doing OS development? Because if you are doing OS development, then it's most likely because your linker script doesn't link .rodata into your binary. So add .rodata into your .text, or use -fwritable-strings (to make strings go to .data instead of .rodata) and stuff should magically work. :)