Datatype of C++ string literal

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Datatype of C++ string literal

Post by ubergeek »

So if I enter text in quotes, like so:

Code: Select all

puts("Test");
What is the datatype of "Test"? Is it a char*, a const char*, an unsigned char*, or what? I'm having a hard time implementing puts(). (I've got putch... so far all my OS does is print "x" on the screen. Not much, but...
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

pretty sure it's a const char pointer as the text is stored in the readonly section of the executable.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

Just do some test, i.e write a function that excepts a char * and see if you can call it, if it works, than it's a char *, if not try const char *. etc
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

It is a "const char*". Try putting a string literal into a function that accepts a char* parameter and you'll get a warning on G++ something like "Deprecated implicit conversion from const char* to char*".
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

I knew that, I wanted him to find out for him self.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

Well then something's wrong. I have

Code: Select all

puts(const char *text)
{
...
}
and when I try to do

Code: Select all

puts("Hello World!")
it compiles successfully, but GRUB tells me something about the executable format not being supported. This leads me to think it didn't really compile successfully, it just thought it did. What's wrong?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

ubergeek wrote:Well then something's wrong. I have

Code: Select all

puts(const char *text)
{
...
}
and when I try to do

Code: Select all

puts("Hello World!")
it compiles successfully, but GRUB tells me something about the executable format not being supported. This leads me to think it didn't really compile successfully, it just thought it did. What's wrong?
That has nothing to do with the executable format. Does GRUB work without that compiled in? Have you tried removing all code from that function until something breaks?
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

Sounds like a problem with linker script.
Do you have .rodata somewhere?
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

No, I do not have any rodata section in my linker script. Before someone tells me, let me guess (correct me if I'm wrong) - is rodata "read only data?" Because that would explain why initializing a const char* with " " would not work. (consts are read-only.)

So how do I add rodata to my linker script?
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

JamesM wrote: That has nothing to do with the executable format. Does GRUB work without that compiled in? Have you tried removing all code from that function until something breaks?
I tested this, what doesn't work is the actual call to puts(). (See below for insight)
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

ubergeek wrote:No, I do not have any rodata section in my linker script. Before someone tells me, let me guess (correct me if I'm wrong) - is rodata "read only data?" Because that would explain why initializing a const char* with " " would not work. (consts are read-only.)

So how do I add rodata to my linker script?
Well yes, string go to .rodata by default (there's -fwritable-strings in gcc for forcing them into .data but that's not necessarily a good idea).

You can add .rodata into your script just like any other section. I forexample just use:

Code: Select all

    .text :
    {
        *(.text)
        *(.rodata*)
    }
You can make it a separate section in the resulting binary just as well, if you feel like it. I couldn't be bothered when I wrote my script.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

mystran wrote:I forexample just use:

Code: Select all

    .text :
    {
        *(.text)
        *(.rodata*)
    }
Is that your entire linker script? I took this one now, from Bran's Kernel Development Tutorial, and it still gave me the exact same error.

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

Wait wait wait! My mistake, I actually was linking with an older linker script, with the new one (with the rodata section) it works great! Now my kernel prints "Hello World!" instead of 'x'.

Next step: Printing "Victory!!!"

Thanks all for your help on this one.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

np. As a completely shameless plug, if you'd have copied the linker script from my tutorials you wouldn't have had that problem (it has .rodata in already)! :D
ubergeek
Posts: 23
Joined: Thu Aug 09, 2007 5:26 am

Post by ubergeek »

Thanks, but actually, I was using a linker script from Bran's Kernel Development Tutorial (see the article in the wiki on its errors which, if I had seen first, would have made this post redundant). But your printing to the screen function worked great for me.
Post Reply