473,387 Members | 1,453 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,387 software developers and data experts.

Strange thing about malloc()

Hi!

I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?

--

Regards,

Ronny Mandal
This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.
Nov 14 '05 #1
16 1947
>This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.
It's absolutely ridiculous to put this on a worldwide USENET posting.
Don't threaten people you want to help you.
I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.


Describe how you can tell the difference between "works" and "doesn't work".
Some code would be nice.

I suspect that you are running over the end of the array, and that
you can get away with it with an odd number (possibly because malloc()
rounds up the size), but not with an even number (when it might not
round up). But there's no way to tell without code.

Gordon L. Burditt
Nov 14 '05 #2
Ronny Mandal wrote on 19/04/05 :
I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?


Hard to say. What do you meant by "it will not work." ? Post a
compilable snippet that exposes the problem with a description of the
environment (required inputs, expected outputs...)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #3


Ronny Mandal wrote:
Hi!

I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?


The latter. My crystal ball tells me your error
is on line 42.

"Doctor, it hurts!"

"What hurts?"

"What do you think you're doing, snooping in my
personal affairs?"

--
Er*********@sun.com

Nov 14 '05 #4
It's absolutely ridiculous to put this on a worldwide USENET posting.
No it is not. I've noticed stranger things about c-releases, e.g in
non-stable releases that is almost like "doesn't work when the moon is
full", which other people from the USENET have had issues with. And then,
because of the USENET, the inquirer receives help.
Don't threaten people you want to help you.
I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.
Describe how you can tell the difference between "works" and "doesn't
work".
Some code would be nice.


Well, in general people on USENET is not interested in posted code. But
after trying to isolate the problem into a few lines of code, it still
behaves strange.

I suspect that you are running over the end of the array, and that
you can get away with it with an odd number (possibly because malloc()
rounds up the size), but not with an even number (when it might not
round up). But there's no way to tell without code.
This seems like a plausible answer, regarding the "you can get away with it
with an odd number". I've never read anyplace about how malloc works
"internally", but maybe it is written in the FAQ. But since the FAQ is
really extensive, I sometimes ask people from ther USENET.
Gordon L. Burditt


--

Ronny Mandal
Nov 14 '05 #5
Gordon Burditt wrote:
This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.

It's absolutely ridiculous to put this on a worldwide USENET posting.
Don't threaten people you want to help you.


That part of the sig is likely put there by the server through which the
message was posted. It's probably not the OP's fault.

--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #6
Ronny Mandal wrote:
It's absolutely ridiculous to put this on a worldwide USENET posting.

No it is not. I've noticed stranger things about c-releases, e.g in
non-stable releases that is almost like "doesn't work when the moon is
full", which other people from the USENET have had issues with. And then,
because of the USENET, the inquirer receives help.


He was referring to the stuff at the botom of your sig.
Don't threaten people you want to help you.

I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.
Describe how you can tell the difference between "works" and "doesn't
work".
Some code would be nice.

Well, in general people on USENET is not interested in posted code. But
after trying to isolate the problem into a few lines of code, it still
behaves strange.

Not true. Best is to post the *minimal* compilable code that exhibits
the behavior that made you post in the first place.
I suspect that you are running over the end of the array, and that
you can get away with it with an odd number (possibly because malloc()
rounds up the size), but not with an even number (when it might not
round up). But there's no way to tell without code.

This seems like a plausible answer, regarding the "you can get away with it
with an odd number". I've never read anyplace about how malloc works
"internally", but maybe it is written in the FAQ. But since the FAQ is
really extensive, I sometimes ask people from ther USENET.


Understood. When it comes to malloc(), however, its internal workings
should be generally transparent. As has been mentioned above, an overrun
in some other piece of dynamically allocated memory is likely the culprit.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #7
"Ronny Mandal" <ro*****@math.uio.no> writes:
I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?
You have a typo on line 372 of main.c.

I'm only guessing, but that's all I can do with the information you've
provided. What is your program attempting to do? How exactly does it
not work?

Can you post a small complete program that exhibits the problem?

(If it's a Win32 issue, we can't help you here.)
This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure.

[snip]

It's not an e-mail, it's a Usenet posting. I suggest you drop the
meaningless disclaimer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
In article <3c*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
That part of the sig is likely put there by the server through which the
message was posted. It's probably not the OP's fault.


Quite so, but if we don't complain to people who use such servers, how
are they ever going to be replaced?

If, by choice, you use a server that adds this sort of thing to your
message, it makes you look silly. If you work for a company that puts
it on all your outgoing messages, it makes your company look as if
they don't understand the net, and are probably the sort who spend
more money on lawyers than products.

-- Richard
Nov 14 '05 #9
If you're interested in the workings of malloc(), you should check out this:

http://www.ibm.com/developerworks/li...rary/l-memory/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #10
Ronny Mandal wrote:
Hi!

I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?


There is almost certainly a bug somewhere in *your* code, my guess would
be overrunning the buffer you have created. Reduce your program to the
smallest compilable example that exhibits the problem and does not use
extensions and post it here and you might get some help.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #11
Sorry about the footer, sometimes I forget to remove it, it comes
automatically, but not on my personal behalf.

Indeed, it is not my typing.
Nov 14 '05 #12
Jonathan Bartlett <jo*****@eskimo.com> writes:
If you're interested in the workings of malloc(), you should check out this:

http://www.ibm.com/developerworks/li...rary/l-memory/


Correction: If you're interested in the workings of some
system-specific implementations of malloc().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
In article <d4**********@readme.uio.no>,
"Ronny Mandal" <ro*****@math.uio.no> wrote:
Hi!

I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?


Have a good look at your code. Are you by any chance writing to a [N] ?
Assuming that malloc () was successful, you are allowed to write to a
[0] up to a [N-1], but not a [N].
Nov 14 '05 #14
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <3c*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
That part of the sig is likely put there by the server through which the
message was posted. It's probably not the OP's fault.


Quite so, but if we don't complain to people who use such servers, how
are they ever going to be replaced?

If, by choice, you use a server that adds this sort of thing to your
message, it makes you look silly. If you work for a company that puts
it on all your outgoing messages, it makes your company look as if
they don't understand the net, and are probably the sort who spend
more money on lawyers than products.


IOW, like Microsoft ;-)

Richard
Nov 14 '05 #15
Thanks for helping me out, strange thing that I just *forgot* that a[N]
holds N-1 elements. malloc worked just fine from the beginning.

Ronny Mandal
Nov 14 '05 #16
On Fri, 22 Apr 2005 00:35:40 +0200, "Ronny Mandal"
<ro*****@math.uio.no> wrote:
Thanks for helping me out, strange thing that I just *forgot* that a[N]
holds N-1 elements. malloc worked just fine from the beginning.

Um, not exactly. When you have
int* a = malloc( sizeof( int ) * N ) ;

a can hold N elements, but the first one is a[0] and the last one is
a[N-1].

Probably what you meant.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #17

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

Similar topics

6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
8
by: Noah Roberts | last post by:
There is some windows specific code in this, but I believe the issue to be something standard so... The problem is that after attaching the name of the subdirectory I have something like...
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
9
by: sylsau | last post by:
Hi, I am doing a little program who calculates the permutation of a set of vertex. I use the recursivity for this calcul. My function who calculate the permutations : void permutation(set...
9
by: Me | last post by:
Hi, I ran into a malloc problem but I can't find the solution. I try to read a file into a variable with malloc like this: BYTE *lcdata; lcdata = malloc(fsize*sizeof(BYTE));
2
by: rkk | last post by:
Hi, My mergesort program is below. There is a small piece of logical error/bug in this code which I can't figure out, as a result the output array isn't completely sorted. Requesting your help...
23
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
2
by: angryRotarian | last post by:
Im trying to write a MPI C program for a debian cluster that multiplies matricies. Ive written almost all of it, but cant seem to shake a strange problem. Here is the code: #include <stdio.h>...
5
by: MN | last post by:
Hi, I was writing a program that must dynamically allocate memory to a 2- dimensions array, initialize it to 0, then place the result of multiplication of two 1-dimension arrays as described in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.