c++ - initializing char** from other functions
- Carlos Santander B. (17/17) Jul 22 2003 Hello. I have something that looks like this:
- Nic Tiger (20/37) Jul 22 2003 You should write ***:
- Carlos Santander B. (2/18) Jul 22 2003
- Heinz Saathoff (19/38) Jul 23 2003 void foo(char **&s) {
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?
Jul 22 2003
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 ifthat'snot the exact message, it's a translation) I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only onenot tocause that error. What's wrong with the code?
Jul 22 2003
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.
Jul 22 2003
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 references=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
Jul 23 2003