Solved

malloc in unix

Posted on 2003-11-11
4
703 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
Comment Utility
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
Comment Utility
Hi sinacetiner,

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


Cheers!
Sunny:o)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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 opening and reading files in the C programming language.

772 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

10 Experts available now in Live!

Get 1:1 Help Now