コード例 #1
0
//sample usage:
int main(){
    int i=3;
    char *q = NULL;
    Sasprintf(q, "select * from tab");
    Sasprintf(q, "%s where col%i is not null", q, i);
    printf("%s\n", q);
}
コード例 #2
0
ファイル: g_string.c プロジェクト: jkoz/gcc-learn
void mem_leak_asprintf() {
	int col_number=3, person_number=27;

    /*NOT OK , free cause fail below Sasprintf*/
	char *q;

    /* fail with Sasprintf because it tries to free immutable string */
	q = "asdsad";

	/*free(NULL) cause segfault*/
	q = NULL;

	/* q should be initialize with strdup or set to null in order to use
 	   Sasprintf macro otherwise a segfault is raised*/
	q = strdup("select ");

	/*memory leak for each of following steps*/
	/*asprintf(&q, "%scol%i \n", q, col_number);*/
	/*asprintf(&q, "%sfrom tab \n", q);*/
	/*asprintf(&q, "%swhere person_id = %i", q, person_number);*/

	int i = 3;
	Sasprintf(q, "select * from tab");
	Sasprintf(q, "%s where col%i is not null", q, i);
	printf("%s\n", q);

	/* only free the last allocated memory */
    free(q);
}
コード例 #3
0
ファイル: newick.c プロジェクト: camwebb/shiba
phylo parseNewick(char *in) {

  phylo p;
  int lbrack = 0;
  int rbrack = 0;
  int comma = 0;

  // dimension the phylo struct
  for (int i = 0; i < strlen(in); i++) 
    {
      // printf("%c", in[i]);
      if      (in[i] == 40) lbrack++;
      else if (in[i] == 41) rbrack++;
      else if (in[i] == 44) comma++;
    }
  if (lbrack != rbrack) error("Imbalanced parentheses in phylogeny file");

  p.nnodes = lbrack + comma + 1; 
  p.parent = mem1d_i(p.nnodes);
  p.ndaughter = mem1d_i(p.nnodes);
  p.depth = mem1d_i(p.nnodes);
  p.bl = mem1d_d(p.nnodes);
  p.taxon = mem2d1_c(p.nnodes);
  p.age = mem1d_d(p.nnodes);

  // Move through the newick format tree character by character 
  int i = 0;
  // start outside parentheses, one hypothetical node proximal to root node
  int node = -1;
  int parent = -2;
  int nodeCount = 0;
  int tmpn;

  while ((i < strlen(in)) && (in[i] != 59))
    {
      // descend a branch, making new node for the parenthesis descended thru
      if (in[i] == 40) // "("
        {
          parent = node;
          // make a new node, set node to index of final row
          nodeCount++;
          node = nodeCount-1;
          p.parent[node] = parent;
          // first run case, since root has no parent data 
          if (node == 0) {
            p.depth[node] = 0;
          } else {
            p.depth[node] = p.depth[parent] + 1;
            p.ndaughter[parent]++;
          }
          /* printf("%c DOWN %d(%d)\n", in[i], node, parent);
             printf("  new node %d, parent %d, depth %d\n", 
             node, p.parent[node], p.depth[node]); */
          i++;
        }

      // sibling taxon, ignore
      else if (in[i] == 44) // ","
        {
          node = parent;
          parent = p.parent[node];
          // printf("%c ACROSS %d(%d)\n", in[i], node, parent);
          i++;
        }

      // back up a node to parent, keep track of locn with node
      else if (in[i] == 41) // ")"
        {
          node = parent;
          parent = p.parent[node];
          // printf("%c UP %d(%d)\n", in[i], node, parent);
          i++;
        }

      else
        {
          // Now, check for new taxa, or new interior name

          //! \todo deal with quoted labels
         
          // Is there nodename here?  I.e., not `:' or `['
          if ((in[i] != 58) && (in[i] != 91))
            {
              // Is it a new terminal? I.e., not following a `)' 
              if (in[i-1] != 41)
                {
                  // creat new node
                  parent = node;
                  nodeCount++;
                  node = nodeCount-1;
                  p.parent[node] = parent;
                  p.depth[node] = p.depth[parent] + 1;
                  p.ndaughter[parent]++;
                  
                  /* printf("%c TERM %d(%d)\n", in[i], node, parent);
                  printf("  new node %d, parent %d, depth %d\n", node, 
                  p.parent[node], p.depth[node]); */
                }
          
              char *tmp = NULL; tmpn = 0;
              // check for name, I.e. not `:' nor `['
              while ((in[i] != 58) && (in[i] != 91) && 
                     (in[i] != 44) && (in[i] != 41) && (in[i] != 40) &&
                     (in[i] != 59))
                {
                  if (!tmpn) {
                    asprintf(&tmp, "%c", in[i]); tmpn++;
                  } else {
                    char *tmp2 = tmp;
                    asprintf(&tmp, "%s%c", tmp2, in[i]); tmpn++;
                    free(tmp2);
                  }
                  i++;
                }
              // was there any taxon name?
              asprintf(&p.taxon[node], "%s", tmp);
              // printf("  taxon name %s\n", tmp);
              free(tmp);
            }
          
          // are there bls?
          if (in[i] == 58)
            {
              i++; // skip over delimiter `:'
              char *tmp = NULL; tmpn = 0;
              // bead bl string, I.e. not `['
              while ((in[i] != 91) &&
                     (in[i] != 44) && (in[i] != 41) && (in[i] != 40) &&
                     (in[i] != 59)) // watch out for final `;'
                {
                  if (!tmpn) {
                    asprintf(&tmp, "%c", in[i]); tmpn++;
                  } else {
                    Sasprintf(tmp, "%s%c", tmp, in[i]); tmpn++;
                  }
                  i++;
                }
              p.bl[node] = atof(tmp);
              // printf("  taxon bl %f\n", p.bl[node]);
              free(tmp);
            }
          
          // are there notes?
          if (in[i] == 91)
            // Discard note
            while ((in[i] != 44) && (in[i] != 41) && (in[i] != 40) &&
                   (in[i] != 59))
              i++;
        }
    }

  /* printf("%s\n", in);
  for (int n = 0; n < p.nnodes; n++)
      printf("%3d %3d %3d %3d %5.2f %s\n", n, p.parent[n], p.depth[n], 
      p.ndaughter[n], p.bl[n], p.taxon[n]); */


  return p; 
}