MudBytes
» MUDBytes Community » Codebase Specific » DikuMUD » SMAUG » Cronel's Editor to C++ Compil...
Pages: << prev 1 next >>
Cronel's Editor to C++ Compile Error
Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#1 id:27235 Posted Jun 23, 2009, 5:01 am


Code (text):
1
2
3
4
5
6
7
8
9
10
11
 
make -s smaug
  Compiling o/editor.o....
editor.c: In function 'EDITOR_DATA* str_to_editdata(const char*, short int)':
editor.c:325: error: assignment of read-only location '*(eline->editor_line::line + ((unsigned int)((unsigned int)i)))'
editor.c:341: error: assignment of read-only location '*(eline->editor_line::line + ((unsigned int)((unsigned int)i)))'
editor.c:349: error: invalid conversion from 'const void*' to 'void*'
editor.c:349: error:   initializing argument 1 of 'void* realloc(void*, size_t)'
 


I've gotten it down to several errors that look like this, so I figure I can fix the rest if I can get help with these two.


Code (text):
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
 
defines:
const char *p;
EDITOR_DATA * edd;
EDITOR_LINE * eline;
short i;
short tsize, line_count;
 
 
code:
   if( max_size != NOLIMIT && tsize + line_count * 2 + 1 >= max_size )
       break;
 
   if( *p == '\r' )
   ;
   else if( *p == '\n' )
   {
      eline->line[i] = '\0';    // 325
      eline->next = make_new_line( "" );
      eline = eline->next;
       line_count++;
      i = 0;
   }
 





Code (text):
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
36
37
38
39
40
41
42
43
44
45
46
 
defines: 
const char *p;
EDITOR_DATA * edd;
EDITOR_LINE * eline;
short i;
short tsize, line_count;
 
#define RESIZE_IF_NEEDED( buf, buf_size, buf_used, added_use )  \      
        if( (buf_used) + (added_use) >= (buf_size) )            \
        {                                                       \
                short added_size;                               \
                added_size = BLOCK_ROUNDUP( added_use );        \
                if( added_size == 0 )                           \
                        added_size = CHAR_BLOCK;                \
                RECREATE( (buf), char, buf_size + added_size ); \
                (buf_size) += added_size;                       \
        }
 
 
code:
   eline = eline->next;
   line_count++;
   i = 0;
   }
   else
   {
   eline->line[i] = *p;     //341
   eline->line_used++;
   tsize++;
   i++;
   RESIZE_IF_NEEDED( eline->line, eline->line_size, eline->line_used, 1 );   // 349
}
p++;
 
}
 
 
if( eline->line[0] != '\0' )
 
   {
 
eline->line[i] = '\0';
 


If you need more, simply ask.
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#2 id:27236 Posted Jun 23, 2009, 5:07 am

And right after I posted I figured it out...

Code (text):
1
2
3
4
5
 
const char *p;
 

should be:
Code (text):
1
2
3
4
5
 
char *p;
 


Mi mistake in massively converting it.
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

David Haley
Wizard






Group: Members
Posts: 6,912
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#3 id:27240 Posted Jun 23, 2009, 8:39 am

Hrm. Nothing you showed involves assigning to p, so I'm not sure why that fixed it, but good that it's working now... :smile:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#4 id:27254 Posted Jun 23, 2009, 1:41 pm

David Haley said:
Hrm. Nothing you showed involves assigning to p, so I'm not sure why that fixed it, but good that it's working now... :smile:

It was in the defines above the actual code, after the error boxes.

Banner said:

#
# defines:
# const char *p;
# EDITOR_DATA * edd;
# EDITOR_LINE * eline;
# short i;
# short tsize, line_count;
#
# #define RESIZE_IF_NEEDED( buf, buf_size, buf_used, added_use )  \   
#        if( (buf_used) + (added_use) >= (buf_size) )            \
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

Last edited Jun 23, 2009, 1:41 pm by Banner
David Haley
Wizard






Group: Members
Posts: 6,912
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#5 id:27255 Posted Jun 23, 2009, 1:48 pm

What I meant is that nothing in the code assigns to p even after expanding the macro. Also, for that matter, p isn't used for anything except a dereference, so pointer constness doesn't matter.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#6 id:27256 Posted Jun 23, 2009, 1:55 pm

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
char *buf;
const char *src, *tmp;
 
short size, used, i;
 
 
CREATE( buf, char, MAX_STRING_LENGTH );
 
size = MAX_STRING_LENGTH;
 
used = 0;
 
buf[0] = '\0';
 



I got an error with *buf here that was the same case as the error with *p, I had buf defined as const char *buf and it said something about read-only assignment when it got down to buf[0] =. I'm guessing assigning something as a const means it's constant, read-only, and it's not going to change, so when it attempted to assign something to it it got confuzzled?
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

David Haley
Wizard






Group: Members
Posts: 6,912
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#7 id:27257 Posted Jun 23, 2009, 1:58 pm

Yes, in the code you just showed, buf[0] = X is illegal if buf is a const char*. It means that it's a pointer to constant characters, meaning that you can't change the characters in the string. Changing buf to char* would indeed fix the problem shown in line 14 of your most recent code paste.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#8 id:27258 Posted Jun 23, 2009, 2:16 pm


David Haley said:
Yes, in the code you just showed, buf[0] = X is illegal if buf is a const char*. It means that it's a pointer to constant characters, meaning that you can't change the characters in the string. Changing buf to char* would indeed fix the problem shown in line 14 of your most recent code paste.

Generally speaking, you'd never use a const char assignment within a function since it's going to change at some point? They can be used in function definitions since it's usually passing an argument the character has already typed, and that is not going to change throughout the function?
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

David Haley
Wizard






Group: Members
Posts: 6,912
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#9 id:27259 Posted Jun 23, 2009, 2:24 pm

Well, it depends on what you're going to do with it. If you want to actually change some string, you can't use const. But if you just want to scan a string, you can (and should) use a const char* to walk over the string.

Any input to the function that the function won't be changing should also be const. In some cases, you have no choice in the matter, as with character input for example which is const at the caller's location, and so you must pass it as const to the function.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Banner
Sorcerer






Group: Members
Posts: 384
Joined: Jul 14, 2006

Go to the bottom of the page Go to the top of the page
#10 id:27260 Posted Jun 23, 2009, 2:26 pm

Well, that makes much more sense when you put it that way. Thank you.
.........................
Lead Developer,
Star Wars: Galactic Insights
--
sudo apt-get sandwich

David Haley
Wizard






Group: Members
Posts: 6,912
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#11 id:27264 Posted Jun 23, 2009, 3:01 pm

The const keyword is a funny one because it takes a while to wrap one's head around it, so it's typically confusing to newbies, but once you eventually get it, it's pretty awesome for many reasons. It took me a while to really appreciate it, but now I sorely miss it in other languages. :wink:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Pages:<< prev 1 next >>
Tags
[+]

Valid XHTML 1.1! Valid CSS!