473,802 Members | 1,972 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
13 1388
Keith Thompson <ks***@mib.orgw rites:
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?

Nope.
>
>>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:
I know what it is. And what form of it it is.
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]
--
Sep 13 '08 #11
On Sat, 13 Sep 2008 14:45:49 GMT, Andrew Poelstra
<ap*******@supe rnova.homewrote :
>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);
Only if sizeof hist[i] is 1 which is unlikely for anything other than
a char.
>
Or potentially:
memset(hist, 0, sizeof hist);
Why was the mistake prone recommendation above a definite and this
which looks correct a potential?
--
Remove del for email
Sep 14 '08 #12
On Sat, 13 Sep 2008 07:17:08 -0700 (PDT), biplab <ge*******@gmai l.com>
wrote:
>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*));
Since you claim below that sum_hist is a long**, this may not allocate
the correct amount of space if sizeof(int*) < sizeof(long*).

In any event, this code requires a diagnostic because there is no
implicit converstion between int** and long**.
> for(i=0;i<tmp;i ++)
sum_hist[i]=(int*)malloc(2 *sizeof(int));
This code is even more likely to fail since sizeof(long) is more
frequently greater than sizeof(int). The same diagnostic is required
regarding the implicit pointer conversion.
>
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]);
%d requires an int. You are passing a long. This invokes undefined
behavior.
>
}
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...
Post a compilable example that demonstrated the undesired behavior.

--
Remove del for email
Sep 14 '08 #13
On 2008-09-14, Barry Schwarz <sc******@dqel. comwrote:
On Sat, 13 Sep 2008 14:45:49 GMT, Andrew Poelstra
<ap*******@sup ernova.homewrot e:
>>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);

Only if sizeof hist[i] is 1 which is unlikely for anything other than
a char.
Of course. My bad.
>>
Or potentially:
memset(hist, 0, sizeof hist);

Why was the mistake prone recommendation above a definite and this
which looks correct a potential?
The original code did not declare any variables (hence my
mistake above) and I was unsure if hist was an array or a
pointer.

--
Andrew Poelstra ap*******@wpsof tware.com
To email me, use the above email addresss with .com set to .net
Sep 14 '08 #14

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
12629
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
1066
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
2885
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
3476
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
1836
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
2156
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
9699
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
9562
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,...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
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
9115
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
5494
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
4270
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
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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.