memory allocation problem in 2d array

Discussion in 'MFC' started by rinuvc, Mar 12, 2008.

  1. rinuvc

    rinuvc New Member

    Joined:
    Mar 12, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    --------------------------------------------------------------------------------

    hi all

    am havin a problem with memory allocation of 2d array

    can any body help me out ...


    it goes like this

    char ** a;

    in a loop for each time

    a = new char[strlen(name)+1];

    strcpy(a,name);

    a[strlen(name)+1]='\0';


    in a loop i deallocate it with

    delete[] a;

    after the loop

    delete [] a;



    can any body help me with this ????
    thanks in advance... :confused:
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    According to you i run this programm.
    No problem . Can you specify your problem problem?
    Code:
    #include<string.h>
    #include<stdio.h>
    
    int main()
    {
      char ** a;
      char *name="Go4Expert";
      int i,n=5;
      for(i=0;i<n;++i)
      {
        a[i] = new char[strlen(name)+1];
        strcpy(a[i],name);
        a[i][strlen(name)+1]='\0';
      }
      for(i=0;i<n;++i)
      {
        printf("%s",a[i]);
      }
      for(i=0;i<n;++i)
        delete[] a[i];
      delete a;
      return 0;
    }
    
     
  3. rinuvc

    rinuvc New Member

    Joined:
    Mar 12, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    i have done it in vc++

    this is the code

    Code:
    char ** interfaceName; 
    
    void CIDLDETAILS :: getInterface(char Buff[],int len) //parses the internal interface from the file
    {
    	int j,k = 0,cnt = 0,count = 0,flag = 0;
    	char keyword[] = Interface;
    	int	keywordLength = strlen(keyword);
    	char interfaceNameTemp[60];
    	interfaceName = new char *[50];   // :confused: 
    	for(int i = 0; i < len; i++)
    	{	
    		
    	for( j = 0 ; j <keywordLength  && (keyword[j] == Buff[i+j]); j++)
    	{
    	}
    	if (j == keywordLength)
    	{
    	      if(Buff[i-1] == SPACE || Buff[i-1] == NEWLINE) 
     	       if(Buff[i+j] == SPACE )
    	       {
    	          while(Buff[i+j] != SPACE )
    	          {
    		i++;
    	}
    	while(IsCharAlphaNumeric( Buff[j+i+k+1] ) || Buff[j+i+k+1] == UNDERSCORE)
    	{
    	interfaceNameTemp[k] = Buff[j+i+k];
    	k++;
    	}
                    interfaceNameTemp[k] = Buff[j+i+k];
    	interfaceNameTemp[k+1] = ENDOFARRAY;
    	count = cnt;
    	flag = 0;
    	while(count != 0)                                    //Avoiding the Duplicate Interfaces
    	{
    	    if((strcmp(interfaceNameTemp,interfaceName[count-1])) == 0)
    	    {
    		flag = 1;
    		break;
                        }
    	    count--;
    	}
    	if(flag == 0)
    	{
    	   interfaceName[cnt] = new char[strlen(interfaceNameTemp)+1]; :confused: 
    	   strcpy(interfaceName[cnt],interfaceNameTemp); :confused: 
    	   interfaceName[cnt][k+1] = '\0'; :confused: 
           	   cnt++;
    	}
                    k = 0;
                  }
              }
           }
        m_InterfaceCount = cnt;
        for(int i = 0; i < cnt ; i++)
           MessageBox(CString(interfaceName[i]));
    
    for(int i = 0; i < m_InterfaceCount ; i++)
    	   delete[]interfaceName[i]; :confused: 
    	delete []interfaceName; :confused: 
    
    }


    am trying to parse a file put the internal interface of the idl file

    in to the array


    its running ok but as soon as i close the dialog box an go to the main dialog box
    its shows #2 stack around the variable m_dlg( which is a dialog object????) is corrupted
     
    Last edited by a moderator: Mar 13, 2008
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    I think it's not part of c++. You can put this querry in win32/MFC forum.
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Please ensure that no duplication of any article/qwuerry should be.

    So please shabbir just close this thread.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have closed your other thread and moved the current thread to MFC/Win32 forum
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice