?
Solved

malloc in unix

Posted on 2003-11-11
4
Medium Priority
?
752 Views
Last Modified: 2008-02-26
hi i wrote  a program in mandrake 9.1 version. ı had to use two dimensional array
char **inputdata;
int count;
inputdata = (char**)malloc(10*sizeof(char));
works correctly but after this ı wrote
for(count=0;count<9;++count)
inputdata[count] = (char*)malloc(5*sizeof(char));

first allocation step wroks correctly(for char**) but second doesnt.
there is no compile mistake.also this code works in mandrake 9.1 version but because of malloc problem it does not work my school's unix server. &#305; tried many ways.
if any one interests with whole code &#305; can send. pls mail at sinacetiner@yahoo.com for whole code
0
Comment
Question by:sinacetiner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 200 total points
ID: 9722040
You need sizeof(char*) for the array of char*.

Like this:
--------8<--------
#include <stdio.h>

int main()
{
        char **inputdata;
        int count;
        inputdata = (char**)malloc(9*sizeof(char*));
        for(count=0;count<9;++count)
                inputdata[count] = (char*)malloc(5*sizeof(char));
        for(count=0;count<9;++count)
                sprintf(inputdata[count],"%04d",count);
        for(count=0;count<9;++count)
                printf("String %d is [%s] length %d\n",count+1,inputdata[count],strlen(inputdata[count]));
        for(count=0;count<9;++count)
                free(inputdata[count]);
        free(inputdata);
}
--------8<--------
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9722043
Hi sinacetiner,

> inputdata = (char**)malloc(10*sizeof(char));
inputdata = (char**)malloc(10*sizeof(char*));


Cheers!
Sunny:o)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question