Solved

malloc in unix

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

910 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now