Solved

malloc in unix

Posted on 2003-11-11
4
749 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 50 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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