digitalmars.D.learn - access violation error
- Michael P. (55/55) Aug 13 2008 /* junior304.d
- Wyverex (6/67) Aug 13 2008 quick fix is to use printf
- Koroskin Denis (33/98) Aug 13 2008 =
- Wyverex (2/12) Aug 13 2008 Worked on my machine!! Though I am using tangobos....
- Michael P. (4/108) Aug 13 2008 Sorry I made you angry...
-
Koroskin Denis
(22/137)
Aug 13 2008
On Thu, 14 Aug 2008 04:21:05 +0400, Michael P.
... - Michael P. (2/25) Aug 13 2008 Okay, thanks, I'll be using readf from now on. :D
- Steven Schveighoffer (17/128) Aug 14 2008 Yes. both scanf and din.readf are variadic functions (meaning, they tak...
- Steven Schveighoffer (8/62) Aug 13 2008 Most likely, this is failing because you are using scanf. Stop using it...
- Gide Nwawudu (12/17) Sep 17 2008 The code above causes an Access Violaion in D2. The following works.
/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); char[][] adjectives; //array of strings to hold the adjectives adjectives.length = 6; //max number that user enters is 5, 6 to be safe char[][] nouns; //same as above, but for nouns nouns.length = 6; //same as adjectives reason //get adjectives first adjectives { scanf( "%s", &adjectives[ i ] ); } for ( int i = 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } //print out all possible similies for ( int i = 0; i < numberOfAdjectives; i++ ) { for ( int j = 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a similie writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!
Aug 13 2008
Michael P. wrote:/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); char[][] adjectives; //array of strings to hold the adjectives adjectives.length = 6; //max number that user enters is 5, 6 to be safe char[][] nouns; //same as above, but for nouns nouns.length = 6; //same as adjectives reason //get adjectives first adjectives { scanf( "%s", &adjectives[ i ] ); } for ( int i = 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } //print out all possible similies for ( int i = 0; i < numberOfAdjectives; i++ ) { for ( int j = 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a similie writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't return strings in a format writef likes... I though theres a readf but cant find documentation on it...
Aug 13 2008
On Thu, 14 Aug 2008 01:23:05 +0400, Wyverex <wyverex.cypher gmail.com> = wrote:Michael P. wrote:=/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible =similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); ==char[][] adjectives; //array of strings to hold the adjectives adjectives.length =3D 6; //max number that user enters is 5, 6 to be=safe char[][] nouns; //same as above, but for nouns nouns.length =3D 6; //same as adjectives reason =//get adjectives first for ( int i =3D 0; i < numberOfAdjectives; i++ ) //get the specified=of adjectives { scanf( "%s", &adjectives[ i ] ); } =for ( int i =3D 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } =ie//print out all possible similies for ( int i =3D 0; i < numberOfAdjectives; i++ ) { for ( int j =3D 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a simil=writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any =Your code is seriously broken! The following line is wrong, for example:reason why? Input is: 3 2 Easy Soft Smart pie rock error!scanf( "%s", &nouns[ i ] );What are you doing here? Are you out of your mind? You are passing a = pointer to a char[]* to a function that accept a char*. Your array will = be = overwritten with whatever you will type. It is wrong, wrong, wrong! I think that you don't understand something. First of all, what is a = char[]? It is a struct that consists of a array size and a pointer: struct CharArray { size_t size; char* ptr; } typeof(nounts[i]) is a char[], typeof(&nouns[i]) is char[]*. You pass th= is = pointer to a function that accepts a pointer to a preallocated buffer fo= r = a string to be written to. A quick fix would be to: 1) allocate a buffer: nouns[i] =3D new char[64]; 2) pass a pointer to the preallocated buffer: scanf("%s", nouns[i].ptr); A good fix would be to use readf instead. I didn't use it, but this shou= ld = be sufficient: readf("%s", &nouns[i]);quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't =return strings in a format writef likes... I though theres a readf but cant find documentation on it...Test before posting, please!
Aug 13 2008
Koroskin Denis wrote:Worked on my machine!! Though I am using tangobos....quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't return strings in a format writef likes... I though theres a readf but cant find documentation on it...Test before posting, please!
Aug 13 2008
Koroskin Denis Wrote:On Thu, 14 Aug 2008 01:23:05 +0400, Wyverex <wyverex.cypher gmail.com> wrote:Sorry I made you angry... I changed all the scanfs to din.readfs and everything worked okay... does that solve my passing a pointer to a string to function that accepts a pointer to a char? I guess I should check the documentation for some stuff like that.Michael P. wrote:Your code is seriously broken! The following line is wrong, for example:/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); char[][] adjectives; //array of strings to hold the adjectives adjectives.length = 6; //max number that user enters is 5, 6 to be safe char[][] nouns; //same as above, but for nouns nouns.length = 6; //same as adjectives reason //get adjectives first of adjectives { scanf( "%s", &adjectives[ i ] ); } for ( int i = 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } //print out all possible similies for ( int i = 0; i < numberOfAdjectives; i++ ) { for ( int j = 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a similie writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!scanf( "%s", &nouns[ i ] );What are you doing here? Are you out of your mind? You are passing a pointer to a char[]* to a function that accept a char*. Your array will be overwritten with whatever you will type. It is wrong, wrong, wrong! I think that you don't understand something. First of all, what is a char[]? It is a struct that consists of a array size and a pointer: struct CharArray { size_t size; char* ptr; } typeof(nounts[i]) is a char[], typeof(&nouns[i]) is char[]*. You pass this pointer to a function that accepts a pointer to a preallocated buffer for a string to be written to. A quick fix would be to: 1) allocate a buffer: nouns[i] = new char[64]; 2) pass a pointer to the preallocated buffer: scanf("%s", nouns[i].ptr); A good fix would be to use readf instead. I didn't use it, but this should be sufficient: readf("%s", &nouns[i]);quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't return strings in a format writef likes... I though theres a readf but cant find documentation on it...Test before posting, please!
Aug 13 2008
On Thu, 14 Aug 2008 04:21:05 +0400, Michael P. <baseball.mjp gmail.com> = = wrote:Koroskin Denis Wrote:leOn Thu, 14 Aug 2008 01:23:05 +0400, Wyverex <wyverex.cypher gmail.com=wrote:Michael P. wrote:/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possib=similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); =bechar[][] adjectives; //array of strings to hold the adjectives adjectives.length =3D 6; //max number that user enters is 5, 6 to=safe char[][] nouns; //same as above, but for nouns nouns.length =3D 6; //same as adjectives reason =//get adjectives first for ( int i =3D 0; i < numberOfAdjectives; i++ ) //get the specif=of adjectives { scanf( "%s", &adjectives[ i ] ); } =for ( int i =3D 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } =//print out all possible similies for ( int i =3D 0; i < numberOfAdjectives; i++ ) { for ( int j =3D 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a =ll =similieYour code is seriously broken! The following line is wrong, for example:writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!scanf( "%s", &nouns[ i ] );What are you doing here? Are you out of your mind? You are passing a pointer to a char[]* to a function that accept a char*. Your array wi==be overwritten with whatever you will type. It is wrong, wrong, wrong! I think that you don't understand something. First of all, what is a char[]? It is a struct that consists of a array size and a pointer: struct CharArray { size_t size; char* ptr; } typeof(nounts[i]) is a char[], typeof(&nouns[i]) is char[]*. You pass==this pointer to a function that accepts a pointer to a preallocated buffer=for a string to be written to. A quick fix would be to: 1) allocate a buffer: nouns[i] =3D new char[64]; 2) pass a pointer to the preallocated buffer: scanf("%s", nouns[i].ptr); A good fix would be to use readf instead. I didn't use it, but this =No, you didn't. It just proves that C functions are unsafe and hard to u= se.should be sufficient: readf("%s", &nouns[i]);Sorry I made you angry...quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't return strings in a format writef likes... I though theres a readf but cant find documentation on it...Test before posting, please!I changed all the scanfs to din.readfs and everything worked okay... =does that solve my passing a pointer to a string to function that =accepts a pointer to a char?Yes, I think.I guess I should check the documentation for some stuff like that.Well, you should understand what are you doing when using C library. It = is = unsafe and very error-prone. In fact, you were doing fine, just used wrong function. Compare the = following: char[] buffer; scanf("%s", &buffer); vs char[] buffer; readf("%s", = &buffer); They are almost the same, but D version (readf) is an order of magnitude= = smarter and safer because of type-safe variadic functions.
Aug 13 2008
Okay, thanks, I'll be using readf from now on. :D -Michael P.Sorry I made you angry...No, you didn't. It just proves that C functions are unsafe and hard to use.I changed all the scanfs to din.readfs and everything worked okay... does that solve my passing a pointer to a string to function that accepts a pointer to a char?Yes, I think.I guess I should check the documentation for some stuff like that.Well, you should understand what are you doing when using C library. It is unsafe and very error-prone. In fact, you were doing fine, just used wrong function. Compare the following: char[] buffer; scanf("%s", &buffer); vs char[] buffer; readf("%s", &buffer); They are almost the same, but D version (readf) is an order of magnitude smarter and safer because of type-safe variadic functions.
Aug 13 2008
"Michael P." wroteKoroskin Denis Wrote:Yes. both scanf and din.readf are variadic functions (meaning, they take a variable number and type of arguments). However, readf is D variadic, meaning that along with the addresses of all the variables you want to read, it also gets a TypeInfo telling the function which type each argument is. So it can check and make sure you are passing the right type. In fact, I think %s is a catch all which means for readf to use the TypeInfo to tell how to scan the arguments in. The scanf function is C variadic, and so does not have this ability. So when you are passing a pointer and saying 'scan in a char*', it has to take your word for it that you are actually passing a char ** (even though you were passing a char[] *) Note that char[] is not the same as char*. The former is a struct with a char* and a length in it. The fact that it worked in your code at all is a miracle :) Glad you got it working! -SteveOn Thu, 14 Aug 2008 01:23:05 +0400, Wyverex wrote:I changed all the scanfs to din.readfs and everything worked okay... does that solve my passing a pointer to a string to function that accepts a pointer to a char?Michael P. wrote:Your code is seriously broken! The following line is wrong, for example:/* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); char[][] adjectives; //array of strings to hold the adjectives adjectives.length = 6; //max number that user enters is 5, 6 to be safe char[][] nouns; //same as above, but for nouns nouns.length = 6; //same as adjectives reason //get adjectives first of adjectives { scanf( "%s", &adjectives[ i ] ); } for ( int i = 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } //print out all possible similies for ( int i = 0; i < numberOfAdjectives; i++ ) { for ( int j = 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a similie writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!scanf( "%s", &nouns[ i ] );What are you doing here? Are you out of your mind? You are passing a pointer to a char[]* to a function that accept a char*. Your array will be overwritten with whatever you will type. It is wrong, wrong, wrong! I think that you don't understand something. First of all, what is a char[]? It is a struct that consists of a array size and a pointer: struct CharArray { size_t size; char* ptr; } typeof(nounts[i]) is a char[], typeof(&nouns[i]) is char[]*. You pass this pointer to a function that accepts a pointer to a preallocated buffer for a string to be written to. A quick fix would be to: 1) allocate a buffer: nouns[i] = new char[64]; 2) pass a pointer to the preallocated buffer: scanf("%s", nouns[i].ptr); A good fix would be to use readf instead. I didn't use it, but this should be sufficient: readf("%s", &nouns[i]);quick fix is to use printf printf( "%s as %s", &adjectives[ i ], &nouns[ j ] ); i think (not sure don't really use phobos that much) scanf doesn't return strings in a format writef likes... I though theres a readf but cant find documentation on it...Test before posting, please!
Aug 14 2008
Most likely, this is failing because you are using scanf. Stop using it :) Use din.readf instead: import std.cstream; char[] str; din.readf("%s", &str); -Steve "Michael P." <baseball.mjp gmail.com> wrote in message news:g7vi50$29f0$1 digitalmars.com.../* junior304.d 2008 junior problem 3 Smile with similies Ask for 2 numbers; the number of adjectives and nouns After you get the numbers and nouns and adjectives, out all possible similies August 12th, 2008 */ //Imports import std.stdio; /***** *Main* *****/ void main() { int numberOfAdjectives; scanf( "%d", &numberOfAdjectives ); int numberOfNouns; scanf( "%d", &numberOfNouns ); char[][] adjectives; //array of strings to hold the adjectives adjectives.length = 6; //max number that user enters is 5, 6 to be safe char[][] nouns; //same as above, but for nouns nouns.length = 6; //same as adjectives reason //get adjectives first adjectives { scanf( "%s", &adjectives[ i ] ); } for ( int i = 0; i < numberOfNouns; i++ ) { scanf( "%s", &nouns[ i ] ); } //print out all possible similies for ( int i = 0; i < numberOfAdjectives; i++ ) { for ( int j = 0; j < numberOfNouns; j++ ) { //for every adjective, place a noun after and turn it into a similie writefln( "%s as %s", adjectives[ i ], nouns[ j ] ); } } } I get an accessviolation when I enter the last 2 for loops; any reason why? Input is: 3 2 Easy Soft Smart pie rock error!
Aug 13 2008
On Wed, 13 Aug 2008 17:35:42 -0400, "Steven Schveighoffer" <schveiguy yahoo.com> wrote:Most likely, this is failing because you are using scanf. Stop using it :) Use din.readf instead: import std.cstream; char[] str; din.readf("%s", &str);The code above causes an Access Violaion in D2. The following works. import std.stdio : writeln; import std.cstream : din; int main() { char[] str; din.readf(&str); writeln(str); return 0; } Gide
Sep 17 2008