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

problems with malloc

Dear members, I am new to this forum and to the C language. I have stumble to this memory allocation problem and I shave spent the last 10 hours trying to find why this piece of code does not work (I compile with Dev C++): as I try to set the vectors Value_old and Value_new to zero the code crushes. I hope you guys can help me on this. Thanks much.

AntoRoma

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define TOTAL_LENGTH   170 
  5. #define POPSIZE               10
  6. #define MEM1 (2 * POPSIZE * sizeof(int *))
  7. #define MEM2 (2 * POPSIZE * TOTAL_LENGTH * sizeof(int))
  8. #define MEM (MEM1 + MEM2)
  9.  
  10. int main()
  11.   int i,j;
  12.   int **Value_old, **Value_new;
  13.   double *ptr;
  14.  
  15.   ptr = malloc(MEMSIZE);
  16.   if(!ptr) exit(-1);
  17.  
  18.   Value_old = (int **)  ptr;
  19.   Value_new = (int **) (ptr + POPSIZE * sizeof(int *));
  20.   for(i = 0; i < POPSIZE; ++i)
  21.   {
  22.         Value_old[i] = (int *)(ptr + MEM1 + i * TOTAL_LENGTH * sizeof(int));
  23.         Value_new[i] = (int *)(ptr + MEM1 + (POPSIZE + i) * TOTAL_LENGTH * sizeof(int));
  24.         for(j = 0; j < TOTAL_LENGTH; ++j)
  25.         {
  26.              Value_old[i][j] = 0;
  27.              Value_new[i][j] = 0; 
  28.         }                         
  29.   }
  30.  
  31.   return 1;
  32. }
Mar 18 '07 #1
6 1745
arne
315 Expert 100+
Dear members, I am new to this forum and to the C language. I have stumble to this memory allocation problem and I shave spent the last 10 hours trying to find why this piece of code does not work (I compile with Dev C++): as I try to set the vectors Value_old and Value_new to zero the code crushes. I hope you guys can help me on this. Thanks much.

AntoRoma

#include <stdlib.h>
#include <stdio.h>

#define TOTAL_LENGTH 170
#define POPSIZE 10
#define MEM1 (2 * POPSIZE * sizeof(int *))
#define MEM2 (2 * POPSIZE * TOTAL_LENGTH * sizeof(int))
#define MEM (MEM1 + MEM2)

int main()
{
int i,j;
int **Value_old, **Value_new;
double *ptr;

ptr = malloc(MEMSIZE);
if(!ptr) exit(-1);

Value_old = (int **) ptr;
Value_new = (int **) (ptr + POPSIZE * sizeof(int *));
for(i = 0; i < POPSIZE; ++i)
{
Value_old[i] = (int *)(ptr + MEM1 + i * TOTAL_LENGTH * sizeof(int));
Value_new[i] = (int *)(ptr + MEM1 + (POPSIZE + i) * TOTAL_LENGTH * sizeof(int));
for(j = 0; j < TOTAL_LENGTH; ++j)
{
Value_old[i][j] = 0;
Value_new[i][j] = 0;
}
}

return 1;
}
This should not compile at all, since MEMSIZE is not defined. Please define it and see if your code works as desired.
Mar 18 '07 #2
This should not compile at all, since MEMSIZE is not defined. Please define it and see if your code works as desired.
Hi:
MEMSIZE is in fact what I call MEM. But even with such change it seems that my code does not work. Any clue? Thanks much

A
Mar 18 '07 #3
dmjpro
2,476 2GB
if u use MEM instead of MEMSIZE then what happens with it.....
Mar 19 '07 #4
arne
315 Expert 100+
Hi:
MEMSIZE is in fact what I call MEM. But even with such change it seems that my code does not work. Any clue? Thanks much

A
Doesn't crash when I try it. But I can't tell if it works as desired, of course :)
Mar 19 '07 #5
Doesn't crash when I try it. But I can't tell if it works as desired, of course :)
Hi,
it seems that if I define ptr as a void * instead of double * the code goes thru fine. I am not sure way though.

Thanks for the help guys.

A
Mar 19 '07 #6
arne
315 Expert 100+
Hi,
it seems that if I define ptr as a void * instead of double * the code goes thru fine. I am not sure way though.

Thanks for the help guys.

A
Yes, the return value of malloc is a pointer to void. Usually one uses it with a cast, like
Expand|Select|Wrap|Line Numbers
  1. char *p;
  2. p = (char *) malloc( 100 * sizeof(char) );
  3.  
Mar 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Axel | last post by:
Hiho, here my Newbie question (Win32 GUI): I'm reading a file in binary mode to a char* named buffer. I used malloc(filesize) to make the needed space avaiable. The filedata in the buffer...
4
by: stephenma7 | last post by:
Hi, everybody. I am new here. I have encountered these many problems for the last couple of days. I have Linux Fedora Core 3(Gnu G++ 3.4.2), Linux Fedora Core 2 (Gnu G++ 3.3.3), Red Hat 9 (Gnu...
8
by: Magnus Malm | last post by:
Hello I am not quite sure if this is the right place to post this question, but I hope this group is one of the rare ones that forgive less wise ones like me. :) I have quite a hard time...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
14
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works,...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
7
by: lancer6238 | last post by:
Hi, I'm writing a program that separates a set of integers into groups and then quicksort each group individually. However, I'm having problems with my realloc() function. (Pardon me if the...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.