Solved

malloc in unix

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

Independent Software Vendors: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

696 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