473,805 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
16 1986
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_Keit h) 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**********@r eadme.uio.no>,
"Ronny Mandal" <ro*****@math.u io.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.ne t>,
Artie Gold <ar*******@aust in.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.u io.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
2939
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
8
1598
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 "d:\db\db-4.1.25\", which has 16 characters; but when it arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)", which has 17 characters. What is even more interesting is that the test "if (dir != '\\')" fails when there is a clubs at the...
10
2602
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 optimization). the codec between /***********/ is the initialization code. What supprise me a lot is the code with initialization(io==1) is much faster than without initialization(io!=1). The initialization code should takes some time and it should...
9
1462
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 *e, int *current, int nbre) {
9
1955
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
3565
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 to resolve this problem. Thanks in advance. #include <stdlib.h> #include <string.h> #include <stdio.h>
23
2148
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
2911
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> #include <stdlib.h> #include <mpi.h> #define MASTER 0 // Make code more readable with names #define OTW 1 //"..." #define ITM 2 //"..."
5
1575
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 the next code example. My code is: #include<stdio.h> #include<stdlib.h>
0
9596
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
10617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10364
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
10370
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
9186
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...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5545
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...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3008
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.