The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
What's the error?
Discuss What's the error? in the C Programming forum on Dev Shed. What's the error? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 13th, 2013, 02:51 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
|
What's the error?
Hi,
I have the following array, which doesn't want work. After I debug, it prints out nothing and shows an application error.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nr=4, nc=6;
float** pmtx = (float**)malloc(nr*sizeof(float*));
for (int i=0; i<nr; ++i)
pmtx[i] = (float*)malloc(nc*sizeof(float));
for (int i=0; i<nr; ++i)
for (int j=0; i<nc; ++j)
pmtx[i][j] = (i+1)*(j+1);
for (int i=0; i<nr; ++i)
{
for (int j=0; j<nc; ++j)
printf("%8.lf", pmtx[i][j]);
printf("\n");
}
printf("\n");
for (int i=0; i<nr; ++i)
free (pmtx[i]);
free (pmtx);
return 0;
}
|

January 13th, 2013, 03:05 PM
|
 |
Contributed User
|
|
|
|
Here is why you should use code tags when posting code.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nr = 4, nc = 6;
float **pmtx = (float **) malloc(nr * sizeof(float *));
for (int i = 0; i < nr; ++i)
pmtx[i] = (float *) malloc(nc * sizeof(float));
for (int i = 0; i < nr; ++i)
for (int j = 0; i < nc; ++j)
pmtx[i][j] = (i + 1) * (j + 1);
for (int i = 0; i < nr; ++i) {
for (int j = 0; j < nc; ++j)
printf("%8.lf", pmtx[i][j]);
printf("\n");
}
printf("\n");
for (int i = 0; i < nr; ++i)
free(pmtx[i]);
free(pmtx);
return 0;
}
Now, debugging is more than just running the code and watching it crash.
Debugging means run the code in the debugger and have a good look around to see what caused the problem
Code:
$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/sc/Documents/a.out...done.
(gdb) run
Starting program: /home/sc/Documents/a.out
Program received signal SIGSEGV, Segmentation fault.
0x000000000040067b in main () at foo.c:14
14 pmtx[i][j] = (i + 1) * (j + 1);
(gdb) print i
$1 = 0
(gdb) print j
$2 = 33776
(gdb) list
9 for (int i = 0; i < nr; ++i)
10 pmtx[i] = (float *) malloc(nc * sizeof(float));
11
12 for (int i = 0; i < nr; ++i)
13 for (int j = 0; i < nc; ++j)
14 pmtx[i][j] = (i + 1) * (j + 1);
15
16 for (int i = 0; i < nr; ++i) {
17 for (int j = 0; j < nc; ++j)
18 printf("%8.lf", pmtx[i][j]);
(gdb)
The first output shows the segfault, AND the line of code causing the problem.
OK, so i and j are involved, so print them out
Woah - WTF is that huge number in j, where did that come from!?
Now print the code around the offending line - do you see it yet?
|

January 13th, 2013, 03:30 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
Thank you, and sorry for not formatting.
Code:
#include <stdio.h>
void main ()
{
printf("this is how to use this code tag, cool\n");
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|