www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - initializing char** from other functions

↑ ↓ ← 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
"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

 not the exact message, it's a translation)
 I also tried with bcc5.5, msvc6 and djgpp2.95, and djgpp was the only one

 cause that error. What's wrong with the code?

Jul 22 2003
↑ ↓ → 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
→ 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) {

^ 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];

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)

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. 

still the printf can print garbage. - Heinz
Jul 23 2003