www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assert Error

reply Jerry <Jerry_member pathlink.com> writes:
The following code runs fine, but gave an assert error at the end, does anyone
know why?

// this code gave "Error: AssertError Failure test2(12)"
import std.stdio;

int main()
{
char[][] list;
list.length = 5;
for(int i =0; i < 5; i++) {
list[i] = "hello";
}

for(int i=0; i < 5; i++)
writefln(list[i]);
}


Ok, I know this is a D forum, but what inspired me to write the above program
was the following C program, which gives 

"""3 [main] a 3348 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
20385 [main] a 3348 open_stackdumpfile: Dumping stack trace to
a.exe.stackdump"""

on gcc, and on VS6.0, it simply says "Visual Studio has encountered an error and
needs to close"

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main()
{      
int i;
char** list;
list = (char**)malloc(5*sizeof(char*));
for(i=0; i < 5; i++) {
list[i] = (char*)malloc(10*sizeof(char));
strcpy(list[i], "hello");
}

for(i=0; i < 5; i++)
printf("%s\n", list[i]);
/*freeing pointers here*/
}

What really baffles me is that if I replace 
"  for(i=0; i < 5; i++)
printf("%s\n", list[i]);  "

with 
" printf("%s\n", list[0]);
printf("%s\n", list[1]);
printf("%s\n", list[2]);
printf("%s\n", list[3]);
printf("%s\n", list[4]); "

the program runs fine. No errors, and it actually gives the right output.  
Does anyone know why?

-Jerry
Aug 06 2005
parent reply Vathix <chris dprogramming.com> writes:
On Sat, 06 Aug 2005 18:29:58 -0400, Jerry <Jerry_member pathlink.com>  
wrote:

 The following code runs fine, but gave an assert error at the end, does  
 anyone
 know why?

 // this code gave "Error: AssertError Failure test2(12)"
 import std.stdio;

 int main()
 {
 char[][] list;
 list.length = 5;
 for(int i =0; i < 5; i++) {
 list[i] = "hello";
 }

 for(int i=0; i < 5; i++)
 writefln(list[i]);
 }

Return something from main.
Aug 06 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Vathix wrote:

 The following code runs fine, but gave an assert error at the end, does anyone
 know why?
 
 // this code gave "Error: AssertError Failure test2(12)"
 import std.stdio;
 
 int main()
 {
 char[][] list;
 list.length = 5;
 for(int i =0; i < 5; i++) {
 list[i] = "hello";
 }
 
 for(int i=0; i < 5; i++)
 writefln(list[i]);
 }

 Return something from main.

As much as Walter's tried to pretend otherwise, the OP's code is invalid, and it's a bug that the compiler doesn't report an error. http://www.digitalmars.com/d/statement.html#return "At least one return statement is required if the function specifies a return type that is not void." This is one of the most common questions on these 'groups. Time to fix? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on on the 'group where everyone may benefit.
Aug 08 2005