Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 22, 2003 initializing char** from other functions | ||||
---|---|---|---|---|
| ||||
Hello. I have something that looks like this: #include <stdio.h> void foo(char **s) { s=new char*[4]; for (int i=0;i<4;i++) s[i]=new char[20]; } void main() { char **bar; foo(bar); for (int i=0;i<4;i++) printf("%s\n",bar[i]); } Why do I get an error about memory not being able to be read? (Sorry if that's not the exact message, it's a translation) I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only one not to cause that error. What's wrong with the code? |
July 22, 2003 Re: initializing char** from other functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | You should write ***: void foo(char ***s) { *s=new char*[4]; for (int i=0;i<4;i++) (*s)[i]=new char[20]; } void main() { char **bar; foo(&bar); } What you did is initializing stack variable, which is not returned from function. So, after function foo(...) executed, the parameter bar remained unchanged. DJPP should not differ from other compilers, because your code certainly contains bug. Nic Tiger. "Carlos Santander B." <Carlos_member@pathlink.com> wrote in message news:bfk11b$29dm$1@digitaldaemon.com... > Hello. I have something that looks like this: > > #include <stdio.h> > void foo(char **s) { > s=new char*[4]; > for (int i=0;i<4;i++) > s[i]=new char[20]; > } > void main() { > char **bar; > foo(bar); > for (int i=0;i<4;i++) > printf("%s\n",bar[i]); > } > > Why do I get an error about memory not being able to be read? (Sorry if that's > not the exact message, it's a translation) > I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only one not to > cause that error. What's wrong with the code? > > |
July 22, 2003 Re: initializing char** from other functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nic Tiger | Thank you, very much. In article <bfk1uq$2abo$1@digitaldaemon.com>, Nic Tiger says... > >You should write ***: > >void foo(char ***s) { >*s=new char*[4]; >for (int i=0;i<4;i++) >(*s)[i]=new char[20]; >} >void main() { >char **bar; >foo(&bar); >} > >What you did is initializing stack variable, which is not returned from >function. So, after function foo(...) executed, the parameter bar remained >unchanged. >DJPP should not differ from other compilers, because your code certainly >contains bug. > >Nic Tiger. > |
July 23, 2003 Re: initializing char** from other functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. schrieb... > I have something that looks like this: > > #include <stdio.h> > void foo(char **s) { void foo(char **&s) { ^ When compiled as C++ module you can declare s as a reference > s=new char*[4]; > for (int i=0;i<4;i++) > s[i]=new char[20]; you have allocated storage but not initialized it. Change the for-loop to something like this: for(int i=0; i<4; ++i) { s[i] = new char[20]; s[i][0] = '0'; // empty string // or sprintf(s[i], "Line %d", i); // Line 0 to Line 3 }//for > } > void main() { > char **bar; > foo(bar); > for (int i=0;i<4;i++) > printf("%s\n",bar[i]); > } > > Why do I get an error about memory not being able to be read? (Sorry if that's not the exact message, it's a translation) The storage allocated in your original program was lost because bar is passed by value. bar as a auto-variable is not initialized by default and can contain a garbage value. Accessing this pointer (printf does the access) in protected mode usually crashes the program. > I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only one not to cause that error. The not initialized pointer might contain a valid address by chance. But still the printf can print garbage. - Heinz |
Copyright © 1999-2021 by the D Language Foundation