www.digitalmars.com         C & C++   DMDScript  

c++ - initializing char** from other functions

reply Carlos Santander B. <Carlos_member pathlink.com> writes:
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
next sibling parent reply "Nic Tiger" <tiger7 progtech.ru> writes:
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?
Jul 22 2003
parent Carlos Santander B. <Carlos_member pathlink.com> writes:
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
prev sibling parent Heinz Saathoff <hsaat bre.ipnet.de> writes:
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
Jul 23 2003