473,413 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Very strange bug in C, please help

Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don't understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"

void error(char *name);

int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;


for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;

for (i=0;i<9;i++)
{

k[i]=10;
}

return(0);
}

void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);

exit(1);
}

Apr 10 '07 #1
11 1812
Vi*******@gmail.com said:

<snip>
double k[3];
<snip>
for (i=0;i<9;i++)
{

k[i]=10;
}
k has only three elements, and you are writing to all nine of them.
That's not good.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 10 '07 #2
Vi*******@gmail.com wrote:
Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don't understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"
If you have your own private "time.h", you probably shouldn't. The
standard header is <time.h>. You don't use either here, of course.
void error(char *name);

int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;
for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;
for (i=0;i<9;i++)
^^^^
As soon as you access k[3] you are in never-never land. There are no
such objects as k[3] through k[8] and you don't own the memory where
they might be.
{
k[i]=10;
}
return 0;
}

void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);
exit(1);
^^^^^^
Use one of the defined values for arguments to exit, namely, 0,
EXIT_SUCCESS, and EXIT_FAILURE. 1 has no standard meaning as an
argument for exit.
}
Apr 10 '07 #3
WOW thank you guys very much.

On Apr 10, 5:22 pm, Martin Ambuhl <mamb...@earthlink.netwrote:
VijaKh...@gmail.com wrote:
Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.
The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.
When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don't understand why. It seems that both v and k point to the same
address in memory.
This is very strange.
(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)
Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"

If you have your own private "time.h", you probably shouldn't. The
standard header is <time.h>. You don't use either here, of course.
void error(char *name);
int main (int argc, char **argv)
{
double v[3][3];
double k[3];
int i,j,l;
for (i=0;i<3;i++)
for(j=0;j<3;j++)
v[i][j]=4;
for (i=0;i<9;i++)

^^^^
As soon as you access k[3] you are in never-never land. There are no
such objects as k[3] through k[8] and you don't own the memory where
they might be.
{
k[i]=10;
}
return 0;
}
void error(char *name)
{
printf("usage: %s image.tiff \n\n",name);
exit(1);

^^^^^^
Use one of the defined values for arguments to exit, namely, 0,
EXIT_SUCCESS, and EXIT_FAILURE. 1 has no standard meaning as an
argument for exit.
}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Apr 10 '07 #4
Vi*******@gmail.com wrote:
WOW thank you guys very much.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Apr 10 '07 #5
On Tue, 10 Apr 2007 17:22:18 -0400, Martin Ambuhl
<ma*****@earthlink.netwrote:
>Vi*******@gmail.com wrote:
>Hi all,
I just write a very simple codes in C and vthere is a very strange bug
which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There is no
relationship between v and k but if I debug and watch the change of
the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s change
and are set to be equal some values of k[i]. Specifically, v[1][1] is
set to be 10, so is v[1][2] .
I don't understand why. It seems that both v and k point to the same
address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual Studio
6)

Thanks
#include <math.h>
#include <stdlib.h>
#include "time.h"

If you have your own private "time.h", you probably shouldn't. The
standard header is <time.h>. You don't use either here, of course.
The Embedded File Systems Library (EFSL) has its own private "time.h".

http://www.efsl.be/
http://sourceforge.net/projects/efsl/

Can you elaborate on why it "probably shouldn't"?

I've run into trouble using EFSL when someone else included both
<time.hand some EFSL headers that included "time.h" and the compiler
gave errors.

I'd never have my own private "time.h" and in fact I renamed the EFSL
"time.h" to "efsl_time.h" to fix the above-mentioned compiler errors.

Thanks
--
jay
Apr 11 '07 #6
jaysome said:
On Tue, 10 Apr 2007 17:22:18 -0400, Martin Ambuhl wrote:
>>Vi*******@gmail.com wrote:
<snip>
>>#include "time.h"

If you have your own private "time.h", you probably shouldn't. [...]

The Embedded File Systems Library (EFSL) has its own private "time.h".
<snip>
Can you elaborate on why it "probably shouldn't"?

I've run into trouble using EFSL when someone else included both
<time.hand some EFSL headers that included "time.h" and the compiler
gave errors.
That's why it probably shouldn't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 11 '07 #7
jaysome wrote:
Martin Ambuhl <ma*****@earthlink.netwrote:
>Vi*******@gmail.com wrote:
>>>
I just write a very simple codes in C and vthere is a very
strange bug which I cannot figure out why.

The first loop is for v[i][j], and the second for k[i]. There
is no relationship between v and k but if I debug and watch the
change of the variable after each command.

When the sencond loop happends for k[i], the values of v[i][j]s
change and are set to be equal some values of k[i]. Specifically,
v[1][1] is set to be 10, so is v[1][2]. I don't understand why.
It seems that both v and k point to the same address in memory.
This is very strange.

(I write this C code in Visual Studio 6 and debug it in Visual
Studio 6)

#include <math.h>
#include <stdlib.h>
#include "time.h"

If you have your own private "time.h", you probably shouldn't.
The standard header is <time.h>. You don't use either here, of
course.

The Embedded File Systems Library (EFSL) has its own private
"time.h".

http://www.efsl.be/
http://sourceforge.net/projects/efsl/

Can you elaborate on why it "probably shouldn't"?

I've run into trouble using EFSL when someone else included both
<time.hand some EFSL headers that included "time.h" and the
compiler gave errors.

I'd never have my own private "time.h" and in fact I renamed the
EFSL "time.h" to "efsl_time.h" to fix the above-mentioned
compiler errors.
You should check both .h files for such things as equal double
include prevention markers.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com

Apr 11 '07 #8
CBFalconer <cb********@yahoo.comwrites:
jaysome wrote:
>Martin Ambuhl <ma*****@earthlink.netwrote:
>>Vi*******@gmail.com wrote:
#include "time.h"

If you have your own private "time.h", you probably shouldn't.
....
>Can you elaborate on why it "probably shouldn't"?
My guess as to Martin's reasoning: because <time.hexists;
or to put it another way:
>I've run into trouble using EFSL when someone else included both
<time.hand some EFSL headers that included "time.h" and the
compiler gave errors.
Because that might happen. UB is wonderful stuff.
>I'd never have my own private "time.h" and in fact I renamed the
EFSL "time.h" to "efsl_time.h" to fix the above-mentioned
compiler errors.

You should check both .h files for such things as equal double
include prevention markers.
In Theory (wonderful place, but after visiting you often don't want to
go home) this Could Not Possibly be a problem because the Implementor
used something from his namespace in <time.hand the User who wrote
"time.h" (which became "efsl_time.h") used something from _his_
namespace (and therefore non-conflicting by definition) there.

mlp
Apr 11 '07 #9
"jaysome" <ja*****@hotmail.comwrote in message
news:or********************************@4ax.com...
Can you elaborate on why it "probably shouldn't"?

I've run into trouble using EFSL when someone else included both
<time.hand some EFSL headers that included "time.h" and the
compiler gave errors.
Be glad you got an error. Some compilers may consider "time.h" and <time.h>
to mean the same thing, or close enough to the same thing that it'll break
your code. More importantly, humans reading your code are almost certain to
make the same mistake, leading to maintenance headaches.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Apr 11 '07 #10
Mark L Pappin wrote:
CBFalconer <cb********@yahoo.comwrites:
>jaysome wrote:
.... snip ...
>>
>>I'd never have my own private "time.h" and in fact I renamed the
EFSL "time.h" to "efsl_time.h" to fix the above-mentioned
compiler errors.

You should check both .h files for such things as equal double
include prevention markers.

In Theory (wonderful place, but after visiting you often don't want to
go home) this Could Not Possibly be a problem because the Implementor
used something from his namespace in <time.hand the User who wrote
"time.h" (which became "efsl_time.h") used something from _his_
namespace (and therefore non-conflicting by definition) there.
He said he "renamed" the EFSL "time.h". It might have an internal
variable of the form "H_time_h" (or whatever) that has not been
altered, and which conflicts with <time.h>.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Apr 11 '07 #11
CBFalconer <cb********@yahoo.comwrites:
Mark L Pappin wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>jaysome wrote:
I'd never have my own private "time.h" and in fact I renamed the
EFSL "time.h" to "efsl_time.h" to fix the above-mentioned
compiler errors.

You should check both .h files for such things as equal double
include prevention markers.

In Theory (wonderful place, but after visiting you often don't want to
go home) this Could Not Possibly be a problem because the Implementor
used something from his namespace in <time.hand the User who wrote
"time.h" (which became "efsl_time.h") used something from _his_
namespace (and therefore non-conflicting by definition) there.

He said he "renamed" the EFSL "time.h". It might have an internal
variable of the form "H_time_h" (or whatever) that has not been
altered, and which conflicts with <time.h>.
The EFSL "time.h" now called "efsl_time.h", which is not part of the C
implementation, might have such an identifier but this Could Not
Possibly conflict with anything in <time.hbecause <time.hmay
contain only that which the standard says it may contain, and things
in the User's namespace are not in that list. If your <time.h>
contains such things, complain to your compiler vendor.

You know this stuff, Chuck. Furrfu.

mlp
Apr 11 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin Hampl | last post by:
Hi, I am using PostgreSQL 7.4, but I did have the same problem with the last version. I indexed the column word (defined as varchar(64)) using lower(word). If I use the following query,...
6
by: Sparticus | last post by:
I have a database that isn't very big. It has about 2400 rows in it. I try and do an update like this one below (it looks big, but it's really not if you look at it) : UPDATE jobs SET...
6
by: No_Excuses | last post by:
All, I am interested in reading the text of a web page and parsing it. After searching on this newgroup I decided to use the following: ******************************* START OF CODE...
1
by: davidw | last post by:
I encountered strange issues. I have code like this sqlReader = SqlHelper.ExecuteReader(connString, System.Data.CommandType.Text,sql); It calls Microsoft.ApplicationBlocks.Data to execute a...
5
by: franz | last post by:
hello, it's hard to me explain you this kind of error i have from .net. i developed a script in asp.net with VS.NET. in VS.NET (code behind) it's work perfect. now i copied the code in an aspx...
3
by: Chewbacca | last post by:
Hello, we have built a program that dynamically creates a web page based off SQL and other data fed content. The following is what is output. Anybody familliar with this sort of error? ...
3
by: settyv | last post by:
Hi, I need to generate PDF stream when i click on Linkbutton in datagrid ..At present i hardcoded the DMS Id and now it is working.But i need to pass DMS ID when click linkbutton.How can i do...
2
by: BarberCut | last post by:
hey guys! please help me answer these questions. i'm not a C++ programmer but my employer asked me to answer these questions ASAP. please help me. thanks. 1. What will I find in the value field...
0
by: Balamurugan Ranganathan | last post by:
I want to calculate the execution time of a sql query through C#.net this is to analysis two queries to compare their execution time it is very Urgent Please help me Please help Me ...
2
by: dotnetnovice | last post by:
Hi everybody! I have a problem and need help Here is the code. public void GetAllValues(Person V) { SqlConnection conn = new SqlConnection(ConnectionString); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.