GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Feb-2004, 19:55
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
Question

Assertion Error


Hey
I have a page of code, now its not complete, but basically it will be a matrices calculator. But I am stuck on just entering the matrices. The assertion error (which ive read up on and says its basically a pointer error) is labelled below. Thing is this code compiled fine in visual studio at uni, and im using the same version of visual studio here at home. So i dont know why the error. I'm a relative beginner, and only just been taught the basics of pointers and structures so bare with me for ugly coding etc. Any help would be greatly appreciated

Mitch

CPP / C++ / C Code:

int enter_matrix(matrix_struct *matrix_a, matrix_struct *matrix_b)
{


    int choice,no_of_rows,no_of_columns, row, col;
    int* row_p,column_p;
    int** matrix_a_p, matrix_b_p, matrix_c_p;

    int no_of_columns_a, no_of_rows_a;


    //free previous memory to avoid leaks.

    free(matrix_a->matrix);
    free(matrix_b->matrix);



    printf("\nPlease enter number of rows in Matrix a: ");
            scanf("%d",&matrix_a->no_of_rows);
            printf("\n\nPlease enter number of columns in Matrix: ");
            scanf("%d",&matrix_a->no_of_columns);


            //allocate memory for rows

            
			matrix_a->matrix=(float**)(malloc((matrix_a->no_of_rows)*sizeof(float*)));

            //allocate memory for each row of the matrix

            for(row=0;row<(matrix_a->no_of_rows);row++)
{
             matrix_a->matrix[row]=(float*)(malloc((matrix_a->no_of_columns)*sizeof(float))); //error
}

//formatting seems to have been mucked up by the pasting :S


            //enter values into matrix

            for(row=0; row<(matrix_a->no_of_rows); row++)
            {
                for(col=0; col<(matrix_a->no_of_columns); col++)
                {
                    printf("\nPlease enter value for row %d and column %d:",row,col);
                    scanf("%f",&matrix_a->matrix[row][col]);
                }
            }

            printf("You entered:\n\n");

            for(row=0;row<(matrix_a->no_of_rows); row++)
            {
                printf("\n");

                for(col=0;col<(matrix_a->no_of_columns);col++)
                    printf("%f ",matrix_a->matrix[row][col]);
            }
 



Thats not all of it, just one section of the code,

Cheers for any help





P.S. I've seen a lot of names from cprogramming.com, has the site shut down?
Attached Files
File Type: txt code.txt (3.5 KB, 8 views)
Last edited by dsmith : 19-Feb-2004 at 22:58. Reason: Changed code highlighting to C highlighting
  #2  
Old 19-Feb-2004, 19:59
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
By the way, the attatched code.txt is the full code, not the same excerpt, just so you know if i'm making my declarations right etc

Cheers again,

Mitch
  #3  
Old 19-Feb-2004, 20:30
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I compiled and ran the code. It worked just fine. The code looks fine. I can't wait to start learning that stuff. We learned all the basics first semester and finished with structs and typedefs. All that stuff is pretty simple. I can't wait to get into linked lists and dynamic memory allocation so I can do some serious stuff... also want to learn to send and receive data across a network.
  #4  
Old 19-Feb-2004, 20:52
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Now that I think of it, I don't even have to wait for next semester. I'll just do it out of the book I bought for last semester. It had all that stuff in it, but we didn't even start covering classes. We're starting with classes at the beginning of next semester, so I'll be ready for that. I've already written a couple of them.
  #5  
Old 19-Feb-2004, 23:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Mitch. I may have to look closer at your code. Malloc can be tricky.

One thing that jumped out at me is this:
CPP / C++ / C Code:
    //free previous memory to avoid leaks.

    free(matrix_a->matrix);
    free(matrix_b->matrix);
 

Question: Is there allocated memory in these matrices for sure every time you call this? free is unpredictable (in some compilers) if there is not an associated malloc called before or if the ptr is not specifically set to null:

Quote:

free() frees the memory space pointed to by ptr, which
must have been returned by a previous call to malloc(),
calloc() or realloc(). Otherwise, or if free(ptr) has
already been called before, undefined behaviour occurs.
If ptr is NULL, no operation is performed.

One thing that I do is put in a couple of fprintf statements to see exactly where things are breaking:
CPP / C++ / C Code:
for(row=0;row<(matrix_a->no_of_rows);row++){
  fprintf(stderr,"Loop %d\n",row);
  matrix_a->matrix[row]=(float* )(malloc((matrix_a->no_of_columns)*sizeof(float))); 
}
Is it breaking the first time through always?
  #6  
Old 20-Feb-2004, 10:21
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
no the first set of "free"'s wont have had a memory allocation happen before it, i just cant think of another (remotely efficient) way of freeing the memory allocation if i need to enter the matrix once, then realise a mistake has been made and enter them again. It needs to free that memory. Where can i put those lines to free any duplicate memory allocation whilst not having them run first time? maybe an if statement set if(counter>0){free_memory} and have counter set to 0 but counter++ everytime the function is called...


It IS breaking forst time every time. So it could well be that...

Funny though that it would compile on one visual studio but not on another which should be exactly the same...

Cheers to both for looking at it, and aaroncohn, you'll get you're chance, and the difficulty rating shoots up sharpish

Thanks a lot, now all i have to do is try and get the other functions to work...

Mitch
  #7  
Old 20-Feb-2004, 10:30
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
I put the if statement around the free's and it compiles fine now, thanks a lot dsmith. {i didnt put a counter, just an marker initialised to 0 and a marker=1 after the if statement (shockingly it was if(marker==1) ) }

Blinder, chers gents.

Mitch
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in ukrspp21 MySQL / PHP Forum 31 04-Jul-2006 22:41
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 07:25
error during program rjd72285 C++ Forum 0 11-Nov-2003 19:49
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 21:10
CD burner, focus or tracking error ShingoDrrazz Computer Hardware Forum 1 09-Aug-2003 17:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.