Global Structure not completley initializing.....

Programming, for all ages and all languages.
Post Reply
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Global Structure not completley initializing.....

Post 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?
C8H10N4O2 | #446691 | Trust the nodes.
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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? :)
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

:lol: - Wow. Okay...... :oops:

I switched them, but I still get the same results...
C8H10N4O2 | #446691 | Trust the nodes.
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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...
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post 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. :)
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
Post Reply