2D char array with dynamic mem allocation gives mem corruption

This is a discussion on 2D char array with dynamic mem allocation gives mem corruption within the C Programming forums, part of the General Programming Boards category; Hi guys, Iam struck with 2D char array! Hope, you could help on this. My requirement is, I want to ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Smile 2D char array with dynamic mem allocation gives mem corruption

    Hi guys,

    Iam struck with 2D char array! Hope, you could help on this.

    My requirement is, I want to add the string in string array and I dont know the size at compile time. Hence, have to use dynamic mem allocation.

    Here is the code what iam trying..

    Code:
    void main(void)
    {
    	char **name, string[100] = "Xyz String";
    	int i=0, n=25, index=0, numOfRows=1;
    	name = (char **)calloc(numOfRows, sizeof(char **));
    
    	for(i=0; i<n; i++)
    		AddStr(name, &index, string);
    
    	for(i=0; i<index; i++)
    		printf("--->%03d %s\n",i,name[i]);
    
    	for(i=0; i<index; i++)
    		free(name[i]);
    	free(name);
    
    
    }
    
    int AddStr(char **name, int *index, char *str)
    {
    	int i=0;
    
    	//reallocating the mem for 2D row size as its incrementing every time. // NOT Sure, is this required or correct?
    	name = (char **)realloc(name, ((*index)+1) * sizeof(char **));
    
    	//allocating the mem for column for every row
    	name[*index] = (char *) calloc(strlen(str)+1, sizeof(char));
    	strcpy(name[(*index)++],str);
    }
    
    Pls. help to to solve this!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Addstr has no way to return the new value stored in name, should it get moved in memory by the realloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Hi Salem,

    Thank you. You are right and it works fine after returning/storing the pointer of newly allocated mem location.

    regards,
    Balaji

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a dynamic array of char
    By Sharke in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2009, 12:25 AM
  2. put matrix from file into 2d dynamic array
    By meriororen in forum C Programming
    Replies: 3
    Last Post: 06-08-2009, 07:51 AM
  3. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21