473,789 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to correct the code segment???????? ?????

for(i=0;i<256;i ++)
{
hist[i]=0;
}
for(i=0;i<heigh t;i++)
{
for(j=0;j<width ;j++)
{
k=(long int)rgb1[i][j];
hist[k]=hist[k]+1;
}
}

for(i=0;i<256;i ++)
{
if(hist[i]!=0)
tmp++;
else continue;
}

sum_hist=(int** )malloc(tmp*siz eof(int*));
for(i=0;i<tmp;i ++)
sum_hist[i]=(int*)malloc(2 *sizeof(int));

for(i=0;i<256;i ++)
{
if(hist[i]!=0)
{
//printf("%d %d\n",i,hist[i]);
sum_hist[i][0]=(int)i;
sum_hist[i][1]=(int)hist[i];
printf("%d %d\n",sum_hist[i][0],sum_hist[i][1]);

}
else continue;
}
for(i=0;i<tmp;i ++)
printf("%d\n",s um_hist[i][1]);

In the above code segment sum_hist is a double pointer of type long
int...the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...
Sep 13 '08 #1
13 1387
On 2008-09-13, biplab <ge*******@gmai l.comwrote:
for(i=0;i<256;i ++)
{
hist[i]=0;
}
You need to fix your formatting. 2-space indents are
recommended for posting on Usenet. In any case, be
consistent, and use whitespace around operators.

Also, this could be replaced with:
memset(hist, 0, 256);

Or potentially:
memset(hist, 0, sizeof hist);
for(i=0;i<heigh t;i++)
{
for(j=0;j<width ;j++)
{
k=(long int)rgb1[i][j];
Why this cast? The way you use k here, all its logical
values could fit into an unsigned char. Surely then
whatever type it already was, is sufficient?
hist[k]=hist[k]+1;
}
}

for(i=0;i<256;i ++)
{
if(hist[i]!=0)
tmp++;
else continue;
The else line is superfluous. Fix your formatting.
}

sum_hist=(int** )malloc(tmp*siz eof(int*));
Don't cast the return of malloc(). Don't use the return of
malloc() without checking it for NULL. Don't pass types to
sizeof.
for(i=0;i<tmp;i ++)
sum_hist[i]=(int*)malloc(2 *sizeof(int));
Ditto.
for(i=0;i<256;i ++)
{
if(hist[i]!=0)
{
//printf("%d %d\n",i,hist[i]);
Don't use single-line comments on usenet, especially when
you are using a random and excessive indentation scheme.
sum_hist[i][0]=(int)i;
sum_hist[i][1]=(int)hist[i];
What's with these casts?
printf("%d %d\n",sum_hist[i][0],sum_hist[i][1]);

}
else continue;
This line is superfluous.
}
for(i=0;i<tmp;i ++)
printf("%d\n",s um_hist[i][1]);

In the above code segment sum_hist is a double pointer of type long
int...the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...
Fix the above issues and post a compilable "segment" of code. Then
we will see what the problem is.

--
Andrew Poelstra ap*******@wpsof tware.com
To email me, use the above email addresss with .com set to .net
Sep 13 '08 #2
On 2008-09-13, biplab <ge*******@gmai l.comwrote:
>
<code snipped>

In the above code segment sum_hist is a double pointer of type long
int...the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...
Here is your code, corrected for the (mainly stylistic) issues I
mentioned else thread. It is abundantly clear to me now what the
problem is. I hope it is also clear to you:

/* Begin uncompilable segment */

memset(hist, 0, 256);

for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{
k = rgb1[i][j];
if(hist[k] == 0)
++count;
++hist[k];
}

sum_hist = malloc(count * sizeof *sum_hist);
for(i = 0; i < count; i++)
sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);

for(i = 0; i < 256; i++)
if(hist[i] != 0)
{
/* printf("%d %d\n", i, hist[i]); */
sum_hist[i][0] = i;
sum_hist[i][1] = hist[i];
printf("%d %d\n", sum_hist[i][0], sum_hist[i][1]);
}

for(i = 0; i < count; i++)
printf("%d\n", sum_hist[i][1]);

/* End segment */

--
Andrew Poelstra ap*******@wpsof tware.com
To email me, use the above email addresss with .com set to .net
Sep 13 '08 #3
On 2008-09-13, Andrew Poelstra <ap*******@supe rnova.homewrote :
On 2008-09-13, biplab <ge*******@gmai l.comwrote:
>>
<code snipped>

In the above code segment sum_hist is a double pointer of type long
int...the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...

Here is your code, corrected for the (mainly stylistic) issues I
mentioned else thread. It is abundantly clear to me now what the
problem is. I hope it is also clear to you:

/* Begin uncompilable segment */

memset(hist, 0, 256);

for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{
k = rgb1[i][j];
if(hist[k] == 0)
++count;
++hist[k];
}

sum_hist = malloc(count * sizeof *sum_hist);
for(i = 0; i < count; i++)
sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);
I forgot to add checks that malloc() does not return NULL.
for(i = 0; i < 256; i++)
if(hist[i] != 0)
{
/* printf("%d %d\n", i, hist[i]); */
sum_hist[i][0] = i;
To the OP, what happens when i exceeds count?
sum_hist[i][1] = hist[i];
printf("%d %d\n", sum_hist[i][0], sum_hist[i][1]);
}

for(i = 0; i < count; i++)
printf("%d\n", sum_hist[i][1]);

/* End segment */

--
Andrew Poelstra ap*******@wpsof tware.com
To email me, use the above email addresss with .com set to .net
Sep 13 '08 #4
Andrew Poelstra <ap*******@supe rnova.homewrite s:
On 2008-09-13, biplab <ge*******@gmai l.comwrote:
>>
<code snipped>

In the above code segment sum_hist is a double pointer of type long
int...the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...

Here is your code, corrected for the (mainly stylistic) issues I
mentioned else thread. It is abundantly clear to me now what the
problem is. I hope it is also clear to you:

/* Begin uncompilable segment */

memset(hist, 0, 256);
Horrible. Code fails review.
>
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{
k = rgb1[i][j];
if(hist[k] == 0)
++count;
++hist[k];
}

sum_hist = malloc(count * sizeof *sum_hist);
for(i = 0; i < count; i++)
sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);
Silly to use sizeof on a variable element. Overly clever and liable to
mislead. It also assumes optimization will spot the obvious.
>
for(i = 0; i < 256; i++)
minus another point for hard coding numbers.

if(hist[i] != 0)
{
/* printf("%d %d\n", i, hist[i]); */
sum_hist[i][0] = i;
sum_hist[i][1] = hist[i];
printf("%d %d\n", sum_hist[i][0], sum_hist[i][1]);
}

for(i = 0; i < count; i++)
printf("%d\n", sum_hist[i][1]);

/* End segment */
The whole thing is weak. It wreaks of needing a structure in place
anyway.
Sep 13 '08 #5
In article <ga**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote:
> sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);
>Silly to use sizeof on a variable element. Overly clever and liable to
mislead.
I don't see why. How would you be misled?
>It also assumes optimization will spot the obvious.
What optimisation are you talking about? The result of sizeof is (in
the absence of variable-length arrays) a constant, and the constant
will be just as constant as if you had written, say, the type name.
It will surely have been determined before code generation.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Sep 13 '08 #6
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <ga**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote:
>> sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);
>>Silly to use sizeof on a variable element. Overly clever and liable to
mislead.

I don't see why. How would you be misled?
It suggests to the naive programmer that there are variable sized
elements. Of course there can not be. We know that. The reader should
know that. But why state it in this way? Just "silly". Not the worst in
the world I agree. But stylewise I personally do not like it.
>
>>It also assumes optimization will spot the obvious.

What optimisation are you talking about? The result of sizeof is (in
the absence of variable-length arrays) a constant, and the constant
will be just as constant as if you had written, say, the type name.
It will surely have been determined before code generation.

One would hope so. But its not a feat of genius to simply make it struct
and use a static sizeof.

*shrug* More a style thing. Can you be sure that

2 * sizeof h[i]

is compiled to the same as

2 * sizeof h[0]

for example? Pretty much yes, but why introducde confusion., Struct that
sucker anyway.

sizeof(elementS truct)

Simple.
>
-- Richard
--
Sep 13 '08 #7
Richard<rg****@ gmail.comwrites :
Andrew Poelstra <ap*******@supe rnova.homewrite s:
[...]
>Here is your code, corrected for the (mainly stylistic) issues I
mentioned else thread. It is abundantly clear to me now what the
problem is. I hope it is also clear to you:

/* Begin uncompilable segment */

memset(hist, 0, 256);

Horrible. Code fails review.
Did you have something in mind other than the use of the magic number
256?
>for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{
k = rgb1[i][j];
if(hist[k] == 0)
++count;
++hist[k];
}

sum_hist = malloc(count * sizeof *sum_hist);
for(i = 0; i < count; i++)
sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);

Silly to use sizeof on a variable element. Overly clever and liable to
mislead. It also assumes optimization will spot the obvious.
It's simply an instance of the usual clc idiom for calling malloc:
ptr = malloc(count * sizeof *ptr);
with "sum_hist[i]" replacing "ptr". It won't mislead anyone who's
familiar with the idiom. As for assuming that "optimizati on will spot
the obvious", I'm afraid I have no idea what you mean; would you care
to explain? (If you're referring to not evaluating the operand of
sizeof, that's not an optimization, it's a language requirement.)

[snip]

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '08 #8
biplab <ge*******@gmai l.comwrites:
for(i=0;i<256;i ++)
{
hist[i]=0;
}
for(i=0;i<heigh t;i++)
{
for(j=0;j<width ;j++)
{
k=(long int)rgb1[i][j];
hist[k]=hist[k]+1;
}
}

for(i=0;i<256;i ++)
{
if(hist[i]!=0)
tmp++;
else continue;
}

sum_hist=(int** )malloc(tmp*siz eof(int*));
for(i=0;i<tmp;i ++)
sum_hist[i]=(int*)malloc(2 *sizeof(int));

for(i=0;i<256;i ++)
{
if(hist[i]!=0)
{
//printf("%d %d\n",i,hist[i]);
sum_hist[i][0]=(int)i;
sum_hist[i][1]=(int)hist[i];
printf("%d %d\n",sum_hist[i][0],sum_hist[i][1]);

}
else continue;
}
for(i=0;i<tmp;i ++)
printf("%d\n",s um_hist[i][1]);

In the above code segment sum_hist is a double pointer of type long
int...
Don't tell us, show us. In other words, post a complete compilable
program. You haven't shown us *any* declarations. Don't make us
guess how everything is declared.

I assume you mean that num_hist is of type long int**. Please don't
refer to a pointer-to-pointer type as a "double pointer"; that refers
to a pointer to type double.
the problem is that when the values of sum_hist is printed withn
the is block..it is giving correct result..but when outside the for
loop..the value is printed......wr ong values are printed.....how to
correct the code...
An ellipsis...i.e. , a sequence of three dots...doesn't mean...what
you...think it...means.

If you use more nearly standard punctuation (a sentence ends with a
single period, the first letter of a sentence is capitalized), it will
be easier for us to read what you write, and therefore easier for us
to help you. We don't insist on flawless spelling or grammar, but
please make some effort.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '08 #9
Richard<rg****@ gmail.comwrites :
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
>In article <ga**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote:
>>> sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);
>>>Silly to use sizeof on a variable element. Overly clever and liable to
mislead.

I don't see why. How would you be misled?

It suggests to the naive programmer that there are variable sized
elements. Of course there can not be. We know that. The reader should
know that. But why state it in this way? Just "silly". Not the worst in
the world I agree. But stylewise I personally do not like it.
Perhaps naive progammers would benefit from the learning experience.
>>>It also assumes optimization will spot the obvious.

What optimisation are you talking about? The result of sizeof is (in
the absence of variable-length arrays) a constant, and the constant
will be just as constant as if you had written, say, the type name.
It will surely have been determined before code generation.

One would hope so. But its not a feat of genius to simply make it struct
and use a static sizeof.
So there is no "optimizati on" involved.
*shrug* More a style thing. Can you be sure that

2 * sizeof h[i]

is compiled to the same as

2 * sizeof h[0]

for example?
You can't be sure that it will compile to the same code, but you can
be certain it will have the same effect (assuming a non-buggy
compiler). And evaluating the operand of sizeof is the kind of bug
that I'd expect to be caught long before the compiler goes out the
door. You might as well worry about whether sizeof(char)==1 .
Pretty much yes, but why introducde confusion., Struct that
sucker anyway.

sizeof(elementS truct)

Simple.
Ok, so sum_hist is an array of elementStruct (apparently a typedef;
you didn't mention that), and

sum_hist[i] = malloc(2 * sizeof(elementS truct));

does the right thing. Until later, during program maintenance, when
sum_hist is changed so it's an array of some_other_type . Your version
continues to compile without complaint. If some_other_type is bigger
than elementStruct, you're wasting space; if it's slightly smaller,
it's possible that the error won't even be detected until you're
demonstrating the code for an important customer.

Or you can just write:

sum_hist[i] = malloc(2 * sizeof *sum_hist[i]);

and when the type of sum_hist is changed, the code will continue to
compile *and* to work properly (though I might want a declared
constant rather than 2). It's not a universally known idiom, but it's
easy to understand once you know the purpose.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '08 #10

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

Similar topics

4
1526
by: F Da Costa | last post by:
Hi, Small question re the use of an Array. I'v got an array with x rowObjects in it. When i get it the type of the container is Array and i can get a hold of the objects just fine. owever, how do i get the length of the array? For container.length does not seem to work.
65
12619
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
0
1065
by: raca | last post by:
Please look at this code fragment: #region Custom Code - Generated_Methods // Generated_Discussion_Methods public abstract IDataReader GetDiscussion(); public abstract void DeleteDiscussion(int itemID); #endregion // Custom Code - Generated_Methods // Generated_NEWCLASS_Methods public abstract IDataReader GetNEWCLASS();
5
2883
by: Nadav | last post by:
Hi, Introduction: ************************************************************ I am working on a project that should encrypt PE files ( Portable executable ), this require me to inject some code to existing PEs. First, I have tried: 1. to inject some code to the end of the ‘.text’ segment of an existing PE 2. to set the entry point RVA to the address of the injected code 3. at the end of the injected code I have set a jmp to the...
1
320
by: Dr. Zharkov | last post by:
Hello. A problem in the following. In VB .NET 2003 I create the project and in file Form1.vb is written down the following code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Create the data: CreateData() 'Error End Sub
8
3474
by: raghu | last post by:
Can anyone please tell me what are the different segments in a C program? Where can I find more details regarding this? Please help. Merry Christmas and Happy New Year Regards, Raghu
19
1834
by: dl | last post by:
I'll try to clarify the cryptic subject line. Let's say I have a base class 'GeometricObject' with a virtual method 'double distance (const GeometricObject &) const'. Among the derived classes I have eg 'Segment', representing a segment of a line, and 'Arc', representing an arc of circle. What is the right place to put the code for computing the distance between a Segment and an Arc? It is not specific to the Segment nor to
4
6987
by: jesjak | last post by:
hi all, can any one give me some explanation of "wat is .bss segment in object file" and what it will contain and difference between data segment and .bss segment.... help me.... thanks, jes
3
2155
by: madshov | last post by:
Hi, I have the following: <start> <segment Id="AAA"> <element Id="id">1</element> <element Id="seq">122</element> <element Id="seq2" Composite="yes">
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.